invokeLater not working as expected (JButton never releasing) - java

I have:
A JFrame with a JButton on it.
A separate Canvas subclass to show animations.
And I wish to, at the press of the JButton bring up a new JFrame displaying the Canvas subclass as it animates.
The problem I face right now is that the new JFrame appears, however it doesn't get a chance to render anything and the JButton on the main frame stays depressed. The logic I figure behind this is that the EDT hasn't finished doing it's jobs such as showing the JButton as released and so does not get a chance to run the animation method and ends up in deadlock.
This logic treated me well in the past as I made this work by creating a new thread, but having learned more about Java, threads and Swing lately I've come to know that all Swing related events must be handled on one thread: the EDT.
This confuses me as to how I got it working before but lead me to believe that using invokeLater would help the problem; as the job of making the JFrame visible and showing animation would be placed at the end of the queue allowing the JButton to unrelease etc. I've had no luck however; have I completely misunderstood something?
Thanks!
(Also please no comments on my use of the Canvas class as opposed to JPanel, I have my reasons).
Sample code:
Test5 (class with main method).
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*
public class Test5 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test5().setup();
}
});
}
private void setup() {
JFrame frame = new JFrame("Test");
JButton button = new JButton("Click here");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
newFrame();
}
});
}
});
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void newFrame() {
JFrame newFrame = new JFrame("The new frame");
newFrame.setVisible(true);
newFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
CanvasClass canvas = new CanvasClass();
newFrame.getContentPane().add(canvas);
newFrame.pack();
canvas.runAnimation();
}
}
CanvasClass (Canvas subclass)
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
public class CanvasClass extends Canvas {
int x;
public CanvasClass() {
setSize(new Dimension(550,550));
this.x = (int) (Math.random() * 255);
}
//#Override
public void paint(Graphics g) {
g.setColor(new Color(x, x, x));
g.fillOval(0,0,500,500);
}
void runAnimation() {
while (true) {
randomise();
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
void randomise() {
x = (int) (Math.random() * 255);
}
}

You actualy invoke it in EDT but it's blocked in the canvas.runAnimation();
Place the code to be executed in a separate Thread (where you can call sleep) but call the repaint() in SwingUtilities.invokeLater()
Or even better to define a javax.swing.Timer and call the runAnimation() in the Timer's actionPerformed()
UPDATE:
int delay = 20; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
canvasInstance.randomise();
canvasInstance.repaint();
}
};
new Timer(delay, taskPerformer).start();
to be called instead of the runAnimation()

Related

Animation sequence in JFrame

I wanted to create a JFrame and put a sequence of images for animation in there. But the images don't appear in the frame window. I just want basic troubleshooting tips to make it appear in the window. Just edit the code for an answer if you can.
My question:
Why isn't the window displaying any pictures? It shows a window with a background color of blue, but that's it. Please tell me an efficient way to store images in variables and display it in a loop.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.*;
public class Game extends JLabel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
public static Game blah;
BufferedImage nekopics[] = new BufferedImage[7];
BufferedImage currentimg;
public String nekosrcs[];
int xpos;
Timer timer;
public Game() throws IOException
{
JFrame jframe = new JFrame();
nekosrcs = new String[] { "walk1.png", "walk2.png",
"walk3.png", "walk4.png", "walk5.png",
"walk6.png"};
jframe.setTitle("Game");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setLayout(new FlowLayout());
jframe.setSize(400, 400);
jframe.setResizable(false);
jframe.setVisible(true);
jframe.getContentPane().setBackground(Color.BLUE);
for (int i=0; i < nekopics.length; i++) {
nekopics[i] = ImageIO.read(new FileInputStream("D:/Programs
/pics"+nekosrcs[i]));
}
for (int i=0; i < nekopics.length; i++) {
timer = new Timer(1000, this);
timer.setInitialDelay(0);
timer.start();
currentimg = nekopics[i];
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paint(g);
g.drawImage(currentimg,100,100,this);
}
public static void main(String[] args) throws IOException {
blah = new Game();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
Alright, there are a lot of problems in your code, let's step into each of them:
You have a lot of spaces between lines, that makes your code a lot larger and harder to read
You haven't indented your code correctly (see the last } on your code, it's at the same level than the others; your for loops, etc), it makes the code so much harder to read and understand as well
You're creating a JFrame but extending JLabel, I'm not sure why you're doing this, if you're doing it so you can use the paintComponent() method, it's not necessary, on my code you can see how you can do it w/o extending any Component
If you haven't read the Swing Timer docs you should click that link and read what the ActionListener parameter does. In this case, we're going to use this listener to call the repaint() method and update our currentImage (or nextImage in the code below) and change the image accordingly. You failed to do this.
You were creating more than 1 Timer too, you created 6 here! All of them new but they had no action to do when the time finished
for (int i=0; i < nekopics.length; i++) {
timer = new Timer(1000, this);
timer.setInitialDelay(0);
timer.start();
currentimg = nekopics[i];
repaint();
}
You're changing unnecessarily the visibility of the paintComponent() method to public from protected
However I want to congratulate you for not using a null layout and following the recommendations I made on the comments above.
And finally the code that changes one image for another inside a Timer is the following, you can copy-paste it and change the image's names so you can see how it works.
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ImageSequence {
private JFrame frame;
private JPanel pane;
private Timer timer;
private int nextImage = 0;
private String[] images = {"tokyo", "tokyo2", "starwars"};
private Image img = null;
public static void main (String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ImageSequence().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame("Image Sequence");
timer = new Timer(1000, listener);
pane = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
img = ImageIO.read(new FileInputStream("/home/jesus/Pictures/" + images[nextImage] + ".jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(img , 0, 0, 200, 200, this);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
timer.start();
frame.getContentPane().add(pane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
ActionListener listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
System.out.println(nextImage);
nextImage = nextImage < images.length - 1 ? nextImage + 1 : 0;
System.out.println(nextImage);
pane.repaint();
}
};
}

Animations in Java not Working

I am trying to animate a circle in Java. I want it to move every time I press a key, but it is not working. Is there a problem with the way I am drawing the circle? Am I forgetting a repaint() somewhere.
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel implements KeyListener {
int x = 300, y = 300;
public Main() {
super();
}
public void paintComponent(Graphics g) {
g.drawOval(x, y, 300, 300);
}
#Override
public void keyPressed(KeyEvent e) {
x++;
y++;
repaint();
}
#Override
public void keyTyped(KeyEvent e) {}
#Override
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(1200, 800);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.add(new Main());
f.setVisible(true);
}
}
Don't use KeyListener, honestly, make use of the Key Bindings API instead, which has been designed to resolve the issues which KeyListener creates.
paintComponent in JPanel does an important job, you are expected to call it's super method before doing any custom painting.
See Painting in AWT and Swing and Performing Custom Painting for more details
You should also make an effort to initialise you UI from within the context of the Event Dispatching Thread, this solves a number of known issues on some platforms, see Initial Threads for more details
You are not very far off. First, you need to actually add a listener for your key events.
f.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
m.keyPressed(e);
}
});
Second, you'll notice this uses 'm'. That is a reference to your Main object.
Change:
f.add(new Main());
to:
Main m = new Main();
f.add(m);
Now it should work!

Java make GUI wait for a timer

I simply want this program to wait for a timer. All I want is for the program to pause for two seconds. I want this program to do is display "Start," wait for two seconds until the timer has finished, then display "Start, Finished Waiting, Finished." How can I make this program wait for the timer to finish? I believe that it currently creates the timer in a separate thread, not pausing the main thread, so it displays,"Start, Finished" then waits for two seconds and then displays "Start, Finished, Finished Waiting." This is not the order that I want things to happen in, and I have looked all over for a simple timer example when running a GUI and have found none. Thank you for your help, here is the code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class GUI extends JFrame {
private static final long serialVersionUID = 3560258176733156660L;
public static void main(String[] args) {
new GUI().setVisible(true);
}
private Timer timer;
private JTextArea area;
private String text;
public GUI() {
setLayout(null);
setSize(500, 120);
setTitle("Timer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
text = "";
area = new JTextArea(text);
area.setBounds(0, 0, 500, 120);
add(area);
doThings();
}
public void doThings() {
text += "Start, ";
area.setText(text);
// Want program to wait for two seconds
waitForTwoSeconds();
text += "Finished ";
area.setText(text);
}
public void waitForTwoSeconds() {
timer = new Timer(2000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
text += "Finished Waiting, ";
area.setText(text);
timer.stop();
}
});
timer.start();
}
}
Take the code from after you call waitForTwoSeconds and place within the actionPerformed method...
public void doThings() {
area.setText("Start, ");
// Want program to wait for two seconds
waitForTwoSeconds();
}
public void waitForTwoSeconds() {
timer = new Timer(2000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
area.append("Finished Waiting, ");
area.append("Finished ");
timer.stop();
}
});
timer.setRepeats(false);
timer.start();
}
This will cause Finished Waiting, Finished to be append to the JTextArea 2 seconds after you click the button...
You DO NOT want to perform any long running/blocking operations within the context of the Event Dispatching Thread, this WILL make it look like your program as hang, cause it has.
See, Concurrency in Swing and How to use Swing Timers for more details
Updated...
Swing (and most UIs) are event driven, that is, something happens and you respond to it. For instance, with the Timer, the timer tripped and you responded to the event. You can't block/wait within the Event Dispatching Thread, it will simply cause the UI to stop responding and painting, this is the way the framework works, you can learn to live with it or continue to be frustrated by it (remember, wanting something and getting it to work, are two different things)
There are, however, things you can do, the Timer is one example, another is the SwingWorker
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextArea ta;
public TestPane() {
setLayout(new BorderLayout());
ta = new JTextArea(10, 20);
JButton btn = new JButton("Make it so");
add(new JScrollPane(ta));
add(btn, BorderLayout.SOUTH);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
btn.setEnabled(false);
ta.append("Start, ");
SwingWorker<String, String> worker = new SwingWorker<String, String>() {
#Override
protected String doInBackground() throws Exception {
Thread.sleep(2000);
publish("Finished waiting, ");
return null;
}
#Override
protected void process(List<String> chunks) {
for (String text : chunks) {
ta.append(text);
}
}
#Override
protected void done() {
ta.append("Finished");
btn.setEnabled(true);
}
};
worker.execute();
}
});
}
}
}
What this basically does is, in a background thread, it waits two seconds and then (via the publish/process methods), prints "Finished Waiting", then after the doInBackground returns, done is (eventually) called and "Finished" is printed.
This is all done so that the UI updates occur from within the context of the Event Dispatching Thread, meeting the single thread requirements of Swing
Ok, so I guess my question should really look like this then:
How do i make the program wait until the waitForTwoSeconds() method is complete before doing area.append("Finished "); ? This is really what I want to accomplish.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class GUI extends JFrame {
private static final long serialVersionUID = 3560258176733156660L;
public static void main(String[] args) {
new GUI().setVisible(true);
}
private Timer timer;
private JTextArea area;
public GUI() {
setLayout(null);
setSize(500, 120);
setTitle("Timer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
area = new JTextArea("");
area.setBounds(0, 0, 500, 120);
add(area);
doThings();
}
public void doThings() {
area.setText("Start, ");
// Want program to wait for two seconds
waitForTwoSeconds();
// Don't want to do this until waitForTwoSeconds() has finished...
area.append("Finished ");
}
public void waitForTwoSeconds() {
timer = new Timer(2000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
area.append("Finished Waiting, ");
timer.stop();
}
});
timer.setRepeats(false);
timer.start();
}
}

Im having trouble making an image update on intervals of 200

I am trying to make a thread that reads the screen and displays it in a frame, this code is meant to run at 5fps, so far it reads the screen, but I am having trouble making the JFrame display the updating Image each "frame" or 200 mili-seconds. when I use repaint(); or revalidate();
public static void startScreenRecorder()
{
Thread screenThread = new Thread()
{
public synchronized void run()
{
long time;
long lastFrameTime = 0;
JFrame frame = new JFrame("Screen capture");
ImagePanel panel = new ImagePanel(captureScreen());
frame.add(panel);
frame.setSize(300, 400);
frame.setVisible(true);
while (true)
{
time = System.currentTimeMillis();
while (time - lastFrameTime < 190)
{
try {
Thread.sleep(10);
} catch (Exception e) {
}
time = System.currentTimeMillis();
}
lastFrameTime = time;
panel = new ImagePanel(captureScreen());
panel.revalidate();
panel.repaint();
frame.revalidate();
frame.repaint();
}
}
};
screenThread.start();
}
Don't use Thread.sleep() to attempt to control animation.
Animation should be done by using a Swing Timer. When you use a Timer the GUI is automatically updated on the EDT.
panel = new ImagePanel(captureScreen());
The above code doesn't do anything. It just creates a panel in memory. Nowhere to you actually add the panel to the GUI. Changing the reference of a variable does not update the GUI.
Instead you should probably add a JLabel to the frame (when you initially create the frame). Then when you have a new Image you just do:
label.setIcon( new ImageIcon( your screen capture ) );
I wouldn't be surprised if your code shows no images at all since it ignores Swing threading rules:
All Swing code needs to be called on the Swing event dispatch thread (EDT) only.
All other long-running code needs to be called in a background thread. I assume that this means captureScreen().
You should never call Thread.sleep(...) on the Swing event thread unless you want to put your entire application to sleep.
Better perhaps to use a Swing Timer.
You create new ImagePanels but do nothing with them -- you never add them to the GUI for instance, except for the first JPanel. Note that if you change the object a variable refers to, here the panel variable, this will have absolutely no effect on instances of the object used elsewhere, there the JPanel displayed in the GUI.
Rather than create new JPanels, why not instead create ImageIcons with your images and swap a visualized JLabel's Icon with setIcon(...)?
Since you have a lot of background stuff going on, consider using a SwingWorker<Void, Icon> to do your work, and have it publish ImageIcons that are then displayed in the GUI's JLabel. If you did this, then you probably wouldn't use a Swing Timer since the timing would be done in the SwingWorker's background thread.
For example:
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
#SuppressWarnings("serial")
public class SwingWorkerEg extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = 400;
private JLabel displayedLabel = new JLabel();
public SwingWorkerEg() {
setLayout(new BorderLayout());
add(displayedLabel);
try {
MySwingWorker mySwingWorker = new MySwingWorker();
mySwingWorker.execute();
} catch (AWTException e) {
e.printStackTrace();
}
}
public void setLabelIcon(Icon icon) {
displayedLabel.setIcon(icon);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private class MySwingWorker extends SwingWorker<Void, Icon> {
private final Rectangle SCREEN_RECT = new Rectangle(0, 0, PREF_W,
PREF_H);
private Robot robot = null;
public MySwingWorker() throws AWTException {
robot = new Robot();
}
#Override
protected Void doInBackground() throws Exception {
Timer utilTimer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
BufferedImage capturedImage = captureScreen();
publish(new ImageIcon(capturedImage));
}
};
long delay = 200;
utilTimer.scheduleAtFixedRate(task, delay, delay);
return null;
}
#Override
protected void process(List<Icon> chunks) {
for (Icon icon : chunks) {
setLabelIcon(icon);
}
}
private BufferedImage captureScreen() {
BufferedImage img = robot.createScreenCapture(SCREEN_RECT);
return img;
}
}
private static void createAndShowGui() {
SwingWorkerEg mainPanel = new SwingWorkerEg();
JFrame frame = new JFrame("SwingWorker Eg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Which would display...

A loader screen in java that doesn't appear on the screen

I'm trying to make a loader screen in java. So far I've managed to successfully make a splash screen for my program, the splash screen works fine.
I've used the same code to create the loader but when I call the object only the sleep part works, I mean nothing really appears on the screen.
public class IL extends JWindow {
Image L=Toolkit.getDefaultToolkit().getImage("L.png");
ImageIcon LI=new ImageIcon(L);
public IL (){
try
{
setSize(LI.getIconWidth(),LI.getIconHeight());
setLocationRelativeTo(null);
show();
Thread.sleep(10000);
dispose();
}
You're blocking the UI thread with that sleep, essentially preventing it from displaying anything.
You should use a timer for this. See How to Use Swing Timers and the Swing Timer API docs. You use a timer to do the hide/dispose after however much time you want. You could also use that timer to display a progress bar or animate your loader page.
Since I had one handy, a complete implementation.
import java.awt.Image;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import javax.imageio.ImageIO;
public class Splash extends JWindow {
public Splash(Image image, int millis){
ImageIcon icon=new ImageIcon(image);
add(new JLabel(icon));
pack();
setLocationRelativeTo(null);
ActionListener hideAction = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dispose();
}
};
Timer timer = new Timer(millis, hideAction);
setVisible(true);
timer.start();
}
public static void main(String[] args) throws Exception {
final Image image = ImageIO.read(
new URL("http://pscode.org/media/stromlo2.jpg"));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Splash(image, 6000);
}
});
}
}

Categories