import java.awt.*;
import javax.swing.*;public class Ball { private int x;
private int y;
private int vx = 1;
private int vy = 1;
private static final int SIZE = 20;
public Ball(int x, int y)
{
this.x = x;
this.y = y;
}
public void move(JComponent component)
{
x += vx;
y += vy;
Rectangle rect = component.getBounds();
int minX = (int)rect.getMinX();
int minY = (int)rect.getMinY();
int maxX = (int)rect.getMaxX();
int maxY = (int)rect.getMaxY();
if (x <= minX)
{
x = minX;
vx = -vx;
}
if (x + SIZE >= maxX)
{
x = maxX;
vx = -vx;
}
if (y <= minY)
{
y = minY;
vy = -vy;
}
if (y >= maxY)
{
y = maxY;
vy = -vy;
}
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getSize()
{
return SIZE;
}
}import java.awt.*;
import javax.swing.*;@SuppressWarnings("serial")
public class BallComponent extends JComponent { private Ball ball;
public BallComponent(Ball ball)
{
this.ball = ball;
while (true)
{
update(getGraphics());
ball.move(this);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g)
{
int x = ball.getX();
int y = ball.getY();
int size = ball.getSize();
g.fillOval(x, y, size, size);
}
}import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class BallFrame extends JFrame { private BallComponent component = new BallComponent(new Ball(0, 0));
public BallFrame()
{
add(component);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new BallFrame();
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
import javax.swing.*;public class Ball { private int x;
private int y;
private int vx = 1;
private int vy = 1;
private static final int SIZE = 20;
public Ball(int x, int y)
{
this.x = x;
this.y = y;
}
public void move(JComponent component)
{
x += vx;
y += vy;
Rectangle rect = component.getBounds();
int minX = (int)rect.getMinX();
int minY = (int)rect.getMinY();
int maxX = (int)rect.getMaxX();
int maxY = (int)rect.getMaxY();
if (x <= minX)
{
x = minX;
vx = -vx;
}
if (x + SIZE >= maxX)
{
x = maxX;
vx = -vx;
}
if (y <= minY)
{
y = minY;
vy = -vy;
}
if (y >= maxY)
{
y = maxY;
vy = -vy;
}
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getSize()
{
return SIZE;
}
}import java.awt.*;
import javax.swing.*;@SuppressWarnings("serial")
public class BallComponent extends JComponent { private Ball ball;
public BallComponent(Ball ball)
{
this.ball = ball;
while (true)
{
update(getGraphics());
ball.move(this);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g)
{
int x = ball.getX();
int y = ball.getY();
int size = ball.getSize();
g.fillOval(x, y, size, size);
}
}import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class BallFrame extends JFrame { private BallComponent component = new BallComponent(new Ball(0, 0));
public BallFrame()
{
add(component);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new BallFrame();
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
出错行在这儿了,因为此时g为null调用 g.fillOval出错,解决方案还在找中
main 方法写成这样
public static void main(String[] args)
{
BallFrame frame = new BallFrame();
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.component.play(true);
}
BallComponent 写成这样
import java.awt.*;
import javax.swing.*;@SuppressWarnings("serial")
public class BallComponent extends JComponent { private Ball ball;
public BallComponent(Ball ball)
{
this.ball = ball;
}
public void play(boolean flag){
while (flag)
{
ball.move(this);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g)
{
int x = ball.getX();
int y = ball.getY();
int size = ball.getSize();
this.repaint();
if(g!=null){
g.fillOval(x, y, size, size);
}
}
}这样可以运行了
import javax.swing.*;@SuppressWarnings("serial")
public class BallComponent extends JComponent { private Ball ball;
public BallComponent(Ball ball)
{
this.ball = ball;
}
public void play(boolean flag){
while (flag)
{
this.repaint();
ball.move(this);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g)
{
int x = ball.getX();
int y = ball.getY();
int size = ball.getSize();
if(g!=null){
g.fillOval(x, y, size, size);
}
}
}这样吧,那个改的有点大
g.fillOval(x, y, size, size);
}
判断也可以没有
这样可以解决,但是好像不是你那种思路
不是说repaint方法会调用update方法,而update方法是用来清除画板并调用paint方法的吗?
paint方法中又用到repaint方法,这样不是会变成死循环吗?