public class Ball{ public int x; public int y; public int step; public int width; public int height; public void paint(Graphics g){ g.fillOval(x,y,width,height); } public void move(int x, int y){ this.x+=x; this.y+=y; } }public AnimatePanel extends JPanel{ Ball ball; public boolean isMove; public AnimatePanel(){ ball = new Ball(); ball.x = 20; ball.y = 20; ball.width = 30; ball.height = 30; new Thread(){ public void run(){ try{ Thread.sleep(1000/24); if(isMove){ ball.move(1,0); } this.repaint(); }catch(Exception e){} } }.start(); } @Override public void paintComponent(Graphics g){ ball.paint(g); } }public class TestFrame{ public static void main(String[] args){ JFrame f = new JFrame(); AnimatePanel aPanel = new AnimatePanel(); f.add(aPanel); f.setSize(800,600); f.add(aPanel); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aPanel.requestFocus(); aPanel.addKeyListener(new KeyAdapter(){ public void keyType(KeyEvent e){ if(e.getKeyCode() == KeyEvent.VK_J){ aPanel.isMove = !aPanel.isMove; } } }); } }直接在回复打的,没有测试能不能编译。(*^_^*)
/** Ball.java */ public class Ball{ public int x; public int y; public int step; public int width; public int height; public void paint(Graphics g){ g.fillOval(x,y,width,height); } public void move(int x, int y){ this.x+=x; this.y+=y; } }/** AnimatePanel.java */ public class AnimatePanel extends JPanel{ Ball ball; public boolean isMove; public AnimatePanel(){ ball = new Ball(); ball.x = 20; ball.y = 20; ball.width = 30; ball.height = 30; new Thread(){ public void run(){ try{ Thread.sleep(1000/24); if(isMove){ ball.move(1,0); } this.repaint(); }catch(Exception e){} } }.start(); } @Override public void paintComponent(Graphics g){ ball.paint(g); } }/** TestFrame.java */ public class TestFrame{ public static void main(String[] args){ JFrame f = new JFrame(); AnimatePanel aPanel = new AnimatePanel(); f.add(aPanel); f.setSize(800,600); f.add(aPanel); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aPanel.requestFocus(); aPanel.addKeyListener(new KeyAdapter(){ public void keyType(KeyEvent e){ if(e.getKeyCode() == KeyEvent.VK_J){ aPanel.isMove = !aPanel.isMove; } } }); } }
public class HiwayTest2 extends JFrame implements KeyListener{ private Graphics g = null; private JPanel panel = null; private int runIncrement = 0; private BallThread thread = null;
public class Ball{
public int x;
public int y;
public int step;
public int width;
public int height; public void paint(Graphics g){
g.fillOval(x,y,width,height);
} public void move(int x, int y){
this.x+=x;
this.y+=y;
}
}public AnimatePanel extends JPanel{
Ball ball;
public boolean isMove; public AnimatePanel(){
ball = new Ball();
ball.x = 20;
ball.y = 20;
ball.width = 30;
ball.height = 30; new Thread(){
public void run(){
try{
Thread.sleep(1000/24);
if(isMove){
ball.move(1,0);
}
this.repaint();
}catch(Exception e){}
}
}.start();
} @Override
public void paintComponent(Graphics g){
ball.paint(g);
}
}public class TestFrame{
public static void main(String[] args){
JFrame f = new JFrame();
AnimatePanel aPanel = new AnimatePanel();
f.add(aPanel);
f.setSize(800,600);
f.add(aPanel);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aPanel.requestFocus();
aPanel.addKeyListener(new KeyAdapter(){
public void keyType(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_J){
aPanel.isMove = !aPanel.isMove;
}
}
});
}
}直接在回复打的,没有测试能不能编译。(*^_^*)
/** Ball.java */
public class Ball{
public int x;
public int y;
public int step;
public int width;
public int height; public void paint(Graphics g){
g.fillOval(x,y,width,height);
} public void move(int x, int y){
this.x+=x;
this.y+=y;
}
}/** AnimatePanel.java */
public class AnimatePanel extends JPanel{
Ball ball;
public boolean isMove; public AnimatePanel(){
ball = new Ball();
ball.x = 20;
ball.y = 20;
ball.width = 30;
ball.height = 30; new Thread(){
public void run(){
try{
Thread.sleep(1000/24);
if(isMove){
ball.move(1,0);
}
this.repaint();
}catch(Exception e){}
}
}.start();
} @Override
public void paintComponent(Graphics g){
ball.paint(g);
}
}/** TestFrame.java */
public class TestFrame{
public static void main(String[] args){
JFrame f = new JFrame();
AnimatePanel aPanel = new AnimatePanel();
f.add(aPanel);
f.setSize(800,600);
f.add(aPanel);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aPanel.requestFocus();
aPanel.addKeyListener(new KeyAdapter(){
public void keyType(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_J){
aPanel.isMove = !aPanel.isMove;
}
}
});
}
}
public class HiwayTest2 extends JFrame implements KeyListener{ private Graphics g = null;
private JPanel panel = null;
private int runIncrement = 0;
private BallThread thread = null;
public HiwayTest2(String title){
super(title);
}
public static void main(String...args){
HiwayTest2 ht = new HiwayTest2("Hiway's Demo");
ht.init();
}
private void init(){
panel = new BallPanel();
this.addKeyListener(this);
panel.setBounds(new Rectangle(0,0,600,400));
this.getContentPane().add(panel);
this.setBounds(new Rectangle(100,100,600,400));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g = this.panel.getGraphics();
this.drawBall();
thread = new BallThread();
thread.start();
// this.repaint();
}
private void drawBall(){
g.setColor(Color.red);
g.fillOval(20+this.runIncrement, 50, 50, 50);
g.setColor(Color.green);
g.drawLine(0, 100, 600, 100);
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed");
if(e.getKeyCode() == KeyEvent.VK_J){
runIncrement += 5;
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_J){
repaint();
}
}
@Override
public void keyTyped(KeyEvent e) {
}
class BallPanel extends JPanel{
@Override
public void paint(Graphics g){
System.out.println("繪製");
super.paint(g);
drawBall();
}
}
class BallThread extends Thread{
@Override
public void run(){
while(true){
drawBall();
}
}
}
}
写出来了~ 但是感觉太卡
为什么draw出来的图形会一闪而过呢? 我不得不写一个线程一直来画这个图形。请大侠们帮忙改一下程序,使得资源占用少一点~ thx
在
class BallThread extends Thread{
@Override
public void run(){
while(true){
drawBall();
}
}
}中加一个Thread.sleep()就可以让线程休息一会儿,从而不会一直在绘制