Smooth motion for JPanel and update JLabel at same time - java

How to move with smooth motion for JPanel and update JLabel at same time?
I want to show current time on a JFrame so I created a new java.util.Timer and update to label every one second.
I created another Java thread to as well, move the panel component.
But while moving the panel and showing (updating) time on the frame, panel refreshing to form original position.
So I search that problem in Google and can't find the solution.
//Code to move jPanel smoothly
Thread t = new Thread(){
int i = 0 ;
public void run(){
while(i<150){
i++;
jPanel2.setLocation(i, jPanel2.getY());
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
}
}
};
t.start();
// Code to show Time
Timer t = new javax.swing.Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jLabel1.setText(new Date()+"");
}
});
t.start();

Here is a small example, how to provide animation and update for a component.
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.WindowConstants;
/**
* <code>MovedClock</code>.
*/
public class MovedClock {
private final JLabel clock = new JLabel();
private final DateTimeFormatter format = DateTimeFormatter.ofPattern("HH:mm:ss");
private void startUI() {
JFrame frame = new JFrame("Moved clock");
frame.setLayout(null); // usually it's a bad idea, but for animation we need this.
clock.setBounds(0, 50, 50, 20);
frame.add(clock);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
updateClock();
Timer clockTimer = new Timer(1000, e -> updateClock());
clockTimer.start();
// 15 milliseconds for about 60fps
Timer moveTimer = new Timer(15, new ActionListener() {
private int count = 1;
private int increment = 1;
#Override
public void actionPerformed(ActionEvent e) {
if (count == 435 || count == 0) {
increment = -increment;
}
Point loc = clock.getLocation();
loc.x += increment;
clock.setLocation(loc);
count += increment;
}
});
moveTimer.start();
}
private void updateClock() {
clock.setText(LocalTime.now().format(format));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new MovedClock()::startUI);
}
}

Related

How can the mouse listener activate after a certain time when the mouse is already over the button?

I'm trying to have a border appear over the react button after the timer has ended. I can't seem to have that happen unless I move the mouse away from the trigger button and then back onto it. Is there a way to activate the mouse listener without moving the mouse off and then back on the trigger button after the timer? And please dont't say: set border when timer ends, because that's not what I'm looking for.
Also, feel free to point out other mistakes or bad habits with my code as well. I'm new to Java coding.
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ButtonTester{
public static final Border PANEL_BORDER = new LineBorder(Color.red, 12);
public static JPanel panel;
public static JButton trigger;
public static JButton react;
public static JLabel msg;
public static void main(String [] args){
JFrame frame = new JFrame();
frame.setSize(new Dimension(500,200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setBorder(PANEL_BORDER);
frame.getContentPane().add(panel);
JButton trigger = new JButton("Trigger");
JButton react = new JButton("React");
JLabel msg = new JLabel();
react.setPreferredSize(new Dimension(200, 60));
trigger.setPreferredSize(new Dimension(200, 60));
panel.add(trigger);
panel.add(react);
panel.add(msg);
panel.setVisible(true);
frame.setVisible(true);
MouseListener mL = new MouseAdapter(){
#Override public void mouseEntered(MouseEvent evt) {
react.setBorder(PANEL_BORDER);
}
#Override public void mouseExited(MouseEvent evt) {
react.setBorder(javax.swing.BorderFactory.createEmptyBorder());
}
};
countDown(msg, trigger, mL);
}
public static void countDown(JLabel msg, JButton trigger, MouseListener mL){
Timer timer = new Timer();
TimerTask task = new TimerTask(){
short seconds = 4;
public void run(){
if(seconds == 0){
timer.cancel();
trigger.addMouseListener(mL);
return;
}
seconds--;
msg.setText("Attempting to add listener in : "+seconds);
}
};
timer.scheduleAtFixedRate(task, 1000, 1000);
}
}
Okay, this example sets up two state variables, one to determine if the mouse has entered or exited the button and one which determines if the timer has completed or not.
If these two conditions are true, then the border is set.
This means that the border of the react button WON'T be changed if the mouse is NOT over the trigger button when the timer runs out, but if the user moves back into the button, it will be changed. It will also be changed in the mouse is over the trigger button AND the timer runs out
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
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 static class TestPane extends JPanel {
public static final Border PANEL_BORDER = new LineBorder(Color.red, 12);
private boolean mouseInTheHouse = false;
private boolean timedOut = false;
private JButton react;
private JButton trigger;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.ipadx = 200;
gbc.ipady = 60;
gbc.gridwidth = GridBagConstraints.REMAINDER;
react = new JButton("React");
trigger = new JButton("Trigger");
add(react, gbc);
add(trigger, gbc);
trigger.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
mouseInTheHouse = true;
stateChanged();
}
#Override
public void mouseExited(MouseEvent e) {
mouseInTheHouse = false;
}
});
Timer timer = new Timer(4000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
timedOut = true;
System.out.println("!!");
stateChanged();
}
});
timer.start();
}
protected void stateChanged() {
if (mouseInTheHouse && timedOut) {
react.setBorder(PANEL_BORDER);
}
}
}
}
Note, that I've not setup a condition for what should happen when the mouse leaves the trigger button, but I assume you'd reset the border.
I see. I have an additional question. What if I had 10 trigger buttons (top of the panel) and 10 react button (bottom of the panel)? The condition is: If I have my mouse over one of the trigger button, then the corresponding react button of the same position plus the react button to the right side of that react button will have borders. How do I detect that without looping through my button List and detecting mouseInHouse?
Basically, distill the idea down to it's most common level. You have two buttons, a Timer, MouseListener and two state variables. Wrap these up into a common class which you can then reuse.
public class ButtonStateManager {
private boolean mouseInTheHouse = false;
private boolean timedOut = false;
private JButton trigger;
private JButton react;
private Timer timer;
public ButtonStateManager(JButton trigger, JButton react, int timeOut) {
this.trigger = trigger;
this.react = react;
trigger.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
mouseInTheHouse = true;
stateChanged();
}
#Override
public void mouseExited(MouseEvent e) {
mouseInTheHouse = false;
}
});
Timer timer = new Timer(timeOut, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
timedOut = true;
stateChanged();
}
});
}
protected void stateChanged() {
if (mouseInTheHouse && timedOut) {
react.setBorder(TestPane.PANEL_BORDER);
}
}
}
Now, this assumes there's a relationship between the two buttons.
If your mouse is on the trigger button, and then MouseListener is added to the button, it cannot capture the previously happened event of entering the trigger button's area.
If you insist on showing the border when your mouse is on the trigger button without getting off and on it again, you should call the mouseEnter method manually after you add the mouseListener to it:
(before that you should pass the final JFrame frame to your countDown method)
if(seconds == 0){
timer.cancel();
trigger.addMouseListener(mL);
Point mousePosition = MouseInfo.getPointerInfo().getLocation();
Rectangle triggerRect = trigger.getBounds();
Rectangle frameRect = frame.getBounds();
Rectangle newRect = new Rectangle(triggerRect.x + frameRect.x, triggerRect.y + frameRect.y, triggerRect.width, triggerRect.height);
if(newRect.contains(mousePosition)) {
mL.mouseEntered(new MouseEvent(trigger, 0, System.currentTimeMillis(), 1, 0, 0, 0, false));
}
return;
}
But as MadProgrammer mentioned your question is not clear by saying "I'm trying to have the react button appear with a border after the timer has ended".
Hope this helps!

Use windowFocusListener on a countdown timer

I'm trying to make a countdown timer that only run when the window is on top of my screen.
I tried with this :
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TimerVisible extends JFrame implements WindowFocusListener{
static TimerVisible frame = new TimerVisible("chrono",2,1,3);//I set a random time
JTextArea display;
private Counter counter;
public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPane();
frame.pack();
frame.setVisible(true);
}
private void addComponentsToPane() {
display = new JTextArea();
display.setEditable(true);
JScrollPane scrollPane = new JScrollPane(display);
scrollPane.setPreferredSize(new Dimension(500, 450));
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowFocusListener(this);
}
public TimerVisible(String name, int hours, int minutes, int secondes) {
super(name);
counter=new Counter(hours, minutes, secondes); //Counter is in secondes but is created with hours, minutes and seconds
}
public void windowGainedFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowGainFocus.");
try{
while(counter.getCounter()!=0){
Thread.sleep(1000);
displayMessage(counter.toString());
counter.decrement();
}
}
catch(InterruptedException exc){
System.exit(-1);
}
}
public void windowLostFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowLostFocus.");
}
private void displayMessage(String msg) {
display.append(msg+"\n");
System.out.println(msg);
}
}
When I run this program, it display the messages and the countdown on my terminal and not the window, but if I set the while loop under comment, it display correctly the message on the window. Is anybody got an idea why I got this difference?
Thank you
Your while loop is running on the Swing event thread, blocking it and preventing it from painting to the GUI or interacting with the user. Use a Swing Timer instead. Note that with a Swing Timer you won't have a while loop, but instead the actionPerformed will be called repeatedly until you stop the Timer.
Something like this could be close to working (code not tested)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class TimerVisible extends JFrame implements WindowFocusListener{
private static final int TIMER_DELAY = 1000;
static TimerVisible frame = new TimerVisible("chrono",2,1,3);//I set a random time
JTextArea display;
private Counter counter;
Timer timer = null;
public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPane();
frame.pack();
frame.setVisible(true);
}
private void addComponentsToPane() {
display = new JTextArea();
display.setEditable(true);
JScrollPane scrollPane = new JScrollPane(display);
scrollPane.setPreferredSize(new Dimension(500, 450));
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowFocusListener(this);
}
public TimerVisible(String name, int hours, int minutes, int secondes) {
super(name);
counter=new Counter(hours, minutes, secondes); //Counter is in secondes but is created with hours, minutes and seconds
}
public void windowGainedFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowGainFocus.");
if (timer != null && timer.isRunning()) {
return;
}
timer = new Timer(TIMER_DELAY, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (counter.getCounter() <= 0) {
timer.stop();
} else {
displayMessage(counter.toString());
counter.decrement();
}
}
});
timer.start();
}
public void windowLostFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowLostFocus.");
}
private void displayMessage(String msg) {
display.append(msg+"\n");
System.out.println(msg);
}
}

Need help to switch between to pictures in stickman game

I'm trying to make a simple stickman game where the stickman runs. I've added 2 pictures; 1 where he's standing and 1 where he's running. Have made a JFrame and have tried to switch between the pictures in it by adding the first pic then remove it and add the another.
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Spil extends JFrame {
private static String path_for_image = "index.jpeg";
private static String path2_for_image = "run.jpeg";
#SuppressWarnings("deprecation")
public void run(){
JFrame frame = new JFrame("STICKMAN");
frame.setVisible(true);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon(path_for_image);
JLabel label = new JLabel(image);
ImageIcon image2 = new ImageIcon(path2_for_image);
JLabel label2 = new JLabel(image2);
while(true){
add(label);
label.move(10, 0);
if(label.isEnabled()){
remove(label);
add(label2);
} else {
remove(label2);
add(label);
}
}
}
}
^^Here I make the JFrame and the code below is my main class:
EDIT:
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Spil extends JFrame {
private static String path_for_image = "index.jpeg";
private static String path2_for_image = "run.jpeg";
#SuppressWarnings("deprecation")
public void run(){
JFrame frame = new JFrame("STICKMAN");
frame.setVisible(true);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon(path_for_image);
JLabel label = new JLabel(image);
ImageIcon image2 = new ImageIcon(path2_for_image);
JLabel label2 = new JLabel(image2);
while(true){
frame.add(label);
label.move(10, 0);
if(label.isEnabled()){
frame.remove(label);
frame.add(label2);
} else {
frame.remove(label2);
frame.add(label);
}
}
}
}
public class Main {
public static void main(String[] args){
Spil run = new Spil();
run.run();
}
}
The problem is that the JFrame appears but without the pictures
use swing timer
there are bunch of problems in your code
1) add(label); should be frame.add(label); you have extends your class with frame .but you have created frame local varible and used it.so add will add to your class/jframe instead of frame.so your frame don't have a lable.
either you can remove extends keyword or you can use your Spil class as a jframe then you don't need to create a another frame variable.
JFrame frame = new JFrame("STICKMAN");) .see example 2 how to do using extends jframe
2) label.isEnabled() always true so you never execute else block.to isEnable be false it should be disabled.
3) removing and adding jlables is really inefficient you can easily change image icon.
4) infinite loop will block the Edt and freez your gui.swing timer will handle this without blocking EDT
5) animation need a time gap if you do very fast it really doesn't look nice.in this example i have set time gap to 10 millisecond. you can change speed of your animation by changing value 10.
swing timer is easy to use and perfect for your requirement.
example 1 without extends JFrame
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class Spil {
private static final ImageIcon img1 = new ImageIcon("index.jpeg");
private static final ImageIcon img2 = new ImageIcon("run.jpeg");
boolean bool = false;
public void run() {
JFrame frame = new JFrame("STICKMAN");
frame.setVisible(true);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel(img1);
frame.add(label);
Timer t = new Timer(10, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (bool) {
label.setIcon(img1);
} else {
label.setIcon(img2);
}
bool=!bool;
}
});
t.start();
}
public static void main(String[] args) {
Spil run = new Spil();
run.run();
}
}
example 2 / extends Jframe
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class Spil extends JFrame {
private static final ImageIcon img1 = new ImageIcon("index.jpeg");
private static final ImageIcon img2 = new ImageIcon("run.jpeg");
boolean bool = false;
public void run() {
super.setTitle("STICKMAN");
JLabel label = new JLabel(img1);
add(label);
Timer t = new Timer(10, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (bool) {
label.setIcon(img1);
} else {
label.setIcon(img2);
}
bool = !bool;
}
});
t.start();
}
public static void main(String[] args) {
Spil run = new Spil();
run.setSize(400,300);
run.setVisible(true);
run.run();
}
}

Can't understand Java Swing Timers. How do I make a 1 time delay?

I need a one-time pause in this program for what I'm trying to do. I display some text in a Java Swing JFrame, repaint shows it, I wait 1.5 sec, then I change the text.
Basically, I started with this:
statusLabel.setText(s);
appFrame.repaint();
Thread.sleep(1500);
statusLabel.setText(y);
appFrame.repaint();
But this wasn't working. Thread.sleep() would invoke before repaint had finished, meaning s would never be shown. I read a lot of places that you're not supposed to use Thread.sleep() in swing applications because it pauses all threads, even the threads trying to repaint, and that to pause something triggered by actionPerformed() you need to use a Java Swing Timer.
Which is all well and fine, except I can't find a single place that offers a decent explanation on how they work. Since, as far as I can tell, timers are specifically used for repeating events on a timer. I just want a 1.5 second delay between 2 repaints.
I tried doing this...
statusLabel.setText(s);
appFrame.repaint();
Timer timer = new Timer(1500, new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
}
});
timer.setInitialDelay(1500);
timer.setRepeats(false);
timer.start();
statusLabel.setText(y);
appFrame.repaint();
...adding a timer with a 1.5 sec initial delay, no repeating, and no body to its actionPerformed event, so that it literally does nothing but wait that 1.5 sec, but it didn't work.
As coded in your example, it looks like the timer would "work", it just doesn't do anything because the actionPerformed method is empty. You might be thinking that timer.start() blocks and waits for the timer to trigger, but it fact it returns immediately. The way timers work is that the timer's actionPerformed method will be invoked from the UI thread when it is supposed to be. Placing code inside the actionPerformed method of a timer is a good way to update the UI state periodically.
Have you tried placing statusLabel.setText(y); inside the actionPerformed method of your ActionListener?
statusLabel.setText(s);
Timer timer = new Timer(1500, new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
statusLabel.setText(y);
}
});
timer.setRepeats(false);
timer.start();
If that's still not working, then consider providing a runnable example which demonstrates your problem. This will result in less confusion and better responses
Updated
What you "seem" to be wanting to do, is set up a series of events which get trigger at different times...Rather then using separate Timers, you should be using a single Timer like a loop, each time it ticks, you check it's state and make some decisions about what should be done, for example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 Flashy {
public static void main(String[] args) {
new Flashy();
}
public Flashy() {
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 static class TestPane extends JPanel {
private JLabel flash;
private JButton makeFlash;
protected static final Color[] FLASH_COLORS = new Color[]{Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW};
protected static final int[] FLASH_DELAY = new int[]{1000, 2000, 3000, 4000};
private int flashPoint;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
flash = new JLabel("Flash");
flash.setOpaque(true);
makeFlash = new JButton("Make Flash");
add(flash, gbc);
add(makeFlash, gbc);
makeFlash.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
flashPoint = -1;
Timer timer = new Timer(0, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Timer timer = ((Timer)e.getSource());
flashPoint++;
if (flashPoint < FLASH_COLORS.length) {
flash.setBackground(FLASH_COLORS[flashPoint]);
System.out.println(FLASH_DELAY[flashPoint]);
timer.setDelay(FLASH_DELAY[flashPoint]);
} else {
flash.setBackground(null);
timer.stop();
makeFlash.setEnabled(true);
}
}
});
timer.setInitialDelay(0);
timer.start();
makeFlash.setEnabled(false);
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
Now, if you wanted to do something really fancy, you could devise a series of key frames over a given period of time.
This means that you could change the duration of the animation, without needing to change any other piece of code, for example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
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 Flashy {
public static void main(String[] args) {
new Flashy();
}
public Flashy() {
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 static class TestPane extends JPanel {
private JLabel flash;
private JButton makeFlash;
protected static final Color[] FLASH_COLORS = new Color[]{Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW};
protected static final double[] FLASH_DELAY = new double[]{0, 0.2, 0.4, 0.6};
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
flash = new JLabel("Flash");
flash.setOpaque(true);
makeFlash = new JButton("Make Flash");
add(flash, gbc);
add(makeFlash, gbc);
makeFlash.addActionListener(new ActionListener() {
private int playTime = 10000;
private long startTime;
private int currentFrame = -1;
#Override
public void actionPerformed(ActionEvent e) {
startTime = System.currentTimeMillis();
Timer timer = new Timer(50, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Timer timer = ((Timer) e.getSource());
long now = System.currentTimeMillis();
long duration = now - startTime;
double progress = (double) duration / (double) playTime;
int keyFrame = 0;
for (keyFrame = 0; keyFrame < FLASH_DELAY.length; keyFrame++) {
double current = FLASH_DELAY[keyFrame];
double next = 1d;
if (keyFrame + 1 < FLASH_DELAY.length) {
next = FLASH_DELAY[keyFrame + 1];
}
if (progress >= current && progress < next) {
break;
}
}
if (keyFrame < FLASH_COLORS.length) {
flash.setBackground(FLASH_COLORS[keyFrame]);
}
if (duration >= playTime) {
timer.stop();
makeFlash.setEnabled(true);
flash.setBackground(null);
}
}
});
timer.setInitialDelay(0);
timer.start();
makeFlash.setEnabled(false);
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
A much more advanced concept, which is demonstrated in this answer

JSlider hide after 10seconds as it happens in a TV set

I have made a program where the right and left arrows show the volume on the JSlider decreasing while the Up and Down arrow show the Channel being changed i.e different colours being shown on screen. I wanted that whenever the screen is stable for 10seconds or more, the "Volume is" and "Channel Is" text along with JSlider should disappear, as it happens in a Television Set. I am using Java Eclipse with VisualSwing as my GUI. My current code is:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
public class TVPanel extends JPanel {
private static JLabel vollab;
private int ChannelNo;
private static final long serialVersionUID = 1L;
private JLabel jLabel0;
private int VolumeMax=10;
private JButton jButton0;
private JSlider jSlider0;
private JMenuItem jMenuItem0;
private JPopupMenu jPopupMenu0;
private JLabel jLabel1;
private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
public TVPanel() {
ChannelNo=0;
initComponents();
}
private void initComponents() {
setLayout(new GroupLayout());
add(getJButton0(), new Constraints(new Leading(100, 176, 10, 10), new Leading(39, 72, 10, 10)));
add(getJSlider0(), new Constraints(new Leading(46, 10, 10), new Leading(162, 10, 10)));
add(getJLabel1(), new Constraints(new Leading(111, 10, 10), new Leading(129, 12, 12)));
add(getJLabel0(), new Constraints(new Leading(37, 68, 12, 12), new Leading(129, 12, 12)));
addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent event) {
keyKeyTyped(event);
}
public void keyPressed(KeyEvent event) {
keyKeyPressed(event);
}
});
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
mouseMouseClicked(event);
}
});
setSize(478, 240);
}
private JLabel getJLabel1() {
if (jLabel1 == null) {
jLabel1 = new JLabel();
jLabel1.setText("10");
}
return jLabel1;
}
private JSlider getJSlider0() {
if (jSlider0 == null) {
jSlider0 = new JSlider();
jSlider0.setMajorTickSpacing(1);
jSlider0.setMaximum(10);
jSlider0.setPaintLabels(true);
jSlider0.setPaintTicks(true);
jSlider0.setValue(10);
jSlider0.setAlignmentX(1.0f);
jSlider0.setInheritsPopupMenu(true);
jSlider0.setValueIsAdjusting(true);
}
return jSlider0;
}
private JButton getJButton0() {
if (jButton0 == null) {
jButton0 = new JButton();
jButton0.setText("");
jButton0.setSize(150, 150);
}
return jButton0;
}
private JLabel getJLabel0() {
if (jLabel0 == null) {
jLabel0 = new JLabel();
jLabel0.setText("Volume Is");
}
return jLabel0;
}
private static void installLnF() {
try {
String lnfClassname = PREFERRED_LOOK_AND_FEEL;
if (lnfClassname == null)
lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lnfClassname);
} catch (Exception e) {
System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
+ " on this platform:" + e.getMessage());
}
}
/**
* Main entry of the class.
* Note: This class is only created so that you can easily preview the result at runtime.
* It is not expected to be managed by the designer.
* You can modify it as you like.
*/
public static void main(String[] args) {
installLnF();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("TVPanel");
//JLabel volLab= new JLabel();
vollab= new JLabel("test");
frame.getContentPane().add(vollab);
frame.requestFocus();
frame.isFocusable();
vollab.isVisible();
TVPanel content = new TVPanel();
content.setPreferredSize(content.getSize());
frame.add(content, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
//Arrows
private void keyKeyPressed(KeyEvent event) {
jLabel0.setVisible(true);
jLabel1.setVisible(true);
Color colorarr[]= new Color[] {Color.BLACK,Color.WHITE,Color.BLUE,Color.CYAN,Color.RED,Color.GREEN,Color.GRAY,Color.MAGENTA,Color.ORANGE,Color.YELLOW};
//int Volume=10;
//int ChannelNo=10;
//jLabel0.setText(Integer.toString(event.getKeyCode()));
if(event.getKeyCode()== 37){
VolumeMax--;
jSlider0.setValue(VolumeMax);
jLabel0.setText("Volume Is");
jLabel1.setText(Integer.toString(jSlider0.getValue()));
}
else if(event.getKeyCode()==38)//UP{
{
ChannelNo++;
for(int i=0; i<ChannelNo;i++){
if(i<10){
jButton0.setBackground(colorarr[i]);
jLabel0.setText("Channel Is");
jLabel1.setText(Integer.toString(i+1));
}
}
}
else if(event.getKeyCode()==39){
//RIGHT
VolumeMax++;
jSlider0.setValue(VolumeMax);
jLabel1.setText(Integer.toString(jSlider0.getValue()));
}
else if(event.getKeyCode()==40){
ChannelNo--;
if(ChannelNo>0){
jButton0.setBackground(colorarr[ChannelNo-1]);
jLabel0.setText("Channel Is");
jLabel1.setText(Integer.toString(ChannelNo-1));
}
}
this.requestFocus();
}
private void mouseMouseClicked(MouseEvent event) {
//jLabel0.setText("mouse");
this.requestFocus();
}
//Other keys
private void keyKeyTyped(KeyEvent event) {
if(event.getKeyCode()==37){
//jLabel0.setText("uparrow");
jSlider0.setValue(9);
}
jLabel0.setText("keyType");
this.requestFocus();
}
}
else if(event.getKeyCode()==39){
Never use code with magic numbers. Define static variables if you need to. However, in this case you don't need to since it has already been done for you:
KeyEvent.VK_RIGHT
To have the panel disappear you need to start a Swing Timer to fire in 20 seconds once the panel is displayed. Then whenever a key event or mouse events changes a value on the panel you can restart the Timer.
Read the section from the Swing tutorial on How to Use Timers for more information.
If this is a modal dialog that you are using than maybe you can even use the Application Inacdtivity to help yoo out.

Categories

Resources