简体   繁体   中英

how can i clear my rectangle after drawing it to the screen?

I realize that a major component missing from my game is a sense of time, ticks, or FPS. I am planning on implementing this soon, but wanted to get some feedback on how to set up my "bullets" instead. I want the user to press the space bar and have a bullet fired across the screen. Now I have gotten close without a sense of FPS; however, it just draws as one giant line, meaning the bullet "trail" never clears. I am wondering why, when I call repaint, the rectangle can move around the screen via my keyboard, but whenever I have something set automatically to move then it just leaves a "trail", even though I am calling repaint(); in each method.

Also, how could i create my bullet from another class?

public class drawingComponent extends JComponent implements KeyListener {

    public Rectangle hello = new Rectangle(300, 100, 50, 50);
    Rectangle bullet = new Rectangle(310,75, 10,10);
    boolean goingon = false;

    public drawingComponent(){
        addKeyListener(this);
    }

    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(new Color(255,25,0));
        g2.setFont(new Font("monospace", Font.BOLD+Font.ITALIC, 30));
        g2.drawString("nothing yet",300,320);
        g2.fill(hello);
        setFocusable(true);
        requestFocus();
        g2.setColor(new Color(0,25,0));
        if (goingon == true){
            while (bullet.y < 1000){
                bullet.y=bullet.y+10;
                g2.fill(bullet);
            }
            bullet.y=300;
        }   
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_W){
            hello.y=hello.y-1;
            hello.setLocation(hello.x,hello.y);
            repaint(); 
            System.out.println(hello.y);
        }
        if(e.getKeyCode() == KeyEvent.VK_S){
            hello.y=hello.y+1;
            hello.setLocation(hello.x,hello.y);
            repaint();  
        }
        if(e.getKeyCode() == KeyEvent.VK_A){
            hello.x=hello.x-1;
            hello.setLocation(hello.x,hello.y);
            repaint();      
        }
        if(e.getKeyCode() == KeyEvent.VK_D){
            hello.x=hello.x+1;
            hello.setLocation(hello.x,hello.y);
            repaint();

        }
        if(e.getKeyCode() == KeyEvent.VK_SPACE){
            goingon = true;
            repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_SPACE){
            goingon = false;
            repaint();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
    }
}

Start by repairing the paint chain...

@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(new Color(255,25,0));
    g2.setFont(new Font("monospace", Font.BOLD+Font.ITALIC, 30));
    g2.drawString("nothing yet",300,320);
    g2.fill(hello);

Next, stop changing the state of the component from within the paint method...

    //setFocusable(true);
    //requestFocus();
    g2.setColor(new Color(0,25,0));
    if (goingon == true){
    while (bullet.y < 1000){
        bullet.y=bullet.y+10;
        g2.fill(bullet);
    }
    bullet.y=300;



}

Painting should simply paint the current state of the component, it should never attempt to modify the state of the component, doing so could trigger another paint request which will put your code into an infinite loop of painting and consume your CPU cycles.

Swing uses a passive rendering algorithm, this means, painting is carried out only when the repaint manager thinks it needs to be done, meaning that painting can be done at random and mostly without your intervention...

Based on the fact that you are trying to force focus to the component, I assume you are trying to overcome issues related to KeyListener . Instead, you should use the key bindings API. Take a look at How to Use Key Bindings for more details

Take a look at Painting in AWT and Swing and Performing Custom Painting for more details.

Don't forget to call super.paintComponent() in overridden paintComponent() method that clears the previews view.

Read more...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM