我有以下代码,b代表另一个类的球类。如果我使用 b.draw(g,true(;我没有从屏幕上看到任何结果,但是如果我使用 g.fillOval(b.getX(( - b.getRadius((, b.getY(( - b.getRadius((, 2*b.getRadius((, 2*b.getRadius(((;我得到了很好的结果,我认为他们都使用图形并且应该工作相同?
提前感谢! 卡鲁姆
public void paintComponent(Graphics g)
{
super.paintComponent(g); // Paint the background
g.setColor(Color.RED);
g.drawString("Hello, Action!", xPos-100, yPos-200);
// Not working
b.draw(g,true);
// Working
//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 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);
}
}
测试程序:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FlyBalloon extends JPanel
implements ActionListener
{
private int xPos, yPos; // hold the coordinates of the message
private Balloon b;
public FlyBalloon(Balloon b)
{
this.b = b;
}
// Called automatically after a repaint request
public void paintComponent(Graphics g)
{
super.paintComponent(g); // Paint the background
g.setColor(Color.RED);
g.drawString("Hello, Action!", xPos-100, yPos-200);
// Not working
b.draw(g,true);
// Working
//g.fillOval(b.getX() - b.getRadius(), b.getY() - b.getRadius(), 2*b.getRadius(), 2*b.getRadius());
}
// Called automatically when the timer "fires"
public void actionPerformed(ActionEvent e)
{
// Adjust the horizontal position of the message:
yPos--; // subtract 1
if (yPos < -100)
yPos = getHeight();
this.b.move(xPos,yPos);
repaint();
}
public static void main(String[] args)
{
JFrame window = new JFrame("Action Demo");
// Set this window's location and size:
// upper-left corner at 300, 300; width 400, height 600
window.setBounds(300, 300, 400, 600);
Balloon b = new Balloon(150,150,100,Color.RED);
// Create panel, a FlyBalloon object, which is a kind of JPanel:
FlyBalloon panel = new FlyBalloon(b);
panel.setBackground(Color.CYAN); // the default color is light gray
// Add panel to window:
Container c = window.getContentPane();
c.add(panel);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
// Set the initial position of the message:
panel.xPos = panel.getWidth() / 2;
panel.yPos = panel.getHeight() ;
// Create a Timer object that fires every 30 milliseconds;
// attach it to panel so that panel "listens to" and
// processes the timer events; start the timer:
Timer clock = new Timer(30, panel);
clock.start();
}
}
如果我使用 b.draw(g,true(;我在屏幕上看不到任何结果,
但是,如果我使用 g.fillOval(...( 我得到了很好的结果,
在这两种情况下,我都会看到文本和气球在屏幕上向上移动,所以它似乎对我有用。
但是,如果它对您不起作用,那么您需要进行一些调试以查看差异。
当我查看代码时,我看到:
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);
这允许您显示用于绘制椭圆的值。
对其他方法执行相同的操作。
然后,您可以比较两种方法的输出,看看有什么区别。