5 Replies - 361 Views - Last Post: 15 August 2013 - 07:33 PM Rate Topic: -----

#1 jpat27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-January 13

Paint() elements not going away when I call repaint();

Posted 15 August 2013 - 05:16 PM

I am trying to make a simple program where a square moves around the JFrame. It works, but the old squares don't go away when I call repaint().

import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.JFrame;

class Movement extends JFrame implements KeyListener{
    int setx = 250;
    int sety = 250;
    
    public Movement(){
    setSize(500, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(false);
    addKeyListener(this);
    
    }
    
    public void paint(Graphics g){
    g.drawRect(setx, sety, 20, 20);
    }
    public void update(Graphics g) {
    paint(g);
    }    
    
    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        
        if (sety > 40 && key == KeyEvent.VK_W){
            sety = sety - 10;
           repaint();
        }
        if (sety < 470 && key == KeyEvent.VK_S){
            sety = sety + 10;
            repaint();
        }
        if (setx < 470 && key == KeyEvent.VK_D){
            setx = setx + 10;
            repaint();
        }
        if (setx > 10 && key == KeyEvent.VK_A){
            setx = setx - 10;
            repaint();
        }
       
    }
    public void keyTyped(KeyEvent e){
        
    }
    public void keyReleased(KeyEvent e) {
            
    }
    
    public static void main(String args[]){
        Movement move = new Movement();
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Paint() elements not going away when I call repaint();

#2 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: Paint() elements not going away when I call repaint();

Posted 15 August 2013 - 05:37 PM

The easiest way is to just paint over the old square with the background color before you paint the new square.

You should also overide PaintComponent() instead of Paint(). Paint is from AWT.
Was This Post Helpful? -1
  • +
  • -

#3 jpat27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-January 13

Re: Paint() elements not going away when I call repaint();

Posted 15 August 2013 - 05:46 PM

How would I implement repainting the backgound? And when I try to use paintComponent, the square disappears.
Was This Post Helpful? 0
  • +
  • -

#4 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: Paint() elements not going away when I call repaint();

Posted 15 August 2013 - 06:44 PM

Try using setBackground along with getBackground.

I'm not to sure on PaintComponent. I would just use Paint for now then.

This post has been edited by salazar: 15 August 2013 - 06:51 PM

Was This Post Helpful? -1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Paint() elements not going away when I call repaint();

Posted 15 August 2013 - 07:14 PM

All replies are male cattle excrement
Just call super.paint() as the first statement of your paint() method (which you should always do anyhow) and it will redraw your background the documented way :)

View Postsalazar, on 15 August 2013 - 09:44 PM, said:

Try using setBackground along with getBackground.

I'm not to sure on PaintComponent. I would just use Paint for now then.

No :)

View Postsalazar, on 15 August 2013 - 08:37 PM, said:

The easiest way is to just paint over the old square with the background color before you paint the new square.

You should also overide PaintComponent() instead of Paint(). Paint is from AWT.

No again :)
Was This Post Helpful? 1
  • +
  • -

#6 jpat27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-January 13

Re: Paint() elements not going away when I call repaint();

Posted 15 August 2013 - 07:33 PM

Thanks so much! Everything works great!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1