Swing repaint() problem
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
public class MovingBall extends JFrame {
private int ballPositionX;
private int ballPositionY;
private JButton controlButton;
private MyDrawPanel drawArea;
public MovingBall() {
ballPositionX = 70;
ballPositionY = 70;
controlButton = new JButton("play");
controlButton.addActionListener(new ButtonHandler());
BorderLayout buttonLayout = new BorderLayout();
this.getContentPane().add(controlButton, buttonLayout.SOUTH);
drawArea = new MyDrawPanel();
BorderLayout drawAreaLayout = new BorderLayout();
this.getContentPane().add(drawArea, drawAreaLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
}
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
go();
}
}
private class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
//super.paintComponent(g);
//g.setColor(Color.white);
//g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.green);
g.fillOval(ballPositionX, ballPositionY, 20, 20);
}
}
private void go() {
while (ballPositionX < 400) {
ballPositionX = ballPositionX + 5;
ballPositionY = ballPositionX + 5;
drawArea.paintImmediately(0, 0, this.getWidth(), this.getHeight());
try{Thread.sleep(20);}
catch(Exception ex){}
}
}
public static void main(String args[]) {
MovingBall animation1 = new MovingBall();
}
}
repaint(); thread; action; they will become messy when they come together.
one solution is to use paintImmediately() instead of repaint().
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
public class MovingBall extends JFrame {
private int ballPositionX;
private int ballPositionY;
private JButton controlButton;
private MyDrawPanel drawArea;
public MovingBall() {
ballPositionX = 70;
ballPositionY = 70;
controlButton = new JButton("play");
controlButton.addActionListener(new ButtonHandler());
BorderLayout buttonLayout = new BorderLayout();
this.getContentPane().add(controlButton, buttonLayout.SOUTH);
drawArea = new MyDrawPanel();
BorderLayout drawAreaLayout = new BorderLayout();
this.getContentPane().add(drawArea, drawAreaLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
}
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
go();
}
}
private class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
//super.paintComponent(g);
//g.setColor(Color.white);
//g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.green);
g.fillOval(ballPositionX, ballPositionY, 20, 20);
}
}
private void go() {
while (ballPositionX < 400) {
ballPositionX = ballPositionX + 5;
ballPositionY = ballPositionX + 5;
drawArea.paintImmediately(0, 0, this.getWidth(), this.getHeight());
try{Thread.sleep(20);}
catch(Exception ex){}
}
}
public static void main(String args[]) {
MovingBall animation1 = new MovingBall();
}
}
repaint(); thread; action; they will become messy when they come together.
one solution is to use paintImmediately() instead of repaint().
评论
发表评论