Waiting for Swing's elements repaint completion - java

I want to be able to wait for Swing's repaint completion.
Example:
frame.repaint();
wait_for_repaint_to_finish();
//work
I have something like this:
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
try {
frame.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
});
Is this the correct way to do it?

The question is why do you need something like this? Why would you ever repaint the entire frame and wait for it to be complete. If we know what you are attempting to do we can probably give a better suggestion.
repaint() just schedules painting to be done. The RepaintManager will potentially consolidate multiple paint requests and do them at one time to make paintng more efficient.
Having said that, if you really need to force a repaint you can use
JComponent.paintImmediately(...);
But this should only be used as a last resort and not to fix a design problem.

You can override paintComponent():
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
...
// repaint finished here
}

Related

Why is my while loop not working in paintComponent?

When I run this code, I see nothing but a blank(white) Panel and I would like to know why.
Here is my code:
Graph.java
public class Graph extends JPanel {
private static final long serialVersionUID = -397959590385297067L;
int screen=-1;
int x=10;
int y=10;
int dx=1;
int dy=1;
boolean shouldrun=true;
imageStream imget=new imageStream();
protected void Loader(Graphics g){
g.setColor(Color.black);
g.fillRect(0,0,x,y);
x=x+1;
y=y+2;
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
while(shouldrun){
Loader(g);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Do not ever call Thread.sleep() on the Event Dispatch Thread!!!
This causes the thread that actually redraws the screen and makes controls responsive to stop doing anything.
For animations, use a Timer. Don't worry about writing the while loop yourself, just tell the Timer to fire every so often, and change the values of x and y inside that timer. Something like:
// this is an **inner** class of Graph
public class TimerActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
}
}
// snip
private final Timer yourTimer;
public Graph() {
yourTimer = new Timer(2000, new TimerActionListener());
timer.start();
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(0,0,x,y);
}
You never change the state of shouldrun within the loop -- so it will never end.
Also, never call Thread.sleep(...) within a painting method. This method is for painting and can never be put to sleep, else the GUI will be put to sleep, will be frozen.
First of all, your paintComponent method should only handle all painting and nothing else (if possible). You should not implement your program loop within paintComponent.
The blank screen can be caused by a number of reasons. You can easily debug it manually by commenting off certain section of your codes and run it. See whether it is still blank.
At least from what I see here, your paintComponent will give your problems.
If you want an animation, you can:
Use a swing timer
Create a loop in a new thread (not Event Dispatch Thread). Your loop will look something like this:
As below:
while(running){
update();
render();
try(
Thread.sleep(1000/fps);
)catch(InterruptedException ie){
ie.printStackTrace();
}
}
Note: To make a proper loop for animation, you will need more than that.

How to keep focus on JTextField. SwingWorker

I need to keep focus on JTextField. Application uses Swing library. I need set focus on that field from time to time in order to avoid user mistakes that would change focus to other comonents. I suppose I need to use SwingWorker. Set focus is an operation on Swing
component so it should be invoked in EDT. My question is how to write SwingWorker to do that?
I know that method done() pass tasks to be invoked in EDT but I need this task to be invoked every let's sey 2 seconds. Method done() is called one time.
So maybe sth like this will be ok?
public class myWorker extends SwingWorker<Void, Void> {
#Override
protected Void doInBackground() throws Exception {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//here set focus on JTextField
return null;
}
});
}}
Edit:
I noticed that method process() that is a part of SwingWorker may be appropriate beacuse it is invoked in EDT. I'm not sure but this method is probably invoked always when I call publish() metod. So could you tell me if this code is valid to do this task?
private class KeepFocusWorker extends SwingWorker<Void, Void>
{
#Override
protected Void doInBackground() throws Exception
{
while(true)
{
publish();
}
}
#Override
protected void process(List<Void> chunks)
{
codeBar.requestFocusInWindow();
}
}
Use javax.swing.Timer instead of SwingWorker. In this case actionPerformed will be executed in EDT. Also to set focus in a component, you need to call requestFocus. As the name suggests, it is a request only and not guaranteed. So you may change you approach.
Timer timer = new Timer(2000, new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
codeBar.requestFocus();
}
});
timer.setRepeats(true);
timer.start();
Surely it's better to limit the user's ability to take focus away from the textfield in the first place? Personally I don't see why it's an issue but I suppose it's better to keep focus in the one component rather than letting the user shift focus only for it to be shifted back every few seconds.
Therefore you could add a FocusListener to the component, override the focusLost method and basically requestFocus() again.
codeBar.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
codeBar.requestFocus();
}
});
NB I've not actually tried this myself but can't see why it wouldn't work.
Alternatively you can use an InputVerifier which always returns false to prevent focus being taken away.

Game loop completely freezes my program

Original Question
I'm currently working on a simple application that displays a map and will later implement pathfinding logic for units. I've implemented the map and view so far and everything runs just fine until I implemented the game loop.
With the game loop enabled, the program just freezes. I can't close the window anymore and the map isn't presented, even though the game loop is executed just fine. I've used this game loop twice in the past and never had any problems until now.
Edit: The game loop continues to execute just fine while everything else freezes.
Here are the two functions involved:
public GameController() {
paused = true;
frame = new GameFrame(this);
map = new Map(500, 500);
mvm = new MapViewModel(map.getMap(), map.getWidth(), map.getHeight());
//TODO: gameLoop() currently breaks the game.
gameLoop();
}
public void gameLoop() {
double tickTime, lastTick;
for (;;) {
tickTime = System.nanoTime();
lastTick = tickTime;
//Repaints the frame
update();
while (tickTime - lastTick < NANOSECONDS_PER_UPDATE) {
try {
Thread.sleep(1);
} catch (InterruptedException ignored) {}
tickTime = System.nanoTime();
}
}
}
edit2: I'm using Swing. The actual painting happens in the paintComponent method of my GamePanel (JPanel):
#Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
//Paints the map
painter.paintMap(g2, controller.getMvm());
}
Obviously, if you have any further questions feel free to ask. Thanks in advance.
Solution
Here's the code I'm using now, GameController and update haven't changed.
public void gameLoop() {
timer = new Timer(MILLISECONDS_PER_UPDATE, updater);
timer.start();
}
updater is an ActionListener that I have added as a private variable to the class.
private ActionListener updater = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("test2");
update();
}
};
You could add updater locally but I prefer it this way.
You tell us nothing about what GUI library you might be using, but assuming that it might be Swing, it looks like you're running some long-running code on the Swing event thread, the main thread responsible for doing all Swing graphics and for interacting with the user. If so, then the code will prevent Swing from redrawing the GUI, freezing your application.
My suggestions are:
Don't do this. Don't run any long-running code on the main event thread.
Instead consider using a Swing Timer to do your "game loop".
Or if you must use your while loop and Thread sleep, do it off of the event thread, but then be sure that all Swing calls that mutate the state of Swing objects be done on the event thread.
For more on Swing threading, please read Concurrency in Swing.

java set delay to change imageicon

i'm trying to set a delay when a button is pressed to set an imageicon to a certain image then set another delay so that another image would be set, all of this by single click.
in other word :
click a button->set image->delay->set another image.
what i get in my code is the last state only "set another image".
also i don't want to use use timers, i want to use delays.
and here the part in my code i'm concerned about.
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
lblNewLabel.setIcon(and);
sleeep(500);
lblNewLabel.setIcon(app);
}
});
and here is the delay function
void sleeep(int n)
{
try {
Thread.sleep(n);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
don't add MouseListener to JButton, nor for mouseClicked(), add ActionListener instead, btw all Mouse and Key events are implemented in JButton API and correctly
don't to use Thread.sleep(n); you have an issue with Concurency in Swing, use Swing Timer instead,
You should try executing the code that sets the image in the event dispatch thread using InvokeLater.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
lblNewLabel.setIcon(and);
}
});
sleeep();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
lblNewLabel.setIcon(and);
}
});

Repainting Continuously in Java

I have a Java program that uses threads. In my run method, I have:
public void run() {
while(thread != null){
repaint();
System.out.println("hi");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
}
public void paintComponent(Graphics g) {
// painting stuff
}
The problem is that the run method is executed, but the paintComponent section is not called. If this is not the right way to keep repainting the component, then how should I repaint it?
Cal repaint from a Swing Timer. That will not block the GUI, and will happen at whatever interval specified in the timer. Of course, by the nature of Swing/AWT painting, if the timer is set to repeat too fast, calls to paint might be coalesced (effectively ignored).
Also, make sure the method is an override using:
#Override
public void paintComponent(Graphics g){
You should only repaint a component when you need to (ie, when you update it).
Depending on what you're doing, you might also be interested in this. This is taken from Killer Game Programming in Java by Andrew Davison. He talks about active rendering. Your game loop is effectively:
public void run()
{
while (running)
{
gameUpdate(); // game state is updated
gameRender(); // render to a buffer
paintScreen(); // draw buffer to screen
try
{
Thread.sleep(20);
}
catch (InterruptedException e) {;}
}
}
And, the implementation of paint screen is (defined by a subclass of JComponent):
private void paintScreen()
{
final Graphics2D g2d;
try
{
g2d = (Graphics2D) this.getGraphics();
if (g2d != null && (backbuffer != null))
{
g2d.drawImage(backbuffer, 0, 0, null);
}
Toolkit.getDefaultToolkit().sync(); // sync the display on some systems [1]
g2d.dispose();
}
catch (Exception e)
{
;
}
}
From the book:
[Note 1] The call to Tookkit.sync() ensures that the display is promptly updated. This is required for Linux, which doesn't automatically flush its display buffer. Without the sync() call, the animation may be only partially updated, creating a "tearing" effect.
You have to call paint(g) for a heavy-weight container such as a JFrame. You call paintComponent(g) for light-weight containers like a JButton. See if that works.

Categories