Results 1 to 4 of 4
- 01-25-2013, 02:17 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 29
- Rep Power
- 0
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.
I'm probably forgetting something really obvious but I just cant figure it outJava Code:public void move(){ y+= ySpeed; if(y >= 235){ System.out.println("out of bounds"); isVisible = false; }
Thankyou
Full code:
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; 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(); } }
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.
- 01-25-2013, 02:32 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 01-25-2013, 03:24 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 29
- Rep Power
- 0
- 01-25-2013, 03:52 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
Similar Threads
-
Referencing object from another Thread
By Floiancu in forum Threads and SynchronizationReplies: 6Last Post: 05-15-2011, 01:20 PM -
First Java game: Why is screen painting so slow?
By CoderMan in forum Java GamingReplies: 5Last Post: 05-20-2010, 02:55 AM -
Global Thread - Game Development
By M77 in forum Suggestions & FeedbackReplies: 1Last Post: 06-29-2008, 04:47 AM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM -
how to access to an object from a thread
By tamayo in forum New To JavaReplies: 1Last Post: 07-24-2007, 04:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks