Call redrawing from another class in Java?

I probably do it wrong, so please be kind. I am developing a Java game and I am at the stage of testing movement / animation of characters.

The face can move up and down the grid. The class into which the grid is inserted is the gamePanel class. The buttons are in the gameControlPanel class.

I have a button that spawns a person in a grid. Then I have a button to move my face up and down.

When the move up button is pressed, it calls the move up method from the person class. (At the moment I am testing only one β€œperson” at a time). The following code is in this method ...

int move = 10; while(move!=0) { setTopLeftPoint(new Point((int)getTopLeftPoint().getX(), (int)getTopLeftPoint().getY() - 3)); try { Thread.sleep(300); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } move-=1; } 

The problem is that I cannot name the repaint method for the gamePanel class from the Person class. To get around this, I created a timer in the gamePanel class, which replicates every 20 ms.

When I press the up button after the person is generated, the button remains pressed until the while loop cycles are completed, and then the circle image is displayed in the grid square above.

I will try to answer any questions regarding this.

+4
java multithreading timer animation swing
Apr 21 '09 at 13:59
source share
4 answers

If you want to redraw at a certain interval, javax.swing.Timer is probably the class for you. In the specific case of repaint you can call it from a thread other than EDT, but you may run into difficulties as you are now dealing with multiple threads.

+1
Apr 23 '09 at 16:53
source share

repaint () does not immediately redraw the GUI. Rather, it sends a message to the AWT stream, telling it to draw at the next convenient opportunity. When this gets a chance, it will repaint your application. However, if you do this in the event handler, the AWT thread is already busy, and you need to exit the handler to return control to the AWT handler.

As a rule, you do not want to perform long-term calculations in the AWT stream (including event handlers), since they will stop responding to other events until your calculations are completed. This will often be displayed to the user as stuck buttons, as described. To get around this, use SwingWorker , which can perform calculations in a separate thread.

Finally, something you need to know (but not necessarily change) is that timers and sleep do not guarantee when they wake up. Rather, they guarantee that they will not wake up before the time runs out, but theoretically they can sleep forever. In addition, not all machines have a timer resolution of 1 ms. In particular, on many Windows machines, timers have a resolution of 55 ms, so a 20 ms timer can give strange results.

+2
Apr 21 '09 at 14:51
source share

I do not have much experience creating games, but having a loop to control the entire animation is a fundamental aspect of game programming. In most simple 2d games, there is only 1 cycle to display most of its animation.

In my experience, a good way to display a whole bunch of things is to collect all the entities in your game in one place and simply iterate over this collection, passing the Graphics object to each object.

This will allow each object to be drawn on a graphic object. Although this is just one way to do this.

 synchronized ( entities ) { for ( Entity e : entities ) { e.draw( g ); e.doAction(); } } 
0
Apr 21 '09 at 14:08
source share

Is the button used by clicking the event flow button of your GUI?

If so, then the redraw method in the graphical interface will not work until the dispatching event stream ends (i.e. when the button is released and exits the loop). I had this problem lately, and the best solution I can offer is to make the class with the motion algorithm stream and disable the stream when a click is detected. This allows you to complete the event dispatch stream and, therefore, allows you to redraw gui.

For more information about a stream, see Starting a Stream .

0
Apr 21 '09 at 14:31
source share



All Articles