Here I have a method
public static Color pickColor(){
final aero.colorpicker.Frame frame = new aero.colorpicker.Frame();
new Thread(new Runnable() {
#Override
public void run() {
while (!frame.isSelected()) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return frame.getColorPicked();
}
isSelected() is flag that makes us know that user CHOOSED the color he want.
The problem is - if I use it like
Color c = pickColor(); in c will write default (black) color of aero.colorpicker.Frame class.
I need pause here, which will wait for select.
package aero.colorpicker;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by Aero on 28.12.2014.
*/
public class Frame extends JFrame {
//MAINFIELD
private Color colorPicked = Color.black;
private boolean selected = false;
//GUIFIELDS
private JPanel rightPanel;
private JLabel R;
private JLabel G;
private JLabel B;
private JTextField RData;
private JTextField GData;
private JTextField BData;
private JPanel RPanel;
private JPanel GPanel;
private JPanel BPanel;
private ColorPanel colorPanel;
private JButton pick;
private BufferedImage colors;
private JLabel imageLabel;
public Frame(){
initFrame();
setVisible(true);
}
private void initComponents(){
rightPanel = new JPanel();
R = new JLabel("R");
G = new JLabel("G");
B = new JLabel("B");
RData = new JTextField();
GData = new JTextField();
BData = new JTextField();
RPanel = new JPanel();
GPanel = new JPanel();
BPanel = new JPanel();
colorPanel = new ColorPanel();
pick = new JButton("Pick");
}
private void addListeners(){
RData.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getRed();
colorPanel.repaint();
}
});
GData.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getGreen();
colorPanel.repaint();
}
});
BData.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getBlue();
colorPanel.repaint();
}
});
pick.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getRed();
getBlue();
getGreen();
Frame.this.setVisible(false);
}
});
imageLabel.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
int x, y;
x = e.getX();
y = e.getY();
setColorPicked(new Color(colors.getRGB(x, y)));
colorPanel.repaint();
RData.setText(Integer.toString(getColorPicked().getRed()));
BData.setText(Integer.toString(getColorPicked().getBlue()));
GData.setText(Integer.toString(getColorPicked().getGreen()));
}
});
}
private void getRed(){
int r;
try {
r = Integer.parseInt(RData.getText());
}catch (NumberFormatException nfe){
RData.setText("0");
r = 0;
}
setColorPicked(r, getColorPicked().getGreen(), getColorPicked().getBlue());
}
private void getGreen(){
int g;
try {
g = Integer.parseInt(GData.getText());
}catch (NumberFormatException nfe){
GData.setText("0");
g = 0;
}
setColorPicked(getColorPicked().getRed(), g, getColorPicked().getBlue());
}
private void getBlue(){
int b;
try {
b = Integer.parseInt(BData.getText());
}catch (NumberFormatException nfe){
BData.setText("0");
b = 0;
}
setColorPicked(getColorPicked().getRed(), getColorPicked().getGreen(), b);
}
private void initFrame(){
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.setLayout(new BorderLayout());
this.setSize(369, 194);
this.setTitle("Color picker");
this.setResizable(false);
initComponents();
InputStream input = Frame.class.getClassLoader().getResourceAsStream("colorGradient.jpg");
try {
colors = ImageIO.read(input);
} catch (IOException e) {
e.printStackTrace();
}
imageLabel = new JLabel(new ImageIcon(colors));
this.add(imageLabel, BorderLayout.CENTER);
rightPanel.setLayout(new GridLayout(5, 1));
RPanel.setLayout(new GridLayout(1, 2));
GPanel.setLayout(new GridLayout(1, 2));
BPanel.setLayout(new GridLayout(1, 2));
RPanel.add(R);
RPanel.add(RData);
GPanel.add(G);
GPanel.add(GData);
BPanel.add(B);
BPanel.add(BData);
rightPanel.add(RPanel);
rightPanel.add(GPanel);
rightPanel.add(BPanel);
rightPanel.add(colorPanel);
rightPanel.add(pick);
this.add(rightPanel, BorderLayout.EAST);
this.repaint();
addListeners();
}
public Color getColorPicked() {
return colorPicked;
}
private void setColorPicked(Color colorPicked) {
this.colorPicked = colorPicked;
}
private void setColorPicked(int r, int g, int b){
this.colorPicked = new Color(r, g, b);
}
private void setSelected(){
selected = true;
}
public boolean isSelected(){
return selected;
}
private class ColorPanel extends JPanel{
public void paintComponent(Graphics g){
g.setColor(colorPicked);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}
Your issue here is that your method pickColor creates the thread that waits for a colour to be picked, starts it and then exits. In other words pickColor isn't waiting for the thread to complete. The thread itself does nothing when it does complete.
One solution would be to wait until the thread completes before exiting the method. That would still be poor design, however. It is possible (in fact, likely) that you are calling pickColor within the thread processing UI events (i.e. the dispatch thread) which will make the UI unresponsive. It also uses a unnecessary busy wait.
If this is what you want to do then you'll need to understand how to create a new event queue. You also need to understand how to use semaphores to block the thread. I'll provide the code here but I wouldn't recommend dumping it into your application without understanding what you are doing. This is designed to be a subclass of your dialog class so that the method used to close the dialog can be overwritten to notify the blocked thread.
For the method that shows the colour picking dialog:
public synchronized Color pickColor() {
// code to create and show the dialog
EventQueue tempEventQueue = new EventQueue();
Toolkit.getDefaultToolkit().getSystemEventQueue().push(tempEventQueue);
try {
wait();
} catch (InterruptedException ex) {
// stop waiting on interrupt
} finally {
tempEventQueue.pop();
}
// return colour from dialog
}
For the method that is called when the user closes the dialog:
public synchronized void closeChooser() {
notifyAll();
super.closeChooser(); // or whatever it's called
}
The correct solution is to have two methods: one to open the colour pick dialog and then a second which is called when the dialog closes. I don't know what capabilities aero.colorpicker.Frame has but if it doesn't provide this then you could subclass it and override whatever method is called when the user accepts or cancels.
Related
I am printing simple value to append JTextArea using simple for loop, and when I run it, it's properly Run if I print value in console output...
But if I append JTextArea and print value in the text area, they are appended all after whole program run.
public class SwingThread {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SwingThread window = new SwingThread();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public SwingThread() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
JButton btnNewButton = new JButton("New button");
scrollPane.setColumnHeaderView(btnNewButton);
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
try
{
for(int i = 0 ; i <= 5 ; i++)
{
textArea.append("Value "+i+"\n");
System.out.println("Value is" + i);
Thread.sleep(1000);
}
}
catch(Exception e)
{
System.out.println("Error : "+e);
}
}
});
}
}
I want to append one by one, but it was appended after the whole program runs.
Your problem is with your use of Thread.sleep, since when you call this on the Swing event thread (or EDT for Event Dispatch Thread) as you are doing, it will put the entire Swing event thread to sleep. When this happens the actions of this thread cannot be performed, including painting the GUI (updating it) and interacting with the user, and this will completely freeze your GUI -- not good. The solution in this current situation is to use a Swing Timer as a pseudo-loop. The Timer creates a loop within a background thread and guarantees that all code within its actionPerformed method will be called on the Swing event thread, a necessity here since we don't want to append to the JTextArea off of this thread.
Also as others have noted, if all you want to do is to perform a repeated action with delay in Swing, then yes, use this Swing Timer. If on the other hand you wish to run a long-running bit of code in Swing, then again this code will block the EDT and will freeze your program. For this situation use a background thread such as one supplied by a SwingWorker. Please check out Lesson: Concurrency in Swing for more on this.
e.g.,
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// delay between timer ticks: 1000
int timerDelay = 1000;
new Timer(timerDelay, new ActionListener() {
private int counter = 0;
#Override
public void actionPerformed(ActionEvent e) {
// timer's stopping condition
if (counter >= MAX_VALUE) { // MAX_VALUE is a constant int = 5
((Timer) e.getSource()).stop();
} else {
textArea.append("Value " + counter + "\n");
}
counter++; // increment timer's counter variable
}
}).start();
}
});
The whole thing:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.*;
import javax.swing.*;
public class SwingThread2 {
protected static final int MAX_VALUE = 5; // our constant
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SwingThread2 window = new SwingThread2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SwingThread2() {
initialize();
}
private void initialize() {
frame = new JFrame();
// frame.setBounds(100, 100, 450, 300); // avoid this
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea(15, 40);
scrollPane.setViewportView(textArea);
JButton btnNewButton = new JButton("New button");
scrollPane.setColumnHeaderView(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// delay between timer ticks: 1000
int timerDelay = 1000;
new Timer(timerDelay, new ActionListener() {
private int counter = 0;
#Override
public void actionPerformed(ActionEvent e) {
// timer's stopping condition
if (counter >= MAX_VALUE) { // MAX_VALUE is a constant int = 5
((Timer) e.getSource()).stop();
} else {
textArea.append("Value " + counter + "\n");
}
counter++; // increment timer's counter variable
}
}).start();
}
});
// better to avoid setting sizes but instead to
// let the components size themselves vis pack
frame.pack();
frame.setLocationRelativeTo(null);
}
}
Just for further information, here is an example of the same program above that uses a SwingWorker to perform a long running action, and then update a JProgressBar with this action. The worker is quite simple, and simply uses a while loop to advance a counter variable by a bounded random amount. It then transmits uses this value to update its own progress property (a value that can only be from 0 to 100, and so in other situations, the value will need to be normalized to comply with this). I attach a PropertyChangeListener to the worker, and this is notified on the Swing event thread whenever the worker's progress value changes and also whenever the SwingWorker changes state, such as when it is done operating. In the latter situation, the worker's StateValue becomes StateValue.DONE. The listener then updates the GUI accordingly. Please ask if any questions.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import javax.swing.*;
public class SwingThread2 {
protected static final int MAX_VALUE = 5; // our constant
private JFrame frame;
private JProgressBar progressBar = new JProgressBar();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SwingThread2 window = new SwingThread2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SwingThread2() {
initialize();
}
private void initialize() {
frame = new JFrame();
// frame.setBounds(100, 100, 450, 300); // avoid this
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea(15, 40);
scrollPane.setViewportView(textArea);
JButton btnNewButton = new JButton("New button");
scrollPane.setColumnHeaderView(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// delay between timer ticks: 1000
int timerDelay = 1000;
new Timer(timerDelay, new ActionListener() {
private int counter = 0;
#Override
public void actionPerformed(ActionEvent e) {
// timer's stopping condition
if (counter >= MAX_VALUE) { // MAX_VALUE is a constant
// int = 5
((Timer) e.getSource()).stop();
} else {
textArea.append("Value " + counter + "\n");
}
counter++; // increment timer's counter variable
}
}).start();
}
});
progressBar.setStringPainted(true);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
bottomPanel.add(new JButton(new MyAction("Press Me")));
bottomPanel.add(progressBar);
frame.getContentPane().add(bottomPanel, BorderLayout.PAGE_END);
// better to avoid setting sizes but instead to
// let the components size themselves vis pack
frame.pack();
frame.setLocationRelativeTo(null);
}
private class MyAction extends AbstractAction {
public MyAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
public void actionPerformed(ActionEvent e) {
progressBar.setValue(0);
setEnabled(false);
MyWorker myWorker = new MyWorker();
myWorker.addPropertyChangeListener(new WorkerListener(this));
myWorker.execute();
}
}
private class WorkerListener implements PropertyChangeListener {
private Action action;
public WorkerListener(Action myAction) {
this.action = myAction;
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
int progress = (int) evt.getNewValue();
progressBar.setValue(progress);
} else if ("state".equals(evt.getPropertyName())) {
if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
action.setEnabled(true);
#SuppressWarnings("rawtypes")
SwingWorker worker = (SwingWorker) evt.getSource();
try {
// always want to call get to trap and act on
// any exceptions that the worker might cause
// do this even though get returns nothing
worker.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}
}
private class MyWorker extends SwingWorker<Void, Void> {
private static final int MULTIPLIER = 80;
private int counter = 0;
private Random random = new Random();
#Override
protected Void doInBackground() throws Exception {
while (counter < 100) {
int increment = random.nextInt(10);
Thread.sleep(increment * MULTIPLIER);
counter += increment;
counter = Math.min(counter, 100);
setProgress(counter);
}
return null;
}
}
}
I need to make a slot machine that implements thread in java and jframe
this is what iv'e done so far kindly tell me what i need to do in order make the images change per .5 seconds when i press the the play and stop when i press stop. If all the three images are the same it'll say you won. This is what iv'e got so far how will i change this numbers or text to images in jlabel.
public class MySlotNumber extends JFrame{
private MyJLabel x;
private MyJLabel y;
private MyJLabel z;
private JButton btn;
public MySlotNumber(){
super("ABC");
setLayout(new FlowLayout());
Font font = new Font("arial",Font.ITALIC,50);
x = new MyJLabel();
x.setFont(font);
y = new MyJLabel();
y.setFont(font);
z = new MyJLabel();
z.setFont(font);
btn = new JButton("PLAY");
btn.setFont(font);
add(x);
add(y);
add(z);
add(btn);
final Thread thx = new Thread(x);
final Thread thy = new Thread(y);
final Thread thz = new Thread(z);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("PLAY")){
if(thx.isAlive()){
thx.resume();
thy.resume();
thz.resume();
} else {
thx.start();
thy.start();
thz.start();
}
btn.setText("STOP");
} else {
thx.suspend();
thy.suspend();
thz.suspend();
btn.setText("PLAY");
System.out.println(x.getText());
}
}
});
}
- - - - - - --
public class MyJLabel extends JLabel implements Runnable{
private Random r;
private int ctr;
private final int T = 500;
public MyJLabel(){
setText("0");
ctr = 0;
r= new Random();
}
#Override
public void run() {
while(true){
try {
Thread.sleep(T);
} catch (InterruptedException ex) {
Logger.getLogger(MyJLabel.class.getName()).log(Level.SEVERE, null, ex);
}
//ctr++;
ctr = r.nextInt(9)+1;
setText(String.valueOf(ctr));
}
}
}
Here is one way to put a picture on a JLabel and change it when you click a button. (I am using window builder for eclipse, so it may look a little odd.) It is not recommended you use absolute paths because when you move your project or the file, it breaks (I did this just for proof of concept).
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Test {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1379, 643);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Andrew\\Pictures\\Random Pictures\\Capture.JPG"));
frame.getContentPane().add(lblNewLabel, BorderLayout.CENTER);
JButton btnClickMe = new JButton("click me");
btnClickMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Andrew\\Pictures\\Random Pictures\\I'm pretty sure he did.JPG"));
}
});
frame.getContentPane().add(btnClickMe, BorderLayout.EAST);
}
}
I am having two classes, a main class and class which extends JPanel and implements Runnable. I am trying to create two threads for the same instance of the JPanel class in an actionListener, but i just don't know where to create the JPanel1 object...
//Edit: Button1 is the start of the application .After that , button 2 will appear with a quick animation of labels and when clicked it(button2) will start the same animation too. How can i do whenever one of these buttons is clicked to launch the run method ?
public void run() {
if(isTom){
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.removeAll();
panel.add(tomLabel1);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.add(tomLabel2);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.add(tomLabel3);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.add(tomLabel4);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.add(tomLabel5);
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
panel.removeAll();
repaint();
revalidate();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
public Game(){
JFrame frame = new JFrame();
Panel1 key = new Panel1();
key.addKeyListener(key);
frame.add(key);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
}
public static void main(String[] args) {
new Game();
}
public class Panel1 extends JPanel implements KeyListener,Runnable{
JButton button1 = new JButton("BUTTON1");
JButton button2 = new JButton("BUTTON2");
add(button1);add(button2);
Thread t = new Thread(this); // This works, but i need it inside the actionListener.
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("button1");
Thread x = new Thread(j);//'j' is an JPanel1 object. I need something like this i guess
x.setName("Thread x");});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("button2");
Thread y = new Thread(j);
y.setName("Thread y");
});
public void run(){
System.out.println(Thread.currentThread().getName());
}
First, Swing is NOT thread safe! This means that you should NEVER create or modify the UI from outside of context of the Event Dispatching Thread!
Second, Swing is a single threaded environment, this means that you should never block or execute long running code from within the context of the Event Dispatching Thread, this will cause the UI to freeze until the block is removed.
Your concept is correct, you implementation is wrong, you should use a Swing Timer instead.
Instead of removing and adding labels, use a single label and change it's properties (text/icon, what ever)
See Concurrency in Swing and How to use Swing Timers for more details
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
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();
}
try {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
}
public class TestPane extends JPanel {
private JButton button1;
private JButton button2;
private SplashScreen splashScreen;
public TestPane() throws IOException {
button1 = new JButton("Button One");
button2 = new JButton("Button Two");
JPanel buttons = new JPanel();
buttons.add(button1);
buttons.add(button2);
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
splashScreen.run();
}
});
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
splashScreen.run();
}
});
splashScreen = new SplashScreen();
setLayout(new BorderLayout());
add(splashScreen);
add(buttons, BorderLayout.SOUTH);
}
}
public static class SplashScreen extends JPanel {
protected static final int IMAGE_COUNT = 4;
private JLabel label;
private Timer timer;
private int delta;
private int count;
private Icon[] icons;
private Dimension preferredSize;
public SplashScreen() throws IOException {
String path = "/images/splash";
String ext = ".png";
icons = new Icon[IMAGE_COUNT];
int maxWidth = 0;
int maxHeight = 0;
for (int index = 0; index < IMAGE_COUNT; index++) {
String name = path + (index + 1) + ext;
System.out.println(name);
icons[index] = new ImageIcon(ImageIO.read(getClass().getResource(name)));
maxWidth = Math.max(maxWidth, icons[index].getIconWidth());
maxHeight = Math.max(maxHeight, icons[index].getIconHeight());
}
preferredSize = new Dimension(maxWidth, maxHeight);
timer = new Timer(250, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (count >= IMAGE_COUNT) {
count = IMAGE_COUNT - 2;
delta = -1;
} else if (count < 0) {
((Timer)e.getSource()).stop();
} else {
label.setIcon(icons[count]);
count += delta;
}
}
});
setLayout(new BorderLayout());
label = new JLabel();
add(label);
}
#Override
public Dimension getPreferredSize() {
return preferredSize;
}
public void run() {
if (!timer.isRunning()) {
delta = 1;
count = 0;
timer.start();
}
}
}
}
Create a new instance of a Thread with your Panel1 class instance:
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("button1");
Thread x = new Thread(Panel1.this);
x.start();
x.setName("Thread x");});
Repeat for the other button with a new Thread object:
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("button2");
Thread y = new Thread(Panel1.this);
y.start();
y.setName("Thread y"); });
Panel1.this is referring to the instance of the Panel1 class that is currently running, making sure that your Threads are executing run() of that instance.
I have a project in NetBeans that uses a layered pane to display overlapping panels by switching between them as necessary. When started, the program displays an intro animation on one of the panels (running on a separate thread), achieved by the following:
public class IntroPanel extends javax.swing.JPanel implements Runnable {
private boolean finished;
private int opacity;
private JLabel[] labels = new JLabel[3];
public IntroPanel() {
initComponents();
labels[0] = jLabel1;
labels[1] = jLabel2;
labels[2] = jLabel3;
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
for(JLabel label : labels)
label.setForeground(new Color(200,200,200,opacity));
}
#Override
public void run(){
while (!finished) {
while (opacity < 255) {
try {
Thread.sleep(30);
opacity += 5;
repaint();
} catch (InterruptedException ex) {
Logger.getLogger(IntroPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
repaint();
Thread.sleep(3000);
} catch (InterruptedException ex) {
Logger.getLogger(IntroPanel.class.getName()).log(Level.SEVERE, null, ex);
}
while (opacity > 0) {
try {
Thread.sleep(30);
opacity -= 5;
repaint();
} catch (InterruptedException ex) {
Logger.getLogger(IntroPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
Logger.getLogger(IntroPanel.class.getName()).log(Level.SEVERE, null, ex);
}
finished = true;
}
}
The intended effect is to have three JLabels fade in, stay on screen for a moment then fade out. The first two labels contain italics text with a large font size, the third contains small-print. The issues here:
The text of the first two labels appears non-italics while fading in or out, only briefly becoming italics while the labels aren't fading.
The third label (the small-print, non-italics one) doesn't fade at all, instead "shaking" rapidly up and down during the repaint loops.
I read about how it's a better idea to write my own active rendering method instead of using paintComponent() but replacing paintComponent() with the following:
public void render() {
Graphics g = getGraphics();
for(JLabel label : labels)
label.setForeground(new Color(200,200,200,opacity));
g.dispose();
}
does the same flickering. Am I missing something here or looped repaints and layered frames just don't mix?
You should do none of the code in your paintComponent inside of that method. That should be in another region of your program. paintComponent is for painting only, not for setting state.
Your while loops should be replaced by Swing Timer, and this is where the setForground calls should go.
You should post a minimal, complete valid, example, a small program that we can compile run and test. Your code does not allow this.
For example:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class IntroPanel extends JPanel {
private static final int DELAY_TIME = 5;
protected static final Color FG_COLOR = Color.blue;
private List<JLabel> labelList = new ArrayList<JLabel>();
private List<Color> colorList = new ArrayList<>();
public IntroPanel() {
for (int i = 0; i < 255; i++) {
colorList.add(new Color(0, 0, 255, i));
}
for (int i = 0; i < 255; i++) {
colorList.add(new Color(0, 0, 255, 255 - i));
}
setLayout(new GridLayout(0, 1));
JLabel label1 = new JLabel("Label 1");
label1.setFont(label1.getFont().deriveFont(Font.ITALIC, 24));
add(label1);
labelList.add(label1);
JLabel label2 = new JLabel("Label 2");
label2.setFont(label2.getFont().deriveFont(Font.ITALIC, 24));
add(label2);
labelList.add(label2);
JLabel label3 = new JLabel("Label 3");
label3.setFont(label3.getFont().deriveFont(Font.PLAIN, 10));
add(label3);
labelList.add(label3);
new Timer(DELAY_TIME, new ActionListener() {
private int index = 0;
#Override
public void actionPerformed(ActionEvent evt) {
if (index < colorList.size()) {
for (JLabel label : labelList) {
label.setForeground(colorList.get(index));
}
index++;
} else {
((Timer)evt.getSource()).stop();
}
}
}).start();
}
private static void createAndShowGUI() {
IntroPanel paintEg = new IntroPanel();
JFrame frame = new JFrame("IntroPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I'm trying to capture the screen without including my application's window. To do this I first call setVisible(false), then I call the createScreenCapture method, and finally I call setVisible(true). This isn't working however and I'm still getting my applications window in the screen capture. If I add a call to sleep this seems to resolve the issue, but I know this is bad practice. What is the right way to do this?
Code:
setVisible(false);
BufferedImage screen = robot.createScreenCapture(rectScreenSize);
setVisible(true);
Have you tried to use SwingUtilities.invokeLater() and run the capture inside of the runnable passed as an argument? My guess is that the repaint performed to remove your application is performed right after the end of the current event in the AWT-EventQueue and thus invoking the call immediately still captures your window. Invoking the createCapture in a delayed event through invokeLater should fix this.
you have to delay this action by implements Swing Timer, for example
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
public class CaptureScreen implements ActionListener {
private JFrame f = new JFrame("Screen Capture");
private JPanel pane = new JPanel();
private JButton capture = new JButton("Capture");
private JDialog d = new JDialog();
private JScrollPane scrollPane = new JScrollPane();
private JLabel l = new JLabel();
private Point location;
private Timer timer1;
public CaptureScreen() {
capture.setActionCommand("CaptureScreen");
capture.setFocusPainted(false);
capture.addActionListener(this);
capture.setPreferredSize(new Dimension(300, 50));
pane.add(capture);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(pane);
f.setLocation(100, 100);
f.pack();
f.setVisible(true);
createPicContainer();
startTimer();
}
private void createPicContainer() {
l.setPreferredSize(new Dimension(700, 500));
scrollPane = new JScrollPane(l,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBackground(Color.white);
scrollPane.getViewport().setBackground(Color.white);
d.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
d.add(scrollPane);
d.pack();
d.setVisible(false);
d.addWindowListener(new WindowListener() {
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
f.setVisible(true);
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
});
}
private void startTimer() {
timer1 = new Timer(1000, new AbstractAction() {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
capture.doClick();
f.setVisible(false);
}
});
}
});
timer1.setDelay(500);
timer1.setRepeats(false);
timer1.start();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("CaptureScreen")) {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); // gets the screen size
Robot r;
BufferedImage bI;
try {
r = new Robot(); // creates robot not sure exactly how it works
Thread.sleep(1000); // waits 1 second before capture
bI = r.createScreenCapture(new Rectangle(dim)); // tells robot to capture the screen
showPic(bI);
saveImage(bI);
} catch (AWTException e1) {
e1.printStackTrace();
} catch (InterruptedException e2) {
e2.printStackTrace();
}
}
}
private void saveImage(BufferedImage bI) {
try {
ImageIO.write(bI, "JPG", new File("screenShot.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
private void showPic(BufferedImage bI) {
ImageIcon pic = new ImageIcon(bI);
l.setIcon(pic);
l.revalidate();
l.repaint();
d.setVisible(false);
//location = f.getLocationOnScreen();
//int x = location.x;
//int y = location.y;
//d.setLocation(x, y + f.getHeight());
d.setLocation(150, 150);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
d.setVisible(true);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
CaptureScreen cs = new CaptureScreen();
}
});
}
}