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.
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 :frusty:
Thankyou
Full code:
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 )
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");
}
}
}
}
Re: Thread ( Game ) - help with painting object
What does "not being re painted properly" mean?
Re: Thread ( Game ) - help with painting object
Quote:
Originally Posted by
Tolls
What does "not being re painted properly" mean?
I have changed it , i hope it makes more sense now
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.