Swing worker freezes the application - java

Below is a small program that utilises the Newton-Raphson method for finding out a square root to a high degree of accuracy.
Everything seems to work as expected except for the SwingWorker. When this is run the application freezes up and can't be used until the SwingWorker completes its task. However, I thought the purpose of a SwingWorker was to avoid this.
Can someone help me rectify the problem?
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.concurrent.ExecutionException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.DefaultCaret;
import net.miginfocom.swing.MigLayout;
#SuppressWarnings("serial")
public class SquareRoot extends JFrame {
private BigDecimal SQRT_DIG = new BigDecimal(150);
private SquareRoot() {
super("Square Rooter");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createLayout(this);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void createLayout(JFrame addTo) {
JPanel rootPanel = new JPanel(new MigLayout("wrap", "grow", ""));
JLabel acc = new JLabel("Accuracy: " + SQRT_DIG);
JSlider accSlide = new JSlider(1, 1000000, 150);
JLabel numRoot = new JLabel("Number to Square Root");
JTextField num = new JTextField();
JButton root = new JButton("Root");
JLabel ansRoot = new JLabel("Answer");
JTextArea ans = new JTextArea();
JScrollPane ansHolder = new JScrollPane(ans);
accSlide.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent arg0) {
SQRT_DIG = new BigDecimal(accSlide.getValue());
acc.setText("Accuracy: " + SQRT_DIG);
}
});
rootPanel.add(acc);
rootPanel.add(accSlide, "grow");
root.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
ans.setText("Please Wait");
new SwingWorker<BigDecimal, Object>() {
#Override
protected BigDecimal doInBackground() throws Exception {
return bigSqrt(new BigDecimal(num.getText()));
}
#Override
protected void done() {
try {
ans.setText(get().toPlainString());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}.run();
}
});
rootPanel.add(numRoot);
rootPanel.add(num, "grow");
rootPanel.add(root, "right");
ans.setEditable(false);
((DefaultCaret) ans.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
ansHolder.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
rootPanel.add(ansRoot);
rootPanel.add(ansHolder, "grow");
addTo.add(rootPanel);
}
private BigDecimal sqrtNewtonRaphson(BigDecimal c, BigDecimal xn, BigDecimal precision) {
BigDecimal fx = xn.pow(2).add(c.negate());
BigDecimal fpx = xn.multiply(new BigDecimal(2));
BigDecimal xn1 = fx.divide(fpx,2*SQRT_DIG.intValue(),RoundingMode.HALF_DOWN);
xn1 = xn.add(xn1.negate());
BigDecimal currentSquare = xn1.pow(2);
BigDecimal currentPrecision = currentSquare.subtract(c);
currentPrecision = currentPrecision.abs();
if (currentPrecision.compareTo(precision) <= -1)
return xn1;
return sqrtNewtonRaphson(c, xn1, precision);
}
private BigDecimal bigSqrt(BigDecimal c) {
return sqrtNewtonRaphson(c, new BigDecimal(1), new BigDecimal(1).divide(new BigDecimal(10).pow(SQRT_DIG.intValue())));
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new SquareRoot());
}
}

I believe your issue is due to you using the run() method to executer the SpringWorker. Using execute() instead will ensure the spring worker will be run in a worker thread.

Related

I want a delay in this program

I used 'Thread' and 'TimeUnit' but not know how to use in the following program. I want when WIN+E execute then after some delay of 1 or 2 second next statement run. As, next statement is in for loop so it should run after 2 seconds infinite time (because of infinite for loop). You can see ActionListener line only.
package v;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class V extends JPanel{
private JButton V;
public V() throws AWTException{
Robot r = new Robot();
setBackground(Color.yellow);
setPreferredSize(new Dimension(800,500));
V = new JButton("PUSH");
add(V);
V.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
for (int i=0; i>0; i++) {r.keyPress(KeyEvent.VK_WINDOWS); r.keyPress(KeyEvent.VK_E); r.keyRelease(KeyEvent.VK_WINDOWS); r.keyRelease(KeyEvent.VK_E);}
}
});
}
public static void main(String[] args) throws AWTException {
V panel = new V();
JFrame frame = new JFrame ("V");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
i would go for something like this:
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class V extends JPanel{
private JButton V;
private boolean notstarted=true;
public V() throws AWTException{
Robot r = new Robot();
setBackground(Color.yellow);
setPreferredSize(new Dimension(800,500));
V = new JButton("PUSH");
add(V);
V.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
if(notstarted){
notstarted=false;
new Thread(new Runnable() {
#Override
public void run() {
while (true) {r.keyPress(KeyEvent.VK_WINDOWS);
r.keyPress(KeyEvent.VK_E);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(StreamServer.class.getName()).log(Level.SEVERE, null, ex);
}
r.keyRelease(KeyEvent.VK_WINDOWS);
r.keyRelease(KeyEvent.VK_E);}
}
}).start();
}
}
});
}
public static void main(String[] args) throws AWTException {
V panel = new V();
JFrame frame = new JFrame ("V");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
As you can see i have added a boolean notstarted to control if the function have been accessed previously, so, this way you cant run that more than one and finally i have added a Thread to mitigate the impact the ActionListener could have on the thread that call it.
Anyway there should be beeter ways to achieve what you are looking for.
You can use javafx.animation.PauseTransition
PauseTransition happen = new PauseTransition(Duration.seconds(2));
happen.setOnFinished(e -> {
System.out.println("hello");
happen.playFromStart();
});
happen.play();
Like that. It will print hello every 2 seconds. That however requires you to extend javafx.application.Application and I see that you don't use javafx in your program.
You can use Thread.sleep as the other answer suggests - to sleep each 2000 miliseconds (in your loop).
There is also java.util.Timer and java.util.TimerTask. It allows you to do code every milisecond given in time. There is an excellent video of that here https://www.youtube.com/watch?v=36jbBSQd3eU

Need help resetting JComboBox

So I am trying to make a Fahrenheit to Celsius (or vice versa) conversion program. So it sort of works but I have a drop down menu which I take the value from to check which conversion equation I need. My program isn't quiet finished I only have one equation but It doesn't go back to normal once I've pressed convert. Does anyone know how I can refresh so it goes back to the first option?
package tools;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
public class TempConvert extends JFrame {
private JTextField Value;
private JComboBox Options;
private JLabel FVal;
private JButton Convert;
private static String[] OptionsList = {"","Celsius", "Farinheit"};
String TempVal;
int GetVal;
double C2F;
float F2C;
int GetUnit;
public TempConvert(){
super("Tempurature Converter");
setLayout(new FlowLayout());
FVal = new JLabel();
Convert = new JButton();
Options = new JComboBox(OptionsList);
Value = new JTextField("Insert Temperature here:");
Value.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
TempVal = Value.getText();
GetVal = Integer.parseInt(TempVal);
}
}
);
Options.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event){
if (event.getStateChange()==ItemEvent.SELECTED)
System.out.print(Options.getSelectedIndex());
GetUnit = Options.getSelectedIndex();
}
}
);
Convert.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (GetUnit==1){
double Calc= (GetVal * 1.8);
C2F = (Calc+32);
System.out.println(C2F);
FVal.setText(C2F + " Fahrenheit");
Convert.revalidate();
Convert.repaint();
}
}
}
);
add(Options);
add(Value);
add (Convert);
add(FVal);
}
}

SwingWorker Process() warning

I tried to write a simple test program using Swing, all I want to do is load a text file and display the path of the chosen text file in a text area. I keep getting a warning on the process method "never used locally" and it is not appending any text to the textbox. Maybe I'm misunderstanding something, I hope someone can help me.
code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MyPanel3 extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea jcomp;
private JButton btn;
private String testfile;
public MyPanel3() {
//construct components
jcomp = new JTextArea (1, 1);
jcomp.setBorder(BorderFactory.createDashedBorder(Color.BLACK));
btn = new JButton ("open");
//adjust size and set layout
setPreferredSize (new Dimension (944, 575));
BoxLayout layout = new BoxLayout (this, BoxLayout.Y_AXIS);
setLayout(layout);
//add main components
add (jcomp);
add (btn);
new SwingWorker<Void, String>(){
protected Void doInBackground(){
//do processes...
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
final JFileChooser chooseFile = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt","txt");
chooseFile.setFileFilter(filter);
chooseFile.setAcceptAllFileFilterUsed(false);
chooseFile.setMultiSelectionEnabled(true);
if(ae.getSource().equals(btn))
{
System.out.println("do in background running");
int returnVal = chooseFile.showOpenDialog(MyPanel3.this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File[] files = chooseFile.getSelectedFiles();
testfile = files[0].getPath();
publish(testfile);
}
}
}
});
return null;
}
protected void process(String s) {
jcomp.append(s);
}
protected void done() {
try
{
//System.out.println("The operation was completed");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}.execute();
}
public static void main(String[] args){
JFrame frame = new JFrame ("MyTest");
frame.getContentPane();
frame.add(new MyPanel3());
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible (true);
}
}
warning reads:
The method process(String) from the type new
SwingWorker(){} is never used locally
EDIT:
with help from MadProgrammer, program is now working (selecting 3 files and printing the paths as strings in the text box)
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MyPanel4 extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea jcomp;
private JButton btn;
public MyPanel4() {
//construct components
jcomp = new JTextArea(1, 1);
jcomp.setBorder(BorderFactory.createDashedBorder(Color.BLACK));
btn = new JButton("open");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
final JFileChooser chooseFile = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt", "txt");
chooseFile.setFileFilter(filter);
chooseFile.setAcceptAllFileFilterUsed(false);
chooseFile.setMultiSelectionEnabled(true);
int returnVal = chooseFile.showOpenDialog(MyPanel4.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
final File[] files = chooseFile.getSelectedFiles();
new SwingWorker<Void, String>() {
private String testfile1 = files[0].getPath();
private String testfile2 = files[1].getPath();
private String testfile3 = files[2].getPath();
protected Void doInBackground() {
List<String> b = new ArrayList<String>();
b.add(testfile1);
b.add(testfile2);
b.add(testfile3);
publish(b.get(0));
publish(b.get(1));
publish(b.get(2));
return null;
}
#Override
protected void process(List<String> chunks) {
for (String pathname : chunks)
{
jcomp.append(pathname + "\n");
}
}
protected void done() {
try
{
System.out.println("Opration Completed");
} catch (Exception e) {
e.printStackTrace();
}
}
}.execute();
}
}
});
//adjust size and set layout
setPreferredSize(new Dimension(944, 575));
BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
setLayout(layout);
//add main components
add(jcomp);
add(btn);
}
public static void main(String[] args) {
JFrame frame = new JFrame("MyTest");
frame.getContentPane();
frame.add(new MyPanel4());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
The SwingWorker should be created inside the buttons actionPerformed method, so that when you click the button, it will run the SwingWorker
You should, also, make sure that all interactions with user interface are fine from within the context of the Event Dispatching Thread. This means you should ask the user to select the file fro within the context of the actionPerformed method and pass the result to the SwingWorked
Updated
Two additional things...
You're not actually reading the file, but simply passing the name of the file to the publish method
SwingWorker defines process as protected void process(List<V> chunks) but you've defined it as protected void process(String s)...mean that you are actually not overriding the SwingWorker's process method, but creating your own...
Take a look at this example to see how you might be able to use a SwingWorker to read a file...
And, update your process to have the corrected method signature...
#Override
protected void process(List<String> chunks) {
for (String line : chunks) {
output.append(line);
}
}
Remember, you should, as much as possible, use the #Override annotation when you think you are overriding a method, the compiler will tell you when you are mistaken, saving you a lot of head scratching...
It should be like this:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MyPanel3 extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea jcomp;
private JButton btn;
private String testfile;
public MyPanel3() {
//construct components
jcomp = new JTextArea(1, 1);
jcomp.setBorder(BorderFactory.createDashedBorder(Color.BLACK));
btn = new JButton("open");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
final JFileChooser chooseFile = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt", "txt");
chooseFile.setFileFilter(filter);
chooseFile.setAcceptAllFileFilterUsed(false);
chooseFile.setMultiSelectionEnabled(true);
int returnVal = chooseFile.showOpenDialog(MyPanel3.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File[] files = chooseFile.getSelectedFiles();
testfile = files[0].getPath();
new SwingWorker<Void, String>() {
protected Void doInBackground() {
publish(testfile);
return null;
}
protected void process(String s) {
jcomp.append(s);
}
protected void done() {
try {
//System.out.println("The operation was completed");
} catch (Exception e) {
e.printStackTrace();
}
}
}.execute();
}
}
});
//adjust size and set layout
setPreferredSize(new Dimension(944, 575));
BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
setLayout(layout);
//add main components
add(jcomp);
add(btn);
}
public static void main(String[] args) {
JFrame frame = new JFrame("MyTest");
frame.getContentPane();
frame.add(new MyPanel3());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
You are running worker when you are creating panel. But you should run it when button is clicked.

JFormattedTextfield only validate when pressed enter

For a task I need to make a JFormattedTextField with the following behavior:
If value is edited and isn't equal to the last validated value the background must become yellow.
Value validation may take place at any time
If focus is lost nothing should happen (if background is yellow it should remain yellow,...)
Action should be taken when Enter is pressed
I can't seem to find the correct combination of Listeners to accomplish this. I tried using KeyAdapter, InputVerifier and PropertyChangeListenerbut that gives me very ugly code wich only works for 80%.
How should this be done?
Edit: I wrote a small example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import javax.swing.AbstractAction;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JPanel {
private JFormattedTextField field;
private JLabel label;
private JButton btn;
public Test() {
super(new BorderLayout());
label = new JLabel("Enter a float value:");
btn = new JButton(new AbstractAction("Print to stdout"){
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(field.getValue());
}
});
field = new JFormattedTextField(new Float(9.81));
field.addKeyListener(new KeyAdapter(){
#Override
public void keyPressed(KeyEvent e){
field.setBackground(Color.YELLOW);
}
#Override
public void keyTyped(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER){
try{
field.commitEdit();
field.setBackground(Color.WHITE);
}catch(ParseException e1){
field.setBackground(Color.RED);
}
}
}
});
field.setInputVerifier(new InputVerifier(){
#Override
public boolean verify(JComponent comp) {
try{
field.commitEdit();
field.setBackground(Color.YELLOW);
return true;
}catch(ParseException e){
field.setBackground(Color.RED);
return false;
}
}
});
add(label, BorderLayout.NORTH);
add(field, BorderLayout.CENTER);
add(btn, BorderLayout.SOUTH);
}
public static void main(String[] args) {
JFrame window = new JFrame("InputVerifier test program");
Container cp = window.getContentPane();
cp.add(new Test());
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
This almost does everything I want. But the problem is the ENTER key is never caught. I think it is consumed before it reaches my KeyListener, but how can I prevent this?
Even if this can be prevented, I still have the feeling there should be a cleaner why to accomplish what above code does.
Try your hands on this code sample, tell me is this the desired behaviour, or you expecting something else, other than this :
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class JFormattedExample
{
private String lastValidValue;
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("JFormattedTextField Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
final JFormattedTextField ftf = new JFormattedTextField(
NumberFormat.getNumberInstance());
ftf.setColumns(10);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
ftf.setValue(100);
lastValidValue = "100";
ftf.addCaretListener(new CaretListener()
{
public void caretUpdate(CaretEvent ce)
{
System.out.println("Last Valid Value : " + lastValidValue);
if (ftf.isEditValid())
{
String latestValue = ftf.getText();
System.out.println("Latest Value : " + latestValue);
if (!(latestValue.equals(lastValidValue)))
ftf.setBackground(Color.YELLOW.darker());
else
{
lastValidValue = ftf.getText();
ftf.setBackground(Color.WHITE);
}
}
else
{
System.out.println("Invalid Edit Entered.");
}
}
});
contentPane.add(ftf);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new JFormattedExample().createAndDisplayGUI();
}
});
}
}

more efficient layout than Box

I've got some very old code which uses a Box to list some information. I create it like so:
Box patterns = Box.createVerticalBox();
Very (very) often, new items are added and old items are removed eg:
label = new JLabel("xyz");
patterns.add(label);
and later
patterns.remove(label);
whenever something is added ore removed I have to have it repaint, so I call:
patterns.revalidate();
patterns.repaint();
Problem is, since this happens very often it chokes up the UI. I think I need a better implementation in order to make it more efficient.
I know I could maintain a list of the active items in the background and then intermittently update the actual UI (batch update) but...
Can someone suggest a more efficient alternative approach?
Why don't you just use a JList and implement a cell renderer?
Or more flexibility with a JTable and implement a table cell renderer (returns a Component instead)?
Based on this example, the following code loafs doing 16 labels at 10 Hz.
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
/** #see https://stackoverflow.com/questions/6605554 */
public class ImageLabelPanel extends Box implements ActionListener {
private static final int N = 16;
private final List<JLabel> list = new ArrayList<JLabel>();
private final Timer timer = new Timer(100, this);
ImageLabelPanel() {
super(BoxLayout.Y_AXIS);
BufferedImage bi = null;
try {
bi = ImageIO.read(new File("image.jpg"));
} catch (IOException e) {
e.printStackTrace(System.err);
}
for (int r = 0; r < N; r++) {
int w = bi.getWidth();
int h = bi.getHeight() / N;
BufferedImage b = bi.getSubimage(0, r * h, w, h);
list.add(new JLabel(new ImageIcon(b)));
}
createPane();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setVisible(true);
timer.start();
}
private void createPane() {
this.removeAll();
for (JLabel label : list) {
add(label);
}
this.revalidate();
}
#Override
public void actionPerformed(ActionEvent e) {
Collections.shuffle(list);
createPane();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ImageLabelPanel();
}
});
}
}

Categories

Resources