用Java绘制椭圆

时间:2012-04-30 22:25:32

标签: java swing ellipse

我有一个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);
}

3 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分: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); 
        }
    }
}

答案 2 :(得分:0)

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

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

所以背景返回原始颜色,椭圆的痕迹将被删除