Results 1 to 4 of 4
  1. #1
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Thread ( Game ) - help with painting object

    Could someone tell me why my ball is not being painted at a different location after I change the Y variable.

    I think it might be something to do with painting the objects but i'm not sure.


    Java Code:
    public void move(){
    		y+= ySpeed;
    		
    		if(y >= 235){
    			System.out.println("out of bounds");
    			isVisible = false;
    		}
    I'm probably forgetting something really obvious but I just cant figure it out

    Thankyou

    Full code:

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.util.ArrayList;
    import javax.swing.JFrame;
    
    public class JavaGame extends JFrame implements Runnable{
    
    	int x, y, xDirection, yDirection;
    	private Image dbImage;
    	private Graphics dbg;
    	private boolean black;
    	private boolean red;
    	private boolean green;
    	private boolean cyan;
    	private boolean yellow;
    	private Ball newball  = new Ball();
    	
    	public JavaGame(){
    
    		addKeyListener(new AL());
    		setTitle("Java Game");
    		setSize(250, 250);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		setBackground(Color.BLUE);
    		setVisible(true);
    		black = true;
    		x = 150;
    		y = 150;
    	}
    
    	public void run(){
    		try{
    			while(true){
    				move();
    				
    				Thread.sleep(10);
    			}
    
    
    		}catch(Exception e){
    			System.out.println("error");
    		}
    	}
    
    	public void move(){
    		x += xDirection;
    		y += yDirection;
    
    		if(x <= 0){
    			x = 0;
    		}
    		if(x >= 235){
    			x = 235;
    		}
    		if(y <= 0){
    			y =30;
    		}
    		if(y >= 235){
    			y = 235;
    		}
    	}
    	public void setXDirection(int xdir){
    		xDirection = xdir;
    	}
    
    	public void setYDirection(int ydir){
    		yDirection = ydir;
    	}
    
    
    	public class AL extends KeyAdapter {
    
    		public void keyPressed(KeyEvent e){
    			int keycode = e.getKeyCode();
    
    			if(keycode == e.VK_LEFT){
    				setXDirection(-1);
    				red = true;
    			}
    			if(keycode == e.VK_RIGHT){
    				setXDirection(+1);
    				green = true;
    			}
    			if(keycode == e.VK_UP){
    				setYDirection(-1);
    				yellow = true;
    			}
    			if(keycode == e.VK_DOWN){
    				setYDirection(+1);
    				cyan = true;
    			}
    		}
    		public void keyReleased(KeyEvent e){
    			int keycode = e.getKeyCode();
    
    			if(keycode == e.VK_LEFT){
    				setXDirection(0);
    				red = false;
    			}
    			if(keycode == e.VK_RIGHT){
    				setXDirection(0);
    				green = false;
    			}
    			if(keycode == e.VK_UP){
    				setYDirection(0);
    				yellow = false;
    			}
    			if(keycode == e.VK_DOWN){
    				setYDirection(0);
    				cyan = false;
    			}
    		}
    	}
    
    	public void paint(Graphics g){
    
    
    
    		dbImage = createImage(getWidth(),getHeight());
    		dbg = dbImage.getGraphics();
    		paintComponent(dbg);
    		g.drawImage(dbImage,0,0,this);
    	}
    
    	public void paintComponent(Graphics g){
    		newball.draw(g);
    		
    		g.setColor(Color.black);
    		g.fillOval(x, y, 15, 15);
    		
    		if(green){
    			g.setColor(Color.green);
    			g.fillOval(x, y, 15, 15);
    			repaint();
    		}
    		else if(red){
    			g.setColor(Color.red);
    			g.fillOval(x, y, 15, 15);
    			repaint();
    		}
    		else if(cyan){
    			g.setColor(Color.cyan);
    			g.fillOval(x, y, 15, 15);
    			repaint();
    		}
    
    		else if(yellow){
    			g.setColor(Color.yellow);
    			g.fillOval(x, y, 15, 15);
    			repaint();
    		}
    		repaint();
    	}
    
    
    
    
    
    	public static void main(String[] args){
    
    		JavaGame jg = new JavaGame();
    		
    		Thread t1 = new Thread(jg);
    		Thread t2 = new Thread(new Ball());
    		t1.start();
    		t2.start();
    
    
    	}
    
    }
    Ball Class ( I am trying to make the GREEN ball appear at the top of the screen and move to the bottom )

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public class Ball implements Runnable {
    	
    	private int x,y, ySpeed;
    	private boolean ballMoving, isVisible;
    	
    	public Ball(){
    	x = (int) (Math.random()*250);
    	y = 50;
    	ySpeed = 5;
    	ballMoving = true;	
    	isVisible = true;
    	}
    	
    	public int getX(){
    		return x;
    	}
    	
    	public int getY(){
    		return y;
    		
    	}
    	public void setX(int xCor){
    		this.x = xCor;
    		
    	}
    	public void setY(int yCor){
    		this.y = yCor;
    		
    	}
    	public void draw(Graphics g){
    		
    	g.setColor(Color.green);
    	g.fillOval(x,y,10,10);
    		
    	}
    	
    	public void move(){
    		y+= ySpeed;
    		
    		if(y >= 235){
    			System.out.println("out of bounds");
    			isVisible = false;
    		}
    		
    	
    	}
    	
    	public void run(){
    		while(ballMoving){
    			try{
    				
    				move();
    				b.repaint();
    				Thread.sleep(50);
    				System.out.println("running");
    				
    			}catch(Exception ex){
    				System.out.println("error");
    			}
    			}
    		
    	}
    	
    	
    }
    Last edited by Dex; 01-25-2013 at 03:32 PM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Thread ( Game ) - help with painting object

    What does "not being re painted properly" mean?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: Thread ( Game ) - help with painting object

    Quote Originally Posted by Tolls View Post
    What does "not being re painted properly" mean?
    I have changed it , i hope it makes more sense now

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Thread ( Game ) - help with painting object

    First don't override paint().
    You only need to override paintComponent().

    Second your new Ball thread is not associated at all with your main frame.
    At least not as far as I can see.

    Stick a load of println's in there to debug it and see what the flow is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Referencing object from another Thread
    By Floiancu in forum Threads and Synchronization
    Replies: 6
    Last Post: 05-15-2011, 01:20 PM
  2. First Java game: Why is screen painting so slow?
    By CoderMan in forum Java Gaming
    Replies: 5
    Last Post: 05-20-2010, 02:55 AM
  3. Global Thread - Game Development
    By M77 in forum Suggestions & Feedback
    Replies: 1
    Last Post: 06-29-2008, 04:47 AM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 AM
  5. how to access to an object from a thread
    By tamayo in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 04:24 AM

Posting Permissions

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