Hi, i've started making a simple breakout game with java swing. This is just for fun and to learn more about java and swing. I got a frame and a panel and a mouse listener. I draw a paddle on the screen and it moves as the mouse moves. I also print out on motion the x value of the mouse.

I want a timer to move the ball. My timer is in the frame class and it works. right now its called every second and increments a variable z. with system.out.println, i can see z incrementing.

The problem is i'm tring to print out z in the drawing function on the screen ( same place x is printing) and it is allways 0. z is a variable on the frame object and panel object. in the frame class where the timer is i do panel.setz(z);.

i think i have two copies of my panel. breakoutpanel panel; is a class variable in the frame. in the constructor i do

panel=new breakoutpanel();
frame.add(panel);

the constructor is called twice. when i comment out frame.add(panel) its called once. so i guess my actual panel is a copy of my panel variable, and once i do frame.add(panel); i need to talk to that panel and my code with panel.setz(z); is communication with the panel i started with and coppied. How would i do this?

Mike

Recommended Answers

All 5 Replies

The code fragment that you posted would only result in a single instance of the panel and should behave exactly as you want it to. The part I don't follow is that you say the panel is a class member of the frame, but you are making a frame.add() call. I thought this code was executing in the frame object, so what is frame ? If you can post the code it would clarify a bit.

The code fragment that you posted would only result in a single instance of the panel and should behave exactly as you want it to. The part I don't follow is that you say the panel is a class member of the frame, but you are making a frame.add() call. I thought this code was executing in the frame object, so what is frame ? If you can post the code it would clarify a bit.

You are right there could be some issue were the reason i'm creating two panels is i have two frames. I'll have to look at the code again and post it tonight when i get home. I havent done swing since i learend it in school several years ago. Been coding in c++. so i'll get the code tonight.

Mike

the code of my panel class is:

class breakoutframe extends JFrame
{

    public breakoutframe()
    {   z=5;
        setTitle("breakout");
        panel = new breakoutpanel();
        add(panel);
    timer = new Timer (  ) ;
    timer.scheduleAtFixedRate( new ToDoTask (  ) , 1000 ,1000) ;



    }

    public static final int DEFAULT_WIDTH = 400;
    public static final int DEFAULT_HEIGHT = 400;
    breakoutpanel panel;
    Timer timer;
    int z;

class ToDoTask extends TimerTask  {
    public void run (  )   {

    z=z+10;
    breakoutpanel panel=getComponent(1);
    panel.setz(z);
    int b=countComponents();
    System.out.println(" z is " + z + " and components are" + b);
      //timer.cancel (  ) ; //Terminate the thread
    }
  }
}

apparently i am doing

panel = new breakoutpanel();
add(panel);

in the frame class. i was writing from work and the code was at home and i was mistken that it was frame.add(panel);

i seem to have two copies of panel.

Mike

i googled on the web and found that "Remember, Java use 'copy reference by value' argument passing. ". further googling of java copy reference by value, showed that java when it passes to a method passes a copy of the reference and at essence is pass by value. so when i created my panel i got one constructor but when i passed it another constructor is called for the new object coppied. i need to talk to that. Mike

I simply coppied my timer code into the panel class, and it worked. I tried to do it in the panel class at first but i was learning to use a timer at the same time and hadn't thought it worked. I do interesting enough now have that i can see with system.out.println(z); -- two coppies of z incrementing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.