import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import javax.swing.*;
public class two extends JFrame{
JButton b;
JPanel pp;
ballpanel Ba;
private final static int W=800;
private final static int H=600;
two()
{
setSize(W,H);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
pp=new JPanel();
Ba=new ballpanel();
pp.setLayout(new FlowLayout());
b=new JButton("开始");
pp.add(b);
b.addActionListener(new action());
add(pp,BorderLayout.SOUTH);
add(Ba,BorderLayout.CENTER);
}
public static void main(String[] args)
{
new two();
}
class action implements ActionListener
{ public void actionPerformed(ActionEvent e) {
p P=new p();
P.start();
}
}
class p extends Thread
{
public void run()
{
while(true)
{
ball Ball=new ball();
Ba.repaint();
try {
Thread.sleep(150);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ballpanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (ball b : balls) {
g2.fill(b.getshape());
System.out.println(b);
}
}
private ArrayList<ball> balls = new ArrayList<ball>();
}
class ball
{
private int x,y;
private int dx=25,dy=20;
private final int WIDTH=30,HEIGHT=30;
public ball()
{
x=dx+x;
y=dy+y;
move();
}
public void move()
{
if(x>=two.W)
{
x=two.W;
dx=-dx;
}
if(y>=two.H)
{
y=two.H;
dy=-dy;
}
if(x<0)
{
x=0;
dx=-dx;
}
if(y<0)
{
y=0;
dy=-dy;
}
}
public Ellipse2D.Double getshape()
{
return new Ellipse2D.Double(x,y,WIDTH,HEIGHT);
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import javax.swing.*;
public class two extends JFrame{
JButton b;
JPanel pp;
ballpanel Ba;
private final static int W=800;
private final static int H=600;
two()
{
setSize(W,H);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
pp=new JPanel();
Ba=new ballpanel();
pp.setLayout(new FlowLayout());
b=new JButton("开始");
pp.add(b);
b.addActionListener(new action());
add(pp,BorderLayout.SOUTH);
add(Ba,BorderLayout.CENTER);
}
public static void main(String[] args)
{
new two();
}
class action implements ActionListener
{ public void actionPerformed(ActionEvent e) {
p P=new p();
P.start();
}
}
class p extends Thread
{
public void run()
{
while(true)
{
ball Ball=new ball();
Ba.repaint();
try {
Thread.sleep(150);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ballpanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (ball b : balls) {
g2.fill(b.getshape());
System.out.println(b);
}
}
private ArrayList<ball> balls = new ArrayList<ball>();
}
class ball
{
private int x,y;
private int dx=25,dy=20;
private final int WIDTH=30,HEIGHT=30;
public ball()
{
x=dx+x;
y=dy+y;
move();
}
public void move()
{
if(x>=two.W)
{
x=two.W;
dx=-dx;
}
if(y>=two.H)
{
y=two.H;
dy=-dy;
}
if(x<0)
{
x=0;
dx=-dx;
}
if(y<0)
{
y=0;
dy=-dy;
}
}
public Ellipse2D.Double getshape()
{
return new Ellipse2D.Double(x,y,WIDTH,HEIGHT);
}
}
}
class p extends Thread { public void run() { while (true) {
ball Ball = new ball();
synchronized (Ba.balls) {
Ba.balls.add(Ball);
}
Ba.repaint();
try {
Thread.sleep(150);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class Two extends JFrame {
JButton b;
JPanel pp;
BallPanel ba;
private final static int w = 800;
private final static int h = 600;
private ArrayList<Ball> balls = new ArrayList<Ball>(); Two() {
setSize(w, h);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
pp = new JPanel();
ba = new BallPanel();
balls.add(new Ball());
pp.setLayout(new FlowLayout());
b = new JButton("开始");
pp.add(b);
b.addActionListener(new action());
add(pp, BorderLayout.SOUTH);
add(ba, BorderLayout.CENTER);
} public static void main(String[] args) {
new Two();
} class action implements ActionListener { public void actionPerformed(ActionEvent e) {
P p = new P();
p.start(); } } class P extends Thread {
public void run() { while (true) {
System.out.println("ppp");
Ball ball = new Ball();
balls.add(ball);
ba.repaint();
try {
Thread.sleep(150);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} class BallPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Ball b : balls) {
g2.fill(b.getshape());
b.move();
}
} } class Ball {
private int x, y;
private int dx = 25, dy = 20;
private final int WIDTH = 30, HEIGHT = 30; public Ball() {
x = dx + x;
y = dy + y;
move();
} public void move() {
x = dx + x;
y = dy + y;
if (x >= Two.w) {
x = Two.w;
dx = -dx;
}
if (y >= Two.h) {
y = Two.h;
dy = -dy;
}
if (x < 0) {
x = 0;
dx = -dx;
}
if (y < 0) {
y = 0;
dy = -dy;
}
} public Ellipse2D.Double getshape() {
return new Ellipse2D.Double(x, y, WIDTH, HEIGHT);
}
}
}
你是说的下面这个,for里的println(b)没有打印吧,上面不是已经说了,你的balls是空的,根本进不了这个for循环,第一次给你的代码,你到底有没有替换了试一试呀?for (ball b : balls) {
g2.fill(b.getshape());
System.out.println(b);
}
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
……
[/Quot
为什么你的代码的小球原来画的不会消失,不是使用了repaint吗
不消失是因为每次都是重绘了n多个球,n是动态增长的,且最新的球位置都是初始点不变的
要消失的话,去掉P线程类中下两句:
Ball ball = new Ball();
balls.add(ball);
去掉了,就什么都没有了..
class BallPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Ball b : balls) {
g2.fill(b.getshape());
b.move();
}
}里的 b.move();为什么放在
class P extends Thread {
public void run() { while (true) {
System.out.println("ppp");
Ball ball = new Ball();
ball.move();
balls.add(ball);
ba.repaint();
try {
Thread.sleep(150);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}里就不行了.
发改后的全文吧:import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class Two extends JFrame {
JButton b;
JPanel pp;
BallPanel ba;
private final static int w = 800;
private final static int h = 600;
private ArrayList<Ball> balls = new ArrayList<Ball>(); Two() {
setSize(w, h);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
pp = new JPanel();
ba = new BallPanel();
balls.add(new Ball()); pp.setLayout(new FlowLayout());
b = new JButton("开始");
pp.add(b);
b.addActionListener(new action());
add(pp, BorderLayout.SOUTH);
add(ba, BorderLayout.CENTER);
} public static void main(String[] args) {
new Two();
} class action implements ActionListener { public void actionPerformed(ActionEvent e) {
P p = new P();
p.start(); } } class P extends Thread {
public void run() { while(true) { System.out.println("ppp");// Ball ball = new Ball();
// ball.move();
// balls.add(ball);
ba.repaint();
try {
Thread.sleep(150);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
} class BallPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for(Ball b : balls) {
g2.fill(b.getshape());
b.move();
System.out.println(b.x + " " + b.y);
}
} } class Ball {
private int x, y;
private int dx = 25, dy = 20;
private final int WIDTH = 30, HEIGHT = 30; public Ball() {
x = dx + x;
y = dy + y;
move();
} public void move() {
x = dx + x;
y = dy + y;
if(x >= Two.w) {
x = Two.w;
dx = -dx;
}
if(y >= Two.h) {
y = Two.h;
dy = -dy;
}
if(x < 0) {
x = 0;
dx = -dx;
}
if(y < 0) {
y = 0;
dy = -dy;
}
} public Ellipse2D.Double getshape() {
return new Ellipse2D.Double(x, y, WIDTH, HEIGHT);
}
}
}移动了move之后不会动了是因为所有的球就只画了两个位置,所有的球可以说都是不移动的
原来的话是排队移动的