Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trying to fix repaint method

  1. #1
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Angry Trying to fix repaint method

    So I'm trying to move a rectangle... but it leaves footprints...repaint(); doesn't seem to work in actionPerformed

    Thanks!


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
     
    public class myFrame extends JPanel implements KeyListener, ActionListener {
     
    	private static final long serialVersionUID = 1L;
     
    	Timer t = new Timer(5, this);
     
    	double rectX = 15;
    	double rectY = 15;
    	int rectWidth = 80;
    	int rectHeight = 20;
    	double rectVeloX = 0.0;
    	double rectVeloY = 0.0;
    	double ballX = 60;
    	double ballY = 60;
    	int ballWH = 20;
     
    	public myFrame() {
    		t.start();
    		addKeyListener(this);
    		setFocusable(true);
    		setFocusTraversalKeysEnabled(false);		
    	}
     
    	public void paintComponent (Graphics draw2D){		
     
    		 //PAD		
    		super.paintComponents(draw2D);
    		Graphics2D draw = (Graphics2D) draw2D;
    		draw.setColor(Color.red);
    		draw.fill(new Rectangle2D.Double(rectX,rectY, rectWidth, rectHeight));
    		draw.getClipBounds();  
    		//BALL		
     
    		/*draw.setColor(Color.blue);
    		draw.fill(new Ellipse2D.Double(ballX,ballY,ballWH,ballWH));*/
     
    	}
     
    	public void actionPerformed (ActionEvent e) {
    	        repaint();
    		rectX += rectVeloX;
    		rectY += rectVeloY;		
    	}
     
     
     
     
    	public void keyPressed(KeyEvent e) {
    		int key = e.getKeyCode();
     
    		   if (key == KeyEvent.VK_RIGHT ) {
     
    			   rectVeloX = 1.5;
    			   rectVeloY = 0;
     
     
    	    }  if (key == KeyEvent.VK_LEFT ) {
     
    			   rectVeloX = -1.5;
    			   rectVeloY = 0;
     
     
    	            //Left arrow key code
    	    }
     
    	}
     
    	public void keyReleased(KeyEvent e) {
    		int key = e.getKeyCode();
     
    			if (key == KeyEvent.VK_RIGHT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
     
    			}
     
    			if (key == KeyEvent.VK_LEFT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
    			}
    	}
     
    	public void keyTyped(KeyEvent e) {}		
    }
     
     
    import javax.swing.JFrame;
     
    public class arkanoidGame {
     
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		myFrame s = new myFrame();
    		frame.add(s);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(800,600);
     
    	}	
    }

  2. The Following User Says Thank You to Lorack For This Useful Post:

    javapenguin (June 23rd, 2012)


  3. #2
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Trying to fix repaint method

    I fixed it by changing
    public void paint(Graphics draw2D){
    super.paint(draw2D);

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to fix repaint method

     
    import javax.swing.JFrame;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
     
    public class myFrame extends JPanel implements KeyListener, ActionListener {
     
    	private static final long serialVersionUID = 1L;
     
    	Timer t = new Timer(5, this);
     
    	double rectX = 15;
    	double rectY = 15;
    	int rectWidth = 80;
    	int rectHeight = 20;
    	double rectVeloX = 0.0;
    	double rectVeloY = 0.0;
    	double ballX = 60;
    	double ballY = 60;
    	int ballWH = 20;
     
    	public myFrame() {
    		t.start();
    		addKeyListener(this);
    		setFocusable(true);
    		setFocusTraversalKeysEnabled(false);		
    	}
     
    	public void paint (Graphics draw2D){		
     
    		 //PAD		
    		super.paint(draw2D);
    		Graphics2D draw = (Graphics2D) draw2D;
    		draw.setColor(Color.red);
    		draw.fill(new Rectangle2D.Double(rectX,rectY, rectWidth, rectHeight));
    		draw.getClipBounds();  
    		//BALL		
     
    		/*draw.setColor(Color.blue);
    		draw.fill(new Ellipse2D.Double(ballX,ballY,ballWH,ballWH));*/
     
    	}
     
    	public void actionPerformed (ActionEvent e) {
     
    		rectX += rectVeloX;
    		rectY += rectVeloY;		
    		  repaint();
    	}
     
     
     
     
    	public void keyPressed(KeyEvent e) {
    		int key = e.getKeyCode();
     
    		   if (key == KeyEvent.VK_RIGHT ) {
     
    			   rectVeloX = 1.5;
    			   rectVeloY = 0;
     
     
    	    }  if (key == KeyEvent.VK_LEFT ) {
     
    			   rectVeloX = -1.5;
    			   rectVeloY = 0;
     
     
    	            //Left arrow key code
    	    }
     
    	}
     
    	public void keyReleased(KeyEvent e) {
    		int key = e.getKeyCode();
     
    			if (key == KeyEvent.VK_RIGHT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
     
    			}
     
    			if (key == KeyEvent.VK_LEFT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
    			}
    	}
     
    	public void keyTyped(KeyEvent e) {}		
     
     
     
     
     
     
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		myFrame s = new myFrame();
    		frame.add(s);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(800,600);
     }
     
    }


    Also, paintComponents() is a method that's not paintComponent(). It does weird things when you call it. Not sure what it's for actually. However, such a method exists, though I think you meant super.paintComponent();

    If you want it to draw left and right, use paintComponent(), if you want it to move left or right, use paint(). And no, it doesn't really matter the order for it if using paint().

    However, paintComponents() does weird things.

    That code was interesting. Now I can possibly use that same technique to move around images. Thanks.

    However, when I applied that with VK_DOWN, it just kept on moving down.

    if (key == KeyEvent.VK_DOWN)
    {

    rectVeloX = 0;
    rectVeloY = .5;


    }

    Also, it will stop moving down if you hit the left or right arrow.
    Last edited by javapenguin; June 23rd, 2012 at 11:08 PM.

  5. The Following User Says Thank You to javapenguin For This Useful Post:

    Lorack (June 24th, 2012)

  6. #4
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Trying to fix repaint method

    You need to add if( key == KeyEvent.VK_DOWN) in keyReleased or else it won't stop

    public void keyReleased(KeyEvent e) {
    		int key = e.getKeyCode();
     
    			if (key == KeyEvent.VK_RIGHT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
     
    			}
     
    			if (key == KeyEvent.VK_LEFT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
    			}    
                            if( key == KeyEvent.VK_DOWN) {
                                    rectVeloX = 0;
                                    rectVeloY = 0;
    	}

  7. The Following User Says Thank You to Lorack For This Useful Post:

    javapenguin (June 24th, 2012)

  8. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to fix repaint method



    Should have realized that. (Probably would have if I wasn't sleep deprived.)

Similar Threads

  1. Help with repaint
    By AraHabs in forum AWT / Java Swing
    Replies: 5
    Last Post: November 5th, 2011, 04:40 PM
  2. How do I use the repaint method to "undraw"? (I am a noob)
    By racecar333 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 31st, 2011, 06:40 PM
  3. Repaint method in Thread not moving String
    By javapenguin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2011, 10:33 PM
  4. Repaint,
    By Time in forum AWT / Java Swing
    Replies: 3
    Last Post: May 21st, 2010, 11:23 PM
  5. Repaint doesn't repaint?
    By PotataChipz in forum AWT / Java Swing
    Replies: 6
    Last Post: January 18th, 2010, 09:56 PM

Tags for this Thread