Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: 'animated' filledRectangle gives me a headache -

  1. #1
    EricB is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Question 'animated' filledRectangle gives me a headache -

    Hi i'm trying to master graphics in java and decided to write a small ans simple tennis game.
    I'm drawing on a panel which is the tenniscourt and i have two object which represent the two players
    each player object has a x and y coordinate and I succesfully manipulate the coordinates through a keylistener.
    everything is ok besides this when I move the player up or down it moves the filledRectangle but there is still a filled rectangle where the player started

    Can anyone explain to me why I have two visible rectangles for each player (on the correct position, and on the original position)
    I have included my 'paintComponent - It might explain my problem better than me


    Thanks
    Eric B.

    Java Code:
    public void paintComponent(Graphics g){
    		Graphics2D g2 = (Graphics2D) g;
    		//g2.clearRect(0, 0, 600, 360);
    		//g2.setBackground(Color.GREEN);
    		
    		//draw tenniscourt 
    		g2.setColor(Color.WHITE);
    		g2.fillRect(297, 0, 4, 360);
    		g2.fillRect(179, 0, 2, 360);
    		g2.fillRect(420, 0, 2, 360);
    		g2.fillRect(0, 179, 600, 2);
    		
    		//draw ball
    		g2.setColor(Color.YELLOW);
    		g2.fillOval(20, 100, 10, 10);
    		
    		//draw the players
    		g2.setColor(Color.WHITE);
    		g2.setColor(Color.BLUE);
    		g2.fillRect(p1.x, p1.y, 5, 50);
    		g2.setColor(Color.RED);
    		g2.fillRect(p2.x, p2.y, 5, 50);
    	}

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,834
    Rep Power
    8

    Default Re: 'animated' filledRectangle gives me a headache -

    In JPanel's paintComponent() method, it fills itself with the background color. This draws over any old stuff that might still be there. Since you don't do that, and you don't call you super's paintComponent(), you aren't redrawing your background, so the old stuff remains visible.
    Fubarable likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    EricB is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Question Re: 'animated' filledRectangle gives me a headache -

    calling super.paintComponent() might be a really good idea, my panel does'nt repaint when i do it.
    I can se my keylistener still works (i'm printing to the console) and the players coordinates changes but the graphics isn't updated!
    I have no idea of what to do.

    Thanks to everyone wasting time reading my code
    Eric B

    This is the entire project:
    Java Code:
    /////////////////////////////////////////////////Tennis.java
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.Timer;
    import javax.swing.JFrame;
    
    
    public class Tennis extends JFrame implements KeyListener{
    
    	private boolean p1up = false;
    	private boolean p1down = false;
    	private boolean p2up = false;
    	private boolean p2down = false;
    	
    	TennisCourt bane = new TennisCourt();
    	
    	private int speed = 40;
    	private int pause = 500;
    	private int racketSpeed = 3;
    	
    	public static void main(String[] args) {
    		
    		new Tennis();
    	}
    	public Tennis(){
    		super("Tennis");
    		setBackground(Color.green);
    		
    		add(new TennisCourt());
    		setSize(600,388);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setResizable(false);
    		addKeyListener(this);
    		setVisible(true);
    		add(bane);
    		Timer timer = new Timer(speed, new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				if (p1up == true) bane.p1.moveUp(racketSpeed);
    				if (p1down == true) bane.p1.moveDown(racketSpeed);
    				if (p2up == true) bane.p2.moveUp(racketSpeed);
    				if (p2down == true) bane.p2.moveDown(racketSpeed);
    				System.out.println(""+p1up + p1down + p2up + p2down +":"+ bane.p1.getX() +":"+ bane.p1.getY());
    				//System.out.println("!");
    				repaint();
    				
    			}
    		});
    		timer.start();
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		if (e.getKeyCode() == 65){
    			p1up = true;
    			p1down = false;
    		}
    		if (e.getKeyCode() == 90){
    			p1up = false;
    			p1down = true;
    		}
    		if (e.getKeyCode() == 38){
    			p2up = true;
    			p2down = false;
    		}
    		if (e.getKeyCode() == 40){
    			p2up = false;
    			p2down = true;
    		}
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		if (e.getKeyCode() == 65 || e.getKeyCode() == 90){
    			p1up = false;
    			p1down = false;
    		}
    		if (e.getKeyCode() == 38 || e.getKeyCode() == 40){
    			p2up = false;
    			p2down = false;
    		}
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		
    	}
    }
    ////////////////////////////////////////////////////Tenniscourt.java
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class TennisCourt extends JPanel {
    	
    	JLabel player1points = new JLabel("0");
    	JLabel delimiter = new JLabel(" - ");
    	JLabel player2points = new JLabel("0");
    	
    	public Racket p1 = new Racket(0,50);
    	public Racket p2 = new Racket(590,260);
    	
    	public TennisCourt(){
    		add(player1points);
    		add(delimiter);
    		add(player2points);
    		repaint();
    	}
    	
    	
    	public void paintComponent(Graphics g){
    		//super.paintComponent(g);
    		
    		Graphics2D g2 = (Graphics2D) g;
    		g2.clearRect(0, 0, 600, 360);
    		g2.setBackground(Color.GREEN);
    		
    		//draw tenniscourt 
    		g2.setColor(Color.WHITE);
    		g2.fillRect(297, 0, 4, 360);
    		g2.fillRect(179, 0, 2, 360);
    		g2.fillRect(420, 0, 2, 360);
    		g2.fillRect(0, 179, 600, 2);
    		
    		//draw ball
    		g2.setColor(Color.YELLOW);
    		g2.fillOval(20, 100, 10, 10);
    		
    		//draw the players
    		g2.setColor(Color.WHITE);
    		g2.setColor(Color.BLUE);
    		g2.fillRect(p1.x, p1.y, 5, 50);
    		g2.setColor(Color.RED);
    		g2.fillRect(p2.x, p2.y, 5, 50);
    	}
    }
    ////////////////////////////////////////////////////////////////Racket.java
    
    import java.awt.Point;
    
    public class Racket {
    	public int x;
    	public int y;
    	
    	public Racket(int x, int y){
    		this.x = x;
    		this.y = y;
    	}
    	
    	public void moveUp(int i){
    		y -= i;
    	}
    	public void moveDown(int i){
    		y += i;
    	}
    	public int getX(){
    		return x;
    	}
    	public int getY(){
    		return y;
    	}
    }
    Last edited by DarrylBurke; 11-05-2011 at 09:11 PM. Reason: Added closing code tag

Similar Threads

  1. reset an animated gif?
    By totj in forum New To Java
    Replies: 0
    Last Post: 06-30-2011, 05:56 AM
  2. Animation with Animated GIF
    By JavaBean in forum Java 2D
    Replies: 3
    Last Post: 06-04-2011, 04:26 PM
  3. NullPointerException Headache
    By denno in forum New To Java
    Replies: 17
    Last Post: 04-05-2011, 08:13 AM
  4. Modulo Headache
    By House in forum New To Java
    Replies: 17
    Last Post: 09-01-2010, 03:29 AM
  5. Event headache
    By kammenos in forum Java Applets
    Replies: 2
    Last Post: 12-12-2008, 11:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •