I m a newbie to java swing . And when trying with graphics i got stuck with this. I could'nt find a proper solution in web . So i thought of posting here .
so now lets come to my problem . First i will explain what i m trying to do . And then i will explain about my problem.
I m trying to make two balls move in different directions simultaneously in a JFrame . (Basically i thought of doing like chain Reaction game in which when you click on a filled box, the balls will move simultaneously in different directions ) .
Here i m creating two (as of now) JPanels for two balls which i m trying to move on a JFrame SIMULTANEOUSLY .
here is the code which i tried out ,
public class chainGraphics extends JPanel implements Runnable{
int oldX,oldY,newX,newY;
int changeX,changeY;
Container myPane;
public chainGraphics(int oldX,int oldY,int newX,int newY,Container myPane) {
// TODO Auto-generated constructor stub
this.myPane=myPane;
this.oldX=oldX;
this.oldY=oldY;
this.newX=newX;
this.newY=newY;
myPane.add(this);
}
public void paintComponent(Graphics g) {
//super.paintComponent(g);
System.out.println("hj");
g.drawOval(changeX,changeY, 40, 40);
}
@Override
public void run() {
System.out.println("hii");
changeX =oldX;
changeY = oldY;
if((newY-oldY)==0){
if(oldX<newX){
for(int i=oldX;i<newX;i++){
System.out.println("hii123");
changeX = i;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
else {
for(int i=oldX;i>newX;i--){
changeX=i;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
}
if((newX-oldX)==0){
if(oldY<newY){
for(int i=oldY;i<newY;i++){
changeY=i;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
else {
for(int i=oldY;i>newY;i--){
changeY=i;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
}
}
public static void main(String[] args) {
JFrame gui = new JFrame();
gui.setTitle("Chain Reaction ;-) ");
gui.setSize(650,650);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
Container Pane = gui.getContentPane();
chainGraphics g = new chainGraphics(100,200,300,200,Pane);
chainGraphics g1 = new chainGraphics(200,100,200,300,Pane);
Thread t1 = new Thread(g);
t1.start();
Thread t2 = new Thread(g1);
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Here i couldnt see two balls moving simultaneously . There is a problem i guess. i could able to see ball moving along y direction only . (but i want both ball to move simutaneously, since i create two threads)
And i also noticed if i create g1 object first and then g object , then the ball moves along x direction only .
I think the Jframe allows to work on 1 JPanel only at a time . (And i am adding the JPanel to Frame thorough Constructor ) . so which ever JPanel is added lastly to frame ,it allows to work on it . is it so ? IF yes what should i do now .My requirement is i want to move balls simultaneously in different Directions. Thanks.