java - getHeight() 和 getWidth() 返回 0 用于绘制半圆

标签 java swing jframe

我正在尝试制作一个在用户运行时显示彩虹的 gui 应用程序。无论出于何种原因,半圆都不会出现在窗口中。我做了一个调试方法,它显示 x 和 y 都为 0。有人能告诉我哪里出了问题吗?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Rainbow extends JPanel
{
   // Declare skyColor:
  static Color skyColor = Color.CYAN;

public Rainbow()
{
setBackground(skyColor);
}

  // Draws the rainbow.

public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = getWidth();    
int height = getHeight();

// Declare and initialize local int variables xCenter, yCenter
// that represent the center of the rainbow rings:
int xCenter = (1/2)*width;
int yCenter = (3/4)*height;
// Declare and initialize the radius of the large semicircle:
int smallRadius = height/4;
int largeRadius = width/4;
int mediumRadius = (int) Math.sqrt(smallRadius * largeRadius);
g.setColor(Color.RED);
g.fillArc(xCenter - largeRadius/2,yCenter - largeRadius/2 + largeRadius/4 -height/4,largeRadius,largeRadius,0,180);
// Draw the large semicircle:

// Declare and initialize the radii of the small and medium
// semicircles and draw them:
g.setColor(Color.GREEN);
g.fillArc(largeRadius+mediumRadius/2, yCenter-(largeRadius+mediumRadius)/2, mediumRadius, mediumRadius, 0, 180);    
g.setColor(Color.MAGENTA);
g.fillArc(largeRadius+smallRadius/2, yCenter-(largeRadius+smallRadius)/2, smallRadius, smallRadius, 0, 180);      // Calculate the radius of the innermost (sky-color) semicircle
debug(xCenter,yCenter,smallRadius,largeRadius,mediumRadius);
// Draw the sky-color semicircle:
// ________________________________________________
}
public static void debug(int x,int y,int r1,int r2,int r3)
{
System.out.println("xCenter is " + x + ".");
System.out.println("yCenter is " + y + ".");
System.out.println("smallRadius is " + r1 + ".");       
System.out.println("largeRadius is " + r2 + ".");  
System.out.println("mediumRadius is " + r3 + ".");  
}
public static void main(String[] args)
{
  JFrame w = new JFrame("Rainbow");
  w.setBounds(300, 300, 300, 200);
  w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Container c = w.getContentPane();
  c.add(new Rainbow());
  w.setVisible(true);
}
}

最佳答案

问题出在这里:

int xCenter = (1/2)*width;   // this division truncates decimals, returns 0
int yCenter = (3/4)*height;

试试这个:

int xCenter = (int)((1.0/2)*width);  // this division preserves decimals
int yCenter = (int)((3.0/4)*height);

或者更简单:

int xCenter = (int)(0.5*width);
int yCenter = (int)(0.75*height);

您正在执行一个整数除法,此表达式:1/2 返回0,任何数字乘以00

关于java - getHeight() 和 getWidth() 返回 0 用于绘制半圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18881237/

相关文章:

java - Pyramid Exercise java没有完全对齐

java - 没有在 Java 中调用 paintComponent 方法

具有可更新 JProgressBar 的 Java Swing 线程

java - 为什么JFrame上有背景图片的按钮无法正常显示

Java枚举构造函数错误

java - 如何为类或方法创建自己的帮助

java - java中跳过一个字符

java - 将鼠标悬停在 JSplitPane 分隔线上时更改光标

java - 将当前显示的 JPanel 替换为 JFrame 中的另一个 JPanel

java - 将组件直接添加到 JFrame 中,还是将它们放在 JPanel 中?