Java 在paintComponent中使用图形
我有下面的代码,b代表另一个类的ball类。如果我使用b.draw(g,true);但是,如果我使用g.fillOval(b.getX()-b.getRadius(),b.getY()-b.getRadius(),2*b.getRadius(),2*b.getRadius()),我在屏幕上看不到任何结果;我得到了很好的结果,我认为他们都使用图形,应该工作相同 提前谢谢! 钙 完整代码:Java 在paintComponent中使用图形,java,swing,user-interface,graphics,paintcomponent,Java,Swing,User Interface,Graphics,Paintcomponent,我有下面的代码,b代表另一个类的ball类。如果我使用b.draw(g,true);但是,如果我使用g.fillOval(b.getX()-b.getRadius(),b.getY()-b.getRadius(),2*b.getRadius(),2*b.getRadius()),我在屏幕上看不到任何结果;我得到了很好的结果,我认为他们都使用图形,应该工作相同 提前谢谢! 钙 完整代码: import java.awt.Color; import java.awt.Graphics; public
import java.awt.Color;
import java.awt.Graphics;
public class Balloon
{
private int xCenter, yCenter, radius;
private Color color;
/**
* Constructs a balloon with the center at (0, 0),
* radius 50, and blue color
*/
public Balloon()
{
xCenter = 0;
yCenter = 0;
radius = 50;
color = Color.BLUE;
}
/**
* Constructs a balloon with a given center, radius and color
* @param x x-coordinate of the center
* @param y y-coordinate of the center
* @param r radius of the balloon
* @param c color of the balloon
*/
public Balloon(int x, int y, int r, Color c)
{
xCenter = x;
yCenter = y;
radius = r;
color = c;
}
/**
* Returns the x-coordinate of the center.
*/
public int getX()
{
return xCenter;
}
/**
* Returns the y-coordinate of the center.
*/
public int getY()
{
return yCenter;
}
/**
* Returns the radius of this balloon.
*/
public int getRadius()
{
return radius;
}
/**
* Returns the color of this balloon.
*/
public Color getColor()
{
return color;
}
/**
* Returns the distance from a given point to the
* center of this balloon.
* @param x, y coordinates of the point
*/
public double distance(int x, int y)
{
double dx = x - xCenter;
double dy = y - yCenter;
return Math.sqrt(dx*dx + dy*dy);
}
/**
* Moves the center of this balloon to (x, y)
* @param x x-coordinate of the new center
* @param y y-coordinate of the new center
*/
public void move(int x, int y)
{
xCenter = x;
yCenter = y;
}
/**
* Sets the radius of this balloon to r
* @param r new radius
*/
public void setRadius(int r)
{
radius = r;
}
/**
* Returns true if a given point is strictly inside this balloon;
* otherwise returns false
* @param x, y coordinates of the point
*/
public boolean isInside(int x, int y)
{
return distance(x, y) < 0.9 * radius;
}
/**
* Returns true if a given point is on the border of this balloon;
* otherwise returns false
* @param x, y coordinates of the point
*/
public boolean isOnBorder(int x, int y)
{
double d = distance(x, y);
return d >= 0.9 * radius && d <= 1.1 * radius;
}
/**
* Draws a solid circle if makeItFilled is true and
* outline only if makeItFilled is false
* @param g graphics context
* @param makeItFilled draws a solid circle if true
*/
public void draw(Graphics g, boolean makeItFilled)
{
g.setColor(color);
if (makeItFilled)
g.fillOval(xCenter - radius,
yCenter - radius, 2*radius, 2*radius);
else
g.drawOval(xCenter - radius,
yCenter - radius, 2*radius, 2*radius);
}
}
导入java.awt.Color;
导入java.awt.Graphics;
公营气球
{
专用int X中心、Y中心、半径;
私人色彩;
/**
*构造中心位于(0,0)的引出序号,
*半径50,蓝色
*/
公共气球()
{
xCenter=0;
中心=0;
半径=50;
颜色=颜色。蓝色;
}
/**
*构造具有给定中心、半径和颜色的引出序号
*@param x中心的x坐标
*@param y-中心的y坐标
*@param r气球的半径
*@param c气球的颜色
*/
公共气球(整数x,整数y,整数r,颜色c)
{
xCenter=x;
中心=y;
半径=r;
颜色=c;
}
/**
*返回中心的x坐标。
*/
公共int getX()
{
返回xCenter;
}
/**
*返回中心的y坐标。
*/
公共int getY()
{
返回中心;
}
/**
*返回此引出序号的半径。
*/
公共int getRadius()
{
返回半径;
}
/**
*返回此引出序号的颜色。
*/
公共颜色getColor()
{
返回颜色;
}
/**
*返回从给定点到目标点的距离
*这个气球的中心。
*@param x,y点坐标
*/
公共双倍距离(整数x,整数y)
{
双dx=x-xCenter;
双dy=y-中心;
返回Math.sqrt(dx*dx+dy*dy);
}
/**
*将此引出序号的中心移动到(x,y)
*@param x新中心的x坐标
*新中心的@param y y坐标
*/
公共无效移动(整数x,整数y)
{
xCenter=x;
中心=y;
}
/**
*将此引出序号的半径设置为r
*@param r新半径
*/
公共空间设置半径(整数r)
{
半径=r;
}
/**
*如果给定点严格位于此气球内,则返回true;
*否则返回false
*@param x,y点坐标
*/
公共布尔isInside(整数x,整数y)
{
返回距离(x,y)<0.9*半径;
}
/**
*如果给定点位于此引出序号的边界上,则返回true;
*否则返回false
*@param x,y点坐标
*/
公共布尔isOnBorder(int x,int y)
{
双d=距离(x,y);
返回d>=0.9*半径(&d)
如果我使用b.draw(g,true);我在屏幕上看不到任何结果
但是如果我使用g.fillOval(…),我得到了很好的结果
在这两种情况下,我都会看到文本和气球在屏幕上向上移动,因此它似乎对我很有用
但是,如果它对您不起作用,那么您需要进行一些调试,看看有什么不同
当我查看代码时,我看到:
g.fillOval(xCenter - radius, yCenter - radius, 2*radius, 2*radius);
及
因此,与其在方法调用中进行所有计算,不如编写如下代码:
int x = xCenter - radius;
int y = yCenter - radius;
int size = 2 * radius;
System.out.println(x + " : " + y " + " : " + size);
g.fillOval(x, y, size, size);
这允许您显示用于绘制椭圆形的值
对另一种方法也执行同样的操作
然后您可以比较这两种方法的输出,看看有什么不同。我们不知道您的b.draw(…)
方法可以。发布一个适当的例子来演示您的问题。ball
实际上是如何工作的?正如camickr已经建议的,一个可以运行的例子可以清楚地演示您的问题,它将为您提供更大的机会来获得帮助您解决问题的答案。发布两个程序的完整代码。“发布完整代码”提示:添加@camickr(或任何人,@
很重要)以通知此人新评论。每个评论只能通知一个人。
g.fillOval(xCenter - radius, yCenter - radius, 2*radius, 2*radius);
g.fillOval(b.getX() - b.getRadius(), b.getY() - b.getRadius(), 2*b.getRadius(), 2*b.getRadius());
int x = xCenter - radius;
int y = yCenter - radius;
int size = 2 * radius;
System.out.println(x + " : " + y " + " : " + size);
g.fillOval(x, y, size, size);