0

我有一个 java 程序,它在屏幕上显示一个椭圆并通过使用箭头键改变它的方向。我经常使用 while 循环在椭圆上调用 repaint()。

椭圆会移动,但问题是它会在其路径上留下一串椭圆。如何制作它以删除旧椭圆然后重新绘制新椭圆?

代码:

public void run(){
        while (animator != null) 
        {
            setBackground(Color.GREEN);
            repaint();
                            // The direction works and the rest works fine.
            player1.move(player1, player1.direction);
            try  
            {  
                Thread.sleep(100);  
            }  
            catch (InterruptedException e)  
            {  
                break;  
            }  
        }
}

// The paintComponent of my player1 is fine.
public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        player1.paintComponent(g2, player1);
}
4

3 回答 3

1

问题可能是您的paintComponent(Graphics)覆盖方法没有调用super.paintComponent(g),它清除了画布,除其他外:

// Bad:
@Override
public void paintComponent(Graphics g) {
    // paint the ellipse, etc.
}

// Good:
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // paint the ellipse, etc.
}
于 2012-04-30T22:36:15.310 回答
1

这是一些基本的代码(不使用连续的while循环):

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;


@SuppressWarnings("serial")
public class Ellipses extends JFrame {

    public static void main(String[] args){
        //Ensures application is not run on the main thread 
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Ellipses myEllipses = new Ellipses();
                myEllipses.init();
            }
        });
    }

    public Ellipses(){
        //Set up the frame
        this.setTitle("Ellipses Example");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(400, 400);
    }

    private Ellipse ellipse1;

    public void init(){
        Container contentPane = this.getContentPane();

        //Create a new ellipse and add to the content pane
        ellipse1 = new Ellipse();
        contentPane.add(ellipse1);


        //Add the keyListener to the contentPane
        contentPane.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {}
            public void keyReleased(KeyEvent e) {}
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_UP){
                    ellipse1.decreaseY();
                }
                if(e.getKeyCode() == KeyEvent.VK_DOWN){
                    ellipse1.increaseY();
                }
                if(e.getKeyCode() == KeyEvent.VK_LEFT){
                    ellipse1.decreaseX();
                }
                if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                    ellipse1.increaseX();
                }

                //Repaint the ellipse

                ellipse1.repaint();
            }
        });

        //Request the focus so key presses can be detected
        contentPane.setFocusable(true);
        contentPane.requestFocus();
    }



    //Create an ellipse which can be drawn to the screen
    public class Ellipse extends JComponent{

        private int x , y; //Coordinates of the oval

        public Ellipse(){
            setCoordinates(100, 100);
        }

        public void setCoordinates(int x, int y){
            this.x = x;
            this.y = y;
        }

        public void increaseY(){
            y+=10;
        }

        public void increaseX(){
            x+=10;
        }

        public void decreaseY(){
            y-=10;
        }

        public void decreaseX(){
            x-=10;
        }

        public void paint(Graphics g){
            //Ensures previous paint is cleared
            super.paintComponents(g);
            g.setColor(Color.RED);
            g.fillOval(x, y, 100, 100); 
        }
    }
}
于 2012-04-30T23:05:10.393 回答
0

您应该在游戏循环中绘制椭圆之前绘制背景颜色 :) 您也可以调用 repaint() ,它实际上调用 update() 来完成清除屏幕和调用paint() 的脏活。当您使用重绘时,更新方法继承

public void update(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, width, height);
g.setColor(getForeground());
paint(g)
}

所以背景恢复到原来的颜色,椭圆的痕迹将被删除

于 2012-04-30T22:30:13.100 回答