How do I start optimising my Java code? - CPU is at 100% - java

i have written an application, but for some reason it keeps peaking at 100%. I ran a profile r on a few of the classes and a report show that isReset() and isRunning() seems to be called alot of times. Do you see anything wrong please inform me. thanks
Class 1 is the only class that uses the isReset() code so i hope this helps u guys in detecting the error
Class 1
package SKA;
/*
* ver 0.32 June 2009
*
* Bug Fix Release:
*
* Fixed Array resize
* Fixed Red Black Tree delete method
* Fixed Red Black Tree save/read option
* Update help file
*
*/
/*
* Additions:
* ver 0.30 May 2009
*
* Added Red Black Tree structure
* Added Delete method for canvases
* Added Array structure
* Added ability to recolor nodes.
* Added Bubble Sort Algorithm
* Added Insertion Sort Algorithm
* Added Shell Sort Algorithm
* Added Selection Sort Algorithm
* Added Quick Sort Algorithm
* Added Red Black Tree Search Algorithm
* Added Black Height Check Algorithm
* Bug fix in canvas - could not delete canvas properly
*/
// Additions:
/* ver 0.25 August 2004
* Added recursion in SkaExecutionPanel by adding SkaFunction
* and using desktop internal panes.
*
* Added binary tree node annotation - text and drawn
* Added subtree highlight feature to VizBinaryTreeNode using SkaRectangle
* Improved node highlighting and selection scheme in VizBinaryTrees/VizDS
* Added Binary tree save and read methods
* Added visual subtree deletion (has bug)
*
* Added ability to set breaks from within algorithm
* Added tooltip messages to SkaProgram/SkaFunction to show variable values
* Added simple value input and output methods to SkaProgram/SkaFunction
* Added SkaTriangle.
* Added Font Adjustment and Color scheme options to show on overhead projectors
*
* Found bug in SkaGraph deleteVertex (with edges)
*/
/* ver 0.16 October 15, 2001
Added Graph save and read methods.
Save is an instance method, while read is a class method.
Added circular layout for graphs,
Added fit/adjust graph layout to plate size method.
Added label editing for Binary Trees and Graphs.
SkaLabels (glyphs) now truncate the string displayed to the width specified
in the constructor.
*/
/* ver 0.15 July 21, 2001
Fixed Reset function in Execution using exceptions so that Ska Algorithms
can be run repeatedly without quitting the entire Ska System.
This also allows trying the same program on different data structures.
Problems with reset so far:
1. Reset message to user can appear much later.
I think this is an I/O sequencing problem and it should go away if
a message status GUI area is used.
2. Bound variable names remain afterwards,
e.g. Graph bound to G will still show name as G after
algorithm is interrupted.
Fixed problem with multiple input requests in 0.14 - by adding another
wait call which waits on before asking for input.
Also introduced trial orderly layout of canvas and program windows ,
which fixes problem in 0.14
*/
/* ver 0.14 July 18, 2001
Added subclasses of SkaProgram, so that multiple programs
can run simultaneously.
Problem - when multiple programs start, their windows overlay each other
Problem - Send DS to algorithm can get confused, if an algorithm
requests input while another is waiting on input or if
two algorithms request input at the same time
*/
/* ver 0.13
Added BinaryTree - does not have node value display yet.
Added arrows on edges of directed graphs
*/
/* ver 0.12
Added VizElementListener - separated from VizElement
Element Input menu item only highlights when input for that DS is requested
DS Input has been cleaned up
*/
/* ver 0.11
can ask user to select individual elements, e.g. vertices
removed standard java cloning code which wasn't being used anyway
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
// TimerQueue
public class SkaTest {
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
public static final int CANVAS_X = 100;
public static final int CANVAS_Y = 100;
public static final int CANVAS_FRAME_WIDTH = WIDTH+100;
public static final int CANVAS_FRAME_HEIGHT = HEIGHT + 100;
public static final int EXEC_WIDTH = 550;
public static final int EXEC_HEIGHT = 400;
static VizDSList dsList = new VizDSList();
static SkaCanvas canvas = new SkaCanvas(dsList);
static JFrame canvasFrame = new JFrame("Data Structure Canvas");
static JMenuBar menuBar = new JMenuBar();
static JMenu algorithmMenu = new JMenu("Algorithm");
static JMenu dsMenu = new JMenu("Create");
static JMenu helpMenu = new JMenu ("Help");
static JLabel status = new JLabel(" ");
static SkaProgram[] alg;
static JFrame execFrame[];
static SkaExecutionPanel execPanel[];
public static void setupFrames(int nAlgs) {
int i;
for (i=0; i < nAlgs; i++) {
// execFrame[i] = new JFrame("Execution Control Panel "+(i+1));
execFrame[i] = new JFrame();
execPanel[i] = new SkaExecutionPanel(execFrame[i]);
}
canvas.setMinimumSize(new Dimension(WIDTH, HEIGHT));
canvasFrame.setSize(CANVAS_FRAME_WIDTH, CANVAS_FRAME_WIDTH);
canvasFrame.getContentPane().setLayout(new BorderLayout(10,7));
// canvasFrame.getContentPane().setPreferredSize(new Dimension(WIDTH, HEIGHT));
canvasFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// canvas.setMinimumSize(new Dimension(WIDTH, HEIGHT));
for (i=0; i < nAlgs; i++) {
execFrame[i].setSize(EXEC_WIDTH, EXEC_HEIGHT);
// execFrame[i].getContentPane().setLayout(new BorderLayout(10,7));
execFrame[i].addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
execPanel[i].setBorder(new SoftBevelBorder(BevelBorder.RAISED));
// execFrame[i].setContentPane(execPanel[i]);
execFrame[i].getContentPane().add("Center", execPanel[i]);
// execFrame[i].setLocation(CANVAS_X +CANVAS_FRAME_WIDTH, CANVAS_Y + i*EXEC_HEIGHT);
execFrame[i].setLocation(CANVAS_X +CANVAS_FRAME_WIDTH + i*30, CANVAS_Y + i*50);
}
canvas.setBorder(new SoftBevelBorder(BevelBorder.RAISED));
canvasFrame.getContentPane().add("Center", new JScrollPane(canvas) );
// canvasFrame.getContentPane().add("Center", new JScrollPane(canvas, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS) );
canvasFrame.getContentPane().add("South", status);
canvasFrame.setLocation(CANVAS_X, CANVAS_Y);
JMenu fileMenu = new JMenu("File");
JMenuItem quitItem = new JMenuItem("Quit");
//TODO Add quit listener
quitItem.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent arg0) {
//System.exit(0);
int again = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit system", "Exiting", JOptionPane.YES_NO_OPTION);
if (again == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
);
fileMenu.add(quitItem);
menuBar.add(fileMenu);
menuBar.add(algorithmMenu);
// menuBar.add(dsMenu);
menuBar.add(helpMenu);
JMenuItem help = new JMenuItem ("Help Contents");
//help.setMnemonic(KeyEvent.VK_H);
//TODO Fix this method
help.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, ActionEvent.CTRL_MASK));
help.addActionListener(new ActionListener()
{
/*
#Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Alot of the functionality have not yet been included in this version\nCurrently working on the automation features now!", "SKA 0.2 Beta", JOptionPane.WARNING_MESSAGE);
}
*/
public void actionPerformed(ActionEvent arg0) {
try {
Runtime.getRuntime().exec("hh.exe C:/ska.chm");
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "File not found", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
JMenuItem about = new JMenuItem ("About SKA");
about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "SKA Version 0.1 Beta");
}
});
helpMenu.add(help);
helpMenu.add(about);
canvasFrame.setJMenuBar(menuBar);
}
/** The create menu item */
public static void createProgram(int i) {
JMenuItem algItem;
switch (i) {
case 0 :
alg[0] = new RedBlackValidate(canvas, execPanel[0]);
execFrame[0].setTitle("Validate Algorithm");
System.out.println("Validate Algorithm");
algItem = new JMenuItem("Validate Algorithm");
algorithmMenu.add(algItem);
break;
/* case 0 :
alg[0] = new BreadthFirstSearch(canvas, execPanel[0]);
execFrame[0].setTitle("BFS Graph Algorithm");
// System.out.println("BreadthFirstSearch");
algItem = new JMenuItem("BFS Graph Algorithm");
algorithmMenu.add(algItem);
break;
case 1:
alg[1] = new LevelOrderAlgorithm(canvas, execPanel[1]);
execFrame[1].setTitle("Level Order Tree Algorithm");
System.out.println("LevelOrderAlgorithm");
algItem = new JMenuItem("Level Order Tree Algorithm");
algorithmMenu.add(algItem);
break;
case 2:
alg[2] = new BinarySearchTreeAlgRecursive(canvas, execPanel[2]);
execFrame[2].setTitle("BinaryTreeSearchRec Algorithm");
System.out.println("BinaryTreeSearchRec Algorithm");
algItem = new JMenuItem("BinaryTreeSearchRec Algorithm");
algorithmMenu.add(algItem);
break;
case 3:
alg[3] = new BinarySearchTreeAlgIterative(canvas, execPanel[3]);
execFrame[3].setTitle("BinaryTreeSearchIter Algorithm");
System.out.println("BinaryTreeSearchIter Algorithm");
algItem = new JMenuItem("BinaryTreeSearchIter Algorithm");
algorithmMenu.add(algItem);
break;
case 4:
alg[4] = new RebBlackTreeSearch (canvas, execPanel[4]);
execFrame[4].setTitle("Red Black Search Algorithm");
System.out.println("Red Black Search Algorithm");
algItem = new JMenuItem("Red Black Search Algoithm Algorithm");
algorithmMenu.add(algItem);
break;
case 5:
alg[5] = new ArrayInsertionSortAlg (canvas, execPanel[5]);
execFrame[5].setTitle("Array Insertion Sort Algorithm");
System.out.println("Array Insertion Sort");
algItem = new JMenuItem("Array Insertion Sort Algorithm");
algorithmMenu.add(algItem);
break;
case 6:
alg[6] = new ArraySelectionSortAlg (canvas, execPanel[6]);
execFrame[6].setTitle("Array Selection Sort Algorithm");
System.out.println("Array SelectionSearch");
algItem = new JMenuItem("Array Selection Sort Algorithm");
algorithmMenu.add(algItem);
break; */
default:
break;
}
}
public static void main(String args[]) {
int i, nAlgs = 1; //nAlgs = 7;
alg = new SkaProgram[nAlgs];
execPanel = new SkaExecutionPanel[nAlgs];
execFrame = new JFrame[nAlgs];
// canvas.setDebugGraphicsOptions(DebugGraphics.BUFFERED_OPTION);
setupFrames(nAlgs);
canvasFrame.setVisible(true);
for (i=0; i < alg.length; i++) {
createProgram(i);
execFrame[i].setVisible(true);
alg[i].start();
alg[i].displayAlgorithm();
}
while (true) {
for (i=0; i < alg.length; i++)
if (execPanel[i].isReset()) {
alg[i].terminate();
createProgram(i);
alg[i].start();
execPanel[i].unreset();
}
}
}
} // End class SkaTest
Class 2
package SKA;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Stack;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
#SuppressWarnings("serial")
public
class SkaExecutionPanel extends JDesktopPane {
public static final int EXEC_WIDTH = SkaTest.EXEC_WIDTH-100;
public static final int EXEC_HEIGHT = SkaTest.EXEC_HEIGHT-50;
boolean run = false, pause = true, step = false, reset = false;
JToolBar toolbar = new JToolBar();
JTextArea textOutputArea = new JTextArea();
SkaProgram prog;
Stack<SkaFunction> functionStack = new Stack<SkaFunction>();
SkaFunction currentFunction = null;
int level = 0, in = 30;
public void doCall(String[] subAlg, String subAlgName) {
doCall(subAlg, subAlgName, false); // make non-icon default
}
public void doCall(String[] subAlg, String subAlgName, boolean iconify) {
if (currentFunction != null)
functionStack.push(currentFunction);
currentFunction = new SkaFunction(this, subAlg, subAlgName, iconify);
add(currentFunction, new Integer(1));
currentFunction.setBounds(level*in,level*in,EXEC_WIDTH, EXEC_HEIGHT);
// currentFunction.setBounds(level*in,level*in,EXEC_WIDTH-(level+1)*in, EXEC_HEIGHT-(level+1)*in);
currentFunction.setVisible(true);
level++;
}
public void doReturn() {
if (currentFunction == null)
return;
if (currentFunction.makeIconWhenDone()) {
getDesktopManager().iconifyFrame(currentFunction);
// currentFunction.setIcon(true);
currentFunction.setIconifiable(true);
}
else
currentFunction.setVisible(false);
currentFunction = (SkaFunction) functionStack.pop();
level--;
}
public void displayAlgorithm(String[] a) {
doCall(a, "main");
}
public void displayAlgorithm(String[] a, String aname) {
doCall(a, aname);
}
public void setControlsEnabled(boolean b) {
toolbar.setEnabled(b);
}
class RunAction extends AbstractAction {
RunAction() {
super("run");
}
public void actionPerformed(ActionEvent e) {
run = true; pause = false; step = false;
}
}
class StepAction extends AbstractAction {
StepAction() {
super("step");
}
public void actionPerformed(ActionEvent e) {
run = false; pause = false; step = true;
}
}
class PauseAction extends AbstractAction {
PauseAction() {
super("pause");
}
public void actionPerformed(ActionEvent e) {
pause = true;
// System.out.print("breaks");
// for (int i=0; i<breaks.length; i++)
// System.out.print("[" +i+ "]=" + breaks[i].toString() + " ");
// System.out.println("");
}
}
class ResetAction extends AbstractAction {
ResetAction() {
super("reset");
putValue(Action.SHORT_DESCRIPTION, "stop program and reset state to begining");
}
public void actionPerformed(ActionEvent e) {
run = false; pause = true; step = false;
// should also restart SkaProgram
reset = true;
if (currentFunction != null) currentFunction.reset();
/*
JInternalFrame[] frames = getAllFrames();
for (int i = 0; i < frames.length; i++) {
// frames[i].dispose();
if (frames[i].isIcon())
frames[i].dispose();
}
*/
}
}
JComboBox speedControl;
String speedNames[] = { "slow", "normal", "fast", "very fast" };
int speeds[] = {4000, 2000, 1000, 500} ; // milliseconds
int speed = speeds[1];
private void initSpeedMenu() {
speedControl = new JComboBox(speedNames);
// speedControl.setMaximumRowCount(3);
speedControl.addItemListener(
new ItemListener() {
public void itemStateChanged( ItemEvent e) {
speed = speeds[speedControl.getSelectedIndex()];
}
}
);
speedControl.setSelectedIndex(1);
speedControl.setMaximumSize(speedControl.getPreferredSize());
speedControl.setToolTipText("execution speed");
}
SkaExecutionPanel(JFrame frame) {
initSpeedMenu();
toolbar.add(new RunAction());
toolbar.add(new StepAction());
toolbar.add(new ResetAction());
toolbar.add(new PauseAction());
toolbar.addSeparator();
toolbar.add(speedControl);
// frame.getContentPane().setLayout(new BorderLayout(10,7));
// makeTable();
frame.getContentPane().add("North", toolbar);
Font f = new Font("SansSerif", Font.BOLD, 14);
textOutputArea.setFont(f);
frame.getContentPane().add("South", new JScrollPane(textOutputArea));
// frame.getContentPane().add(this, BorderLayout.CENTER)
// frame.getContentPane().add("Center", toolbar, BorderLayout.NORTH);
// setSize(300,250);
// add toolbar, table, set layout, sizes
}
// public void setAlgorithm(SkaProgram p) {
// prog = p;
// sendAlgorithm(p.getPseudocode());
// }
public int getSpeed() { return speed;
}
public boolean isRunning() { return run;
}
public boolean isPaused() { return pause;
}
public boolean isStepping() { return step;
}
public boolean isReset() { return reset;
}
public void pause() { pause = true;
}
public void unreset() { reset = false;
}
public void aboutToWait() {
currentFunction.aboutToWait();
}
public void doneWait() {
currentFunction.doneWait();
}
public void showToolTipValue(String s) {
currentFunction.showToolTipValue(s);
}
public void showAlgLineBreak(int i) {
currentFunction.showAlgLineBreak(i);
}
public void showAlgLine(int i) {
currentFunction.showAlgLine(i);
}
public void displayText(String s) {
textOutputArea.append(s); // +"\n" add newline?
}
} // End class SkaExecutionPanel

The problem doesn't seem to be in the methods which are being used a lot, it's how frequently you call them which appears to be why they are showing up so frequently. I'd check for all of the calls, see if any are superfluous then go to the third most time consuming method. After that, I would check my algorithms for being particularly intensive. Also, check all method calls and make sure they're not being run without need.
I know this isn't solving the problem itself, but its a start with the information given.
EDIT: The while loop is an infinite loop causing a for loop to run in which every item in an array has been checked to see if its been reset. You can replace this with an observer pattern where when an object is reset, it notifies the observing object which then performs that set of steps. This way you don't have an infinite loop and you cut down on the usage of .isReset(). This is in the main method of Class 1.
Edit 2: Here's the example of an implementation of the observer pattern which is on wikipedia.

"How do I start optimising my Java code?"
You start by profiling it first.

Start with the most intensively used method in your readout and move to less and less intense methods until you find one with a loop construct (for, while). Check the loop construct to see if it is doing too much work, and see if the functions that call this function call it often.
i.e (pseudocode)
dotProduct(vector a, vector b)
{
//create vector dot product here
maths maths maths
}
calc(data)
{
for obj in data:
dotproduct(obj, foo)
}
dotProduct will use the most CPU time, but calc is the place to start - can we cache results? are we recalculating data? Are we iterating through data in a stupid way?

Judging by the function names "isReset()" and "isRunning()" being called a lot, I would guess that you're wasting CPU time polling for a condition/event. Java should have some sort of semaphore/signalling system that you can use to let the do-er notify the wait-er exactly when the event occurs.

First things first. you have a warning in you code somewhere, as that you have used the annotation
#SuppressWarnings("serial"). While this very well may have nothing to with your cpu maxing out. Fix this first and you very well may stumble on the problem.

Related

Using multi-threading to pause my application does not behave as expected

I am designing a matching memory game, I am almost done with it and everything is working as it should be, however, when the user has opened two different cards the program won't pause(wait) few seconds so the user can see what the second card was.
I have tried using a long for loop operation but encountered the same problem. I have tried Thread.sleep, TimeUnit.SECONDS.sleep, Task and Platform.runLater.
The program opens the card and closes it instantly THEN it waits for the specified duration, keeping in mind that I am calling pauseThread after open and before close functions.
I have tried the above suggestions but they are leading me no where and I can't seem to find where the problem is with my code or where should I place the pauseThread. Thanks in advance.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class MemoryMatchingGame extends Application{
private static Card selectedCard=null; // This is to save a reference for the first card to use in comparison
private static int numOfCorrectPairs = 0; // Keeping track of how many cards the user got correct
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
String[] images = {"C:\\Users\\userName\\Desktop\\Project#4\\1.png", // This is a string array to store images locations
"C:\\Users\\userName\\Desktop\\Project#4\\2.png",
"C:\\Users\\userName\\Desktop\\Project#4\\3.jpg",
"C:\\Users\\userName\\Desktop\\Project#4\\4.jpg",
"C:\\Users\\userName\\Desktop\\Project#4\\5.jpg",
"C:\\Users\\userName\\Desktop\\Project#4\\6.png",
"C:\\Users\\userName\\Desktop\\Project#4\\7.jpg",
"C:\\Users\\userName\\Desktop\\Project#4\\8.jpg"};
ArrayList<Card> listOfCards = new ArrayList<Card>();
for(int i=0; i<images.length; i++) { // This for loop will add each image twice to the array list
listOfCards.add(new Card(images[i]));
listOfCards.add(new Card(images[i]));
}
Collections.shuffle(listOfCards); // Shuffling the deck of cards
primaryStage.setTitle("Memory Matching Game");
HBox hb = new HBox();
VBox firstColoumn = new VBox();
for(int i=0; i<4; i++)
firstColoumn.getChildren().add(listOfCards.get(i));
VBox secondColoumn = new VBox();
for(int i=4; i<8; i++)
secondColoumn.getChildren().add(listOfCards.get(i));
VBox thirdColoumn = new VBox();
for(int i=8; i<12; i++)
thirdColoumn.getChildren().add(listOfCards.get(i));
VBox fourthColoumn = new VBox();
for(int i=12; i<16; i++)
fourthColoumn.getChildren().add(listOfCards.get(i));
hb.getChildren().addAll(firstColoumn, secondColoumn, thirdColoumn, fourthColoumn);
Scene scene = new Scene(hb, 460, 450);
primaryStage.setScene(scene);
primaryStage.show();
}
private class Card extends Button {
private String imageLocation; // To store the destination of the image
private Image img; // To store a reference of the image to be used when setting graphic on a button
public Card(String imageLocation) throws FileNotFoundException {
this.imageLocation = imageLocation;
FileInputStream fis = new FileInputStream(imageLocation);
img = new Image(fis);
setPrefSize(150, 150);
setOnMouseClicked(e -> {
if(isCardOpen()==true)
return; // To ensure no action is made once an image is already opened and the user clicked on it again
if(selectedCard==null) {// This will test if the user has a card open already for comparison or not, if not it will store a reference to the card to use to compare once another card is opened
selectedCard = this;
open();
}
else { // If we enter this statement, this means the user has a card open already and we are ready to perform comparison
open(); // First action taken is to reveal the second card then perform comparison
if(this.isEqual(selectedCard)) {
numOfCorrectPairs++;
System.out.println("Got one");
}
else {
//Get program to pause here
Hold pauseThread = new Hold();
pauseThread.run();
System.out.println("After pausing");
this.close();
selectedCard.close();
}
selectedCard=null; // This will nullify the variable so that we are able to perform comparison again for two other cards
} // End of else statement
}); // End of actionHandler
close(); // This will ensure whenever a card is created it is set face-down
}
private void close() {
setGraphic(null);
}
public void open() {
setGraphic(new ImageView(img));
System.out.println("Open");
}
private boolean isCardOpen() {
return this.getGraphic()!=null;
}
private boolean isEqual(Card selectedCard) {
return this.imageLocation.equals(selectedCard.imageLocation);
}
}
private class Hold extends Thread{
public void run() {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Everything in your code is running in the JavaFX Application Thread. You don't want to pause this thread because it will lock your GUI. As has already been mentioned, you are starting another thread and putting it to sleep, but this doesn't add delay to your GUI that is running in the JavaFX Thread.
An alternative approach would be to use Platform.runLater(). The Hold thread can invoke a method in the JavaFX thread that implements a Platform.runLater() runnable. The runnable is a short lambda that holds the code to close the selected card. The timing may vary slightly from 3000 ms, but you don't have much going on in the JavaFX thread and it doesn't seem critical for this application.
Here are the modifications to try.
First modify the Hold class to include a constructor to pass in the Card object. Then call the closeAfterPause() method on card.
private class Hold extends Thread {
private Card card;
public Hold(Card card) {
this.card = card;
}
public void run() {
try {
TimeUnit.SECONDS.sleep(3);
card.closeAfterPause();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Then create the closeAfterPause() method in the MemoryMatchingGame class.
private void closeAfterPause() {
Platform.runLater(() -> {
System.out.println("After Pausing");
close();
selectedCard.close();
});
}
Then modify the else part of your if-else statement as follows
else {
//Get program to pause here
Hold pauseThread = new Hold(this);
new Thread(pauseThread).start();
}
FX comes with a rich set of Animation/Timeline support - there's no need for falling back onto bare Threads. The most simple form of getting wait-for-xx is to use a Timeline configured with xx and an actionHandler that does something when ready:
Timeline holdTimer = new Timeline(new KeyFrame(
Duration.seconds(2), e -> closeCards()));
Also it's a good idea to centralize all logic outside of a Control. Actually, you should never-ever extend a view for the purpose to include view-unrelated logic. So your long-time goal should be to
extract all single card logic from Card into a CardModel which exposes properties like f.i. image, id, open, disposed
use a plain Button, configure and bind its properties to model properties as appropriate
centralize all game logic like timing, selecting, when opening is allowed, when succeed into a game controller
As I don't want to spoil your fun in doing that - I'll just post a little outline in the direction of the last bullet. Its responsibilities so far
provide api to open a single card
provide api to end a turn: either match or close cards
internals to keep track of opened cards and timing
The snippets just re-mixes your code a bit, moving game logic from the button into the controller (aka: here simply the outer class) and setting the button's action handler to access the controller.
private Card firstCard;
private Card secondCard;
private Timeline holdTimer = new Timeline(new KeyFrame(
Duration.millis(2000), e -> closeCards()));
public void closeCards() {
if (firstCard == null || secondCard == null) {
System.out.println("error!!");
return;
}
if (firstCard.isEqual(secondCard)) {
System.out.println("success");
firstCard.setDisable(true);
secondCard.setDisable(true);
firstCard = null;
secondCard = null;
} else {
firstCard.close();
secondCard.close();
firstCard = null;
secondCard = null;
}
}
public void openCard(Card card) {
if (card.isCardOpen()) return;
if (holdTimer.getStatus() == Status.RUNNING) return;
if (firstCard == null) {
firstCard = card;
firstCard.open();
} else if (secondCard == null) {
secondCard = card;
secondCard.open();
holdTimer.playFromStart();
}
}
// Dont! dont, dont!!! ever extend a Control
//**TBD**: Move open/close state logic into a CardModel
// then configure a plain Button with the properies of that model
private class Card extends Button {
private String imageLocation; // To store the destination of the image
// private Image img; // To store a reference of the image to be used when setting graphic on a button
public Card(String imageLocation) throws FileNotFoundException {
this.imageLocation = imageLocation;
setPrefSize(150, 150);
setOnAction(e -> openCard(this));
}
public void close() {
setText("");
}
public void open() {
setText(imageLocation);
System.out.println("Open");
}
public boolean isCardOpen() {
return getText() != null && getText().length() > 0;//this.getGraphic()!=null;
}
private boolean isEqual(Card selectedCard) {
if (selectedCard == null) return false;
return this.imageLocation.equals(selectedCard.imageLocation);
}
}

Swing getText won't read user input

I've been trying to get the GUI going for the application I'm working on, and, it's been ages since doing anything in Swing. Most of the stuff has come back, but not this. I cannot understand why, across none of the different text components, if I type something into them, getText() will always return "" regardless. It'll return correctly if I use setText() as a test, but that's not really helpful.
This remains consistent across JTextArea/JTextField, various ways of referencing the text field (direct variable vs pulling from the HashMap), and regardless of what kind of thread does the accessing. At times I've used the debugger to try and look at things as they're happening through other test snippets, and still nothing. Since the edit, have trimmed a lot of these cases out, but still persists in the problem.
In all cases, user input is never acquired.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LifePathUI2 {
final static boolean shouldWeightX = true;
private GridBagConstraints cons;
private BorderLayout windowLayout;
private GridBagLayout layout;
private JFrame mainWindow;
private JPanel mainPanel;
private JComponent currentComponent;
private JTextField characterName;
/**
* #throws HeadlessException
*/
public LifePathUI2() throws HeadlessException {
cons = new GridBagConstraints();
windowLayout = new BorderLayout();
layout = new GridBagLayout();
mainWindow = new JFrame();
mainPanel = new JPanel();
mainWindow.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
init();
}
public void init()
{
mainWindow.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
mainWindow.setLayout(windowLayout);
cons.ipadx = 5;
cons.ipady = 5;
cons.anchor = GridBagConstraints.NORTHWEST;
cons.fill = GridBagConstraints.NONE;
cons.weighty = 1.0;
cons.weightx = 1.0;
// to make everything work right we add a mainPanel under the mainWindow
cons.gridheight = 1;
cons.gridwidth = 1;
mainWindow.add(mainPanel);
// Readers keep in mind if you don't set this below, strange behavior happens
mainPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
mainPanel.setLayout(layout);
currentComponent = mainPanel;
addLabel(0,0,"Character Name");
characterName = addTextF(1,0,"",30,true);
endRow(2,0);
addButton(0,1,"Foo").addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println(characterName.getText());
}
});
endVertical(0,2);
mainWindow.setSize(1400, 900);
mainWindow.setVisible(true);
}
/**
* Inserts a spacer to signify the end of components for this row (effects layout so it doesn't center)
*/
private void endRow(int x, int y)
{
cons.weightx = 100.0;
addLabel(x,y,"");
cons.weightx = 1.0;
}
/**
* Inserts a spacer to signify the end of components vertically (effects layout so it doesn't center)
*/
private void endVertical(int x, int y)
{
cons.weighty = 100.0;
addLabel(x,y,"");
cons.weighty = 1.0;
}
/**
* Shorthand command to add label at coordinates with text
* #param x non-negative integer
* #param y non-negative integer
* #param text Display Text for label
*/
private JLabel addLabel(int x, int y, String text)
{
JLabel temp = new JLabel(text);
addC(temp,x,y);
return temp;
}
/**
* Shorthand command to add Button at coordinates with text
* #param x non-negative integer
* #param y non-negative integer
* #param text Display Text for Button
* #return The component created
*/
private JButton addButton(int x, int y, String text)
{
JButton temp = new JButton(text);
addC(temp,x,y);
return temp;
}
/**
* Shorthand command to add Text Field at coordinates with text
* #param x non-negative integer
* #param y non-negative integer
* #param value the default value for the text field
* #param cols Number of Columns
* #param updateListen If true will add listener that triggers UI updates when values change
* #return The component created
*/
private JTextField addTextF(int x, int y, String value, int cols, boolean updateListen)
{
JTextField temp = new JTextField(value, cols);
// this prevents the common issue of the text fields turning into slits
temp.setMinimumSize(temp.getPreferredSize());
addC(temp,x,y);
return temp;
}
/**
* Shorthand to add new components to the UI tab at given coords
* #param comp Component to add to UI
* #param x non-negative integer
* #param y non-negative integer
*
*/
private void addC(JComponent comp, int x, int y) {
cons.gridx = x;
cons.gridy = y;
currentComponent.add(comp,cons);
}
/**
* #param args
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
LifePathUI2 ui = new LifePathUI2();
ui.init();
}
});
}
}
I've searched every question here for swing stuff and none of them problems seem to correspond to this problem specifically. Was not entirely expecting that.
I apologize for the bigger snippet but since all of the parts immediately around creating the text fields, reading them, creating the GUI itself, all seem to match the examples online I've seen... I'm just smart enough to know there must be something odd I did here and just not seeing it.
EDIT: Simplifying example. Despite breaking this down to a much more simple test case, still no dice
EDIT2: Finished compression of things. I was actually expecting this to work because this is on the level of working code I've written before. But still nothing
A wild guess, but your update() method looks like it might be trying to extract the text from the Document that its listening to (you don't say), and if so, then use the DocumentEvent to get the Document and then extract the text. Something like:
private class TextChangeListener implements DocumentListener {
#Override
public void changedUpdate(DocumentEvent e) {
update(e);
}
#Override
public void insertUpdate(DocumentEvent e) {
update(e);
}
#Override
public void removeUpdate(DocumentEvent e) {
update(e);
}
}
and
private void update(DocumentEvent e) {
String text;
try {
Document doc = e.getDocument();
text = e.getDocument().getText(0, doc.getLength());
// do something with text here! ************
System.out.println(text);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
Other than that, I'm lost in your code. Simplify. Refactor. Simplify some more. Refactor some more. Think smaller classes that can be fully tested in isolation.
Edit
You call init() TWICE, and that's the problem since it means that you create two of everything, one displayed and one not.
You first call it here:
public LifePathUI2() throws HeadlessException {
// ....
init();
}
and then call it again in your main:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
LifePathUI2 ui = new LifePathUI2();
ui.init();
}
});
Call it only once.
Change this:
public LifePathUI2() throws HeadlessException {
cons = new GridBagConstraints();
windowLayout = new BorderLayout();
layout = new GridBagLayout();
mainWindow = new JFrame();
mainPanel = new JPanel();
mainWindow.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
init();
}
to this:
public LifePathUI2() throws HeadlessException {
cons = new GridBagConstraints();
windowLayout = new BorderLayout();
layout = new GridBagLayout();
mainWindow = new JFrame();
mainPanel = new JPanel();
mainWindow.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
// init();
}

JavaFX : Button never sets a graphic

EDIT :
A MCVE version of my code has been made to help debug it. It reproduces my bug. The purpose of my code is doing a Memory game. Which means that when it is your turn, you "open" a card, then another one. If they form a pair, they don't get turned over, they stay open. Otherwise, you turn them back over and try to find a pair on the next turn.
Simply put, the bug is : when you are opening the second card of your turn and both cards don't form a pair, the second one never gets opened!
Hopefully, this version of my code will help you to find the bug, which will help me a lot!
I have put the code on Github : https://gist.github.com/anonymous/e866671d80384ae53b53
(And you will find it attached at the end of the question)
Explanation of the issue
I am having fun on doing a little Memory game in JavaFX and I came across this strange behavior where the card I click on (represented by a custom class that extends the Button class) never changes the image displayed.
Normally, when I click on the card, it "opens" itself by changing the graphic it displays.
The strange and annoying thing is that it only happens in a specific case.
The behavior of my card is correct when I "open" the first card of the turn of the player. It also works when I "open" a second one and both cards are a pair. Sadly, it doesn't work only in the case where I want to open a second card and it doesn't match as a pair with the first one.
I modified the Button class by adding openCard() and closeCard() methods. Those methods will set a specific graphic on the button-card.
I will now show some code but it is hard to tell what might be the part that is making this behavior happen. Even more so that I am using Eclipse but can't possibly figure out how to debug a JavaFX app with breakpoints (I am using console prints) because the app will eventually crash when I reach my breakpoints and start crawling through the lines of code.
The code
Firstly, the modified Button class :
public class Card extends Button{
private String cardDesign;
public Card(int row, int column){
this.setGraphic(new ImageView("/resources/card_back.png"));
this.setBackground(new Background(new BackgroundFill(Color.SLATEGRAY,
new CornerRadii(6), null)));
}
public void setOpenCardDesign(String design){ cardDesign = design; }
public void openCard(){ this.setGraphic(new ImageView(cardDesign)); }
public void closeCard(){
this.setGraphic(new ImageView("/resources/card_back.png"));
}
}
Now the controller class, the event is set on a MouseEvent. There is more code in this controller (like checking if there is a pair), but this isn't an issue here I think as the problem is already at the line where I call the method to open the card.
I use the getSource() method here because my cards are arranged in a gridPane and I need to know which one has been clicked on.
#Override
public void handle(MouseEvent event) {
//Get the card that was clicked on
Card card = (Card) event.getSource();
//Open the card
card.openCard();
//Do some more after this...
}
That's pretty much it as from what I could figure out.
As already stated, I tried to check if the method openCard() is being called. It is as some comment printed in my console showed up. I even added some console printing just before and just after the line where I set the graphic and they both are showing up. I can't know for sure what happens when my app reaches the setGraphic() line as nothing is showing up in my app (the card remains closed).
Any hint would help because I am slowly sinking in madness right now.
Thank you in advance.
The MCVE version of my code
The card object : Card.java
package memory;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.paint.Color;
public class Card extends Button{
//-------------------------------------------------
//Store the position of the card
private int row;
private int column;
//-------------------------------------------------
//Constructor
public Card(int row, int column){
//Give the cards a specific color at init
this.setBackground(new Background(new BackgroundFill(Color.DEEPSKYBLUE,
new CornerRadii(6), null)));
this.setText("CLOSED");
this.row = row;
this.column = column;
}
//-------------------------------------------------
//Open the card
public void openCard(){
System.out.println("OPEN");
//Cards are red when open
this.setBackground(new Background(new BackgroundFill(Color.RED,
new CornerRadii(6), null)));
this.setText("OPEN");
}
//-------------------------------------------------
//Close the card
public void closeCard(){
System.out.println("CLOSE");
//Cards are blue when closed
this.setBackground(new Background(new BackgroundFill(Color.DEEPSKYBLUE,
new CornerRadii(6), null)));
this.setText("CLOSED");
}
//-------------------------------------------------
//Getters for row and column info
public int getRow() { return row; }
public int getColumn() { return column; }
}
The main (includes the view and start point of the app) : Main.java
package memory;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
//-------------------------------------------------
//The layout and the cards
GridPane gridCard = new GridPane();
static Card [][] cardArray;
//The event handler
private static EventHandler<MouseEvent> handler;
//The array which remembers the pairs and the reminder of last open card
static int[][] indexArray;
static int index;
//Boolean array to check if the card is already open
static boolean[][] isOpen;
//Number of pairs to find
static int pairs = 5;
//-------------------------------------------------
//Cheap main
public static void main(String[] args) {
Application.launch(args);
}
//-------------------------------------------------
#Override
public void start(Stage primaryStage) throws Exception {
//Init the event handler
handler = new Controller();
//Some formatting for the grid pane
gridCard.setHgap(10);
gridCard.setVgap(10);
gridCard.setPadding(new Insets(0, 10, 0, 10));
gridCard.setAlignment(Pos.CENTER);
//Creating our card board, index array and bool array
cardArray = new Card [2][5];
indexArray = new int [2][5];
isOpen = new boolean [2][5];
//Adding the cards to our card array
for(int i = 0; i < 2; i++){
for(int j = 0; j < 5; j++){
cardArray[i][j] = new Card(i, j);
//Make those buttons look like cards
cardArray[i][j].setPrefHeight(100);
cardArray[i][j].setPrefWidth(70);
//Register the event
cardArray[i][j].addEventHandler(MouseEvent.MOUSE_CLICKED, gameController());
//Add those cards
gridCard.add(cardArray[i][j], j, i);
//Set the pairs (no randomness here)
indexArray[i][j] = j+1;
}
}
//Print out the indexes of all the cards
System.out.println("----------------");
System.out.println("Card indexes :");
for (int i = 0; i < indexArray.length; i++) {
System.out.println();
for (int j = 0; j < indexArray[0].length; j++) {
System.out.print(indexArray[i][j]+ " | ");
}
System.out.println();
}
System.out.println("----------------");
//Set BorderPane
BorderPane root = new BorderPane();
root.setBackground(new Background(new BackgroundFill(Color.BLACK,
CornerRadii.EMPTY, null)));
root.setCenter(gridCard);
//Set the stage
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("Memory Test");
primaryStage.show();
}
//-------------------------------------------------
//Getter for the event handler
public static EventHandler<MouseEvent> gameController() {
return handler;
}
//-------------------------------------------------
//Getter, Setter and "resetter" for the index
public static void resetIndex() { index = 0; }
public static int getIndex() { return index; }
public static void setIndex(int i) {
index = i;
}
//-------------------------------------------------
}
The controller : Controller.java
package memory;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.input.MouseEvent;
public class Controller implements EventHandler<MouseEvent>{
//-------------------------------------------------
#Override
public void handle(MouseEvent event) {
//Get the card which cas clicked on
Card card = (Card) event.getSource();
//If the card was already open, don't do anything
if (!Main.isOpen[card.getRow()][card.getColumn()]) {
//Open the card
card.openCard();
//We opened the first card of the turn
if (Main.getIndex() == 0) {
//Set the card as open
Main.isOpen[card.getRow()][card.getColumn()] = true;
//Remember the index
Main.setIndex(Main.indexArray[card.getRow()][card.getColumn()]);
System.out.println("index: "+Main.getIndex());
//We opened the second card
}else if (Main.getIndex() != 0) {
//Check if it is a pair
if (Main.getIndex() == Main.indexArray[card.getRow()][card.getColumn()]) {
//Decrement the number of pairs
Main.pairs--;
//Open the second card
Main.isOpen[card.getRow()][card.getColumn()] = true;
//Reset the index
Main.resetIndex();
}else{ //Close both cards if it isn't a pair
//Wait 0.7 second to let the player remember the cards
try {
Thread.sleep(700);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Close the current card
card.closeCard();
System.out.println("index : " + Main.indexArray[card.getRow()][card.getColumn()]);
Main.isOpen[card.getRow()][card.getColumn()] = false;
//Close the first opened card by looking at the index
//It closes both cards with the same index, but it doesn't matter
//as the pair hasn't been found anyway
for (int i = 0; i < Main.indexArray.length; i++) {
for (int j = 0; j < Main.indexArray[0].length; j++) {
if (Main.getIndex() == Main.indexArray[i][j]) {
Main.cardArray[i][j].closeCard();
System.out.println("index: " + Main.indexArray[i][j]);
Main.isOpen[i][j] = false;
}
}
}
//Reset the index of last opened card
Main.resetIndex();
}
}
}
//Check endgame
if (Main.pairs == 0) {
//Show a dialog box
Alert incorrectPairs = new Alert(AlertType.INFORMATION);
incorrectPairs.setTitle("GAME OVER");
incorrectPairs.setHeaderText("The game is over");
incorrectPairs.setContentText("You found all the pairs, congrats!");
incorrectPairs.showAndWait();
}
}
//-------------------------------------------------
}
You're blocking the UI thread with Thread.sleep(...). That prevents any pending changes from being repainted, so you don't see the first update at all; you only see the subsequent updates after the pause is complete.
The simplest way to implement a pause on the UI thread is to use a PauseTransition from the JavaFX animation API. Basically, you do
PauseTransition pause = new PauseTransition(Duration.millis(700));
pause.setOnFinished(e -> {
// code to execute after pause...
});
pause.play();
In your case, you probably want to disable the user interface, so the user cannot click on anything during the pause, so you could do something like
PauseTransition pause = new PauseTransition(Duration.millis(700));
pause.setOnFinished(e -> {
card.closeCard();
// ... etc...
card.getParent().setDisable(false);
});
card.getParent().setDisable(true);
pause.play();

Java slideshow image delay using paintComponent

I am putting together a slideshow program that will measure a user's time spent on each slide. The slideshow goes through several different magic tricks. Each trick is shown twice. Interim images are shown between the repetition. Transition images are shown between each trick.
On the first repetition of a trick the JPanel color flashes on the screen after a click before the next image is shown. This doesn't happen during the second repetition of the same trick. It's possible that the image is taking too long to load.
Is there an easy way to pre-load the images so that there isn't a delay the first time through?
NOTE: Original code deleted.
EDIT 1/10/2013: This code now works on slower machines. trashgod's second addendum helped the most. The mouseClick control structure periodically asks SwingWorker classes to load 40 images or less of the current trick while also setting the used images to null. I have simplified my code down for this to just two Image[]s and added a main method so it stands alone. Images are still required to run though. This is now pretty bare bones code, and if you're trying to make a slideshow with a lot of images I think it would be a good place to start.
NOTE: I think I figured out how to properly implement SwingWorker while still using multiple Image[]s. trashgod and kleopatra is this implementation in-line with what you were suggesting? I didn't end up using publish and process since I couldn't figure out how to get that to work appropriately with an indexed array, but because the StringWorker doesn't load all images in the array (only 40), and the code calls StringWorker every 20 images, there should be a pretty good buffer.
EDIT 1/10/2013 Changed out MouseListener by instead extending MouseAdapter on my Mouse class. Also fixed my paintComponent method to include a call to super.paintComponent(g).
Added publish/process methods to my SwingWorker class ImageWorker. Added a wrapper class, ArrayWrapper to allow passing imageArray[i] and its corresponding index int i with publish to process.
package slideshow3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.List;
public class SlideShow3 extends JFrame
{
//screenImage will be replaced with each new slide
private Image screenImage;
private int width;
private int height;
//Create panel for displaying images using paintComponent()
private SlideShow3.PaintPanel mainImagePanel;
//Used for keybinding
private Action escapeAction;
//Image array variables for each trick
private Image[] handCuffs; //h
private Image[] cups; //c
//Used to step through the trick arrays one image at a time
private int h = 0;
private int c = 0;
//Used by timeStamp() for documenting time per slide
private long time0 = 0;
private long time1;
public SlideShow3()
{
super();
//Create instance of each Image array
handCuffs = new Image[50];
cups = new Image[176];
//start(handCuffsString);
start("handCuffs");
try
{
screenImage = ImageIO.read(new File("images/begin1.jpg"));
}
catch (IOException nm)
{
System.out.println("begin");
System.out.println(nm.getMessage());
System.exit(0);
}
/******************************************
* Removes window framing. The next line sets fullscreen mode.
* Once fullscreen is set width and height are determined for the window
******************************************/
this.setUndecorated(true);
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
width = this.getWidth();
height = this.getHeight();
//Mouse click binding to slide advance control structure
addMouseListener(new Mouse());
//Create panel so that I can use key binding which requires JComponent
mainImagePanel = new PaintPanel();
add(mainImagePanel);
/******************************************
* Key Binding
* ESC will exit the slideshow
******************************************/
// Key bound AbstractAction items
escapeAction = new EscapeAction();
// Gets the mainImagePanel InputMap and pairs the key to the action
mainImagePanel.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), "doEscapeAction");
// This line pairs the AbstractAction enterAction to the action "doEnterAction"
mainImagePanel.getActionMap().put("doEscapeAction", escapeAction);
/******************************************
* End Key Binding
******************************************/
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run()
{
SlideShow3 show = new SlideShow3();
show.setVisible(true);
}
});
}
//This method executes a specific SwingWorker class to preload images
public void start(String e)
{
if(e.equals("handCuffs"))
{
new ImageWorker(handCuffs.length, h, e).execute();
}
else if(e.equals("cups"))
{
new ImageWorker(cups.length, c, e).execute();
}
}
//Stretches and displays images in fullscreen window
private class PaintPanel extends JPanel
{
#Override
public void paintComponent(Graphics g)
{
if(screenImage != null)
{
super.paintComponent(g);
g.drawImage(screenImage, 0, 0, width, height, this);
}
}
}
/******************************************
* The following SwingWorker class Pre-loads all necessary images.
******************************************/
private class ArrayWrapper
{
private int i;
private Image image;
public ArrayWrapper(Image image, int i)
{
this.i = i;
this.image = image;
}
public int getIndex()
{
return i;
}
public Image getImage()
{
return image;
}
}
private class ImageWorker extends SwingWorker<Image[], ArrayWrapper>
{
private int currentPosition;
private int arraySize;
private String trickName;
private Image[] imageArray;
public ImageWorker(int arraySize, int currentPosition, String trick)
{
super();
this.currentPosition = currentPosition;
this.arraySize = arraySize;
this.trickName = trick;
}
#Override
public Image[] doInBackground()
{
imageArray = new Image[arraySize];
for(int i = currentPosition; i < currentPosition+40 && i < arraySize; i++)
{
try
{
imageArray[i] = ImageIO.read(new File("images/" + trickName + (i+1) + ".jpg"));
ArrayWrapper wrapArray = new ArrayWrapper(imageArray[i], i);
publish(wrapArray);
}
catch (IOException e)
{
System.out.println(trickName);
System.out.println(e.getMessage());
System.exit(0);
}
}
return imageArray;
}
#Override
public void process(List<ArrayWrapper> chunks)
{
for(ArrayWrapper element: chunks)
{
if(trickName.equals("handCuffs"))
{
handCuffs[element.getIndex()] = element.getImage();
}
else if(trickName.equals("cups"))
{
cups[element.getIndex()] = element.getImage();
}
}
}
#Override
public void done()
{
try
{
if(trickName.equals("handCuffs"))
{
handCuffs = get();
}
else if(trickName.equals("cups"))
{
cups = get();
}
}
catch(InterruptedException ignore){}
catch(java.util.concurrent.ExecutionException e)
{
String why = null;
Throwable cause = e.getCause();
if(cause != null)
{
why = cause.getMessage();
}
else
{
why = e.getMessage();
}
System.err.println("Error retrieving file: " + why);
}
}
}
/******************************************
* End SwingWorker Pre-Loading Classes
******************************************/
//Prints out time spent on each slide
public void timeStamp()
{
time1 = System.currentTimeMillis();
if(time0 != 0)
{
System.out.println(time1 - time0);
}
time0 = System.currentTimeMillis();
}
/******************************************
* User Input Classes for Key Binding Actions and Mouse Click Actions
******************************************/
private class EscapeAction extends AbstractAction
{
#Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public class Mouse extends MouseAdapter
{
#Override
public void mouseClicked(MouseEvent e)
{
if(!(h<handCuffs.length) && !(c<cups.length))
{
timeStamp();
System.exit(0);
}
else if(h<handCuffs.length)
{
timeStamp();
screenImage = handCuffs[h];
repaint();
System.out.print("handCuffs[" + (h+1) + "]\t");
h++;
//purge used slides and refresh slide buffer
if(h == 20 || h == 40)
{
for(int i = 0; i < h; i++)
{
handCuffs[i] = null;
}
start("handCuffs");
}
if(h == 45)
{
start("cups");
}
}
else if(c<cups.length)
{
timeStamp();
screenImage = cups[c];
repaint();
System.out.print("cups[" + (c+1) + "]\t");
c++;
//purge used slides and refresh slide buffer
if(c == 20 || c == 40 || c == 60 || c == 80 || c == 100 || c == 120 || c == 140 || c == 160)
{
for(int i = 0; i < c; i++)
{
cups[i] = null;
}
start("cups");
}
}
}
}
/******************************************
* End User Input Classes for Key Binding Actions and Mouse Click Actions
******************************************/
}
This example uses a List<ImageIcon> as a cache of images returned by getImage(). Using getResource(), the delay is imperceptible. The next and previous buttons are bound to the Space key by default.
Addendum: You can control navigation by conditioning a button's setEnabled() state using an instance of javax.swing.Timer, for example.
Addendum: Your second example waits until the mouse is clicked to begin reading an image, an indeterminate process that may return a copy immediately or may not complete until after repaint(). Instead, begin reading the images in the background using ImageIO.read(), as shown here. You can process() your List<Image> and show progress, as seen here. The SwingWorker can be launched from the initial thread, running while you subsequently build your GUI on the EDT. You can display the first image as soon as it is processed.

JInternalFrame selection

I have a JDesktopPane containing some JInternalFrames. I want some menus on the menubar to be activated only when one of the JInternalFrames is selected. I've tried using VetoableChangeListener, with the following code in it:
JInternalFrame selectedFrame = desk.getSelectedFrame();
if ((selectedFrame != null)) {
imageMenu.setEnabled(Boolean.TRUE);
} else {
imageMenu.setEnabled(Boolean.FALSE);
}
But the results are not what I expected - for example, the menu is enabled only the second time I add a frame. when I close all frames, it remains enabled.
How can I make this work?
you have to read basic tutorial about JInternalFrames with link to the InternalFrameListener,
but another and look like as better way is programatically to know those event in all cases and evety times is by adding PropertyChangeListener as shows examples Getting All Frames in a JDesktopPane Container, by adding PropertyChangeListener you can listeng for these events
Add an InternalFrameListener to each internal frame added to the desktop pane, and each time an event is triggered, execute the code you have shown in your question.
This code could be better written though:
setEnabled takes a primitive boolean as argument, not a java.lang.Boolean. Use true and false rather than Boolean.TRUE and Boolean.FALSE.
The expression (selectedFrame != null) evaluates as a boolean. Just write
imageMenu.setEnabled(selectedFrame != null);
instead of
if ((selectedFrame != null)) {
imageMenu.setEnabled(Boolean.TRUE);
} else {
imageMenu.setEnabled(Boolean.FALSE);
}
I would just create a custom event and fire it when a JInternalFrame gets focus (isActivated).
The menu items would listen for this event, intercept it and set their status enabled or disabled accordingly.
The advantage here is that you don't have to handle what menu items should be available for which types of internal frames, just fire the appropriate event. It'll make your life easier if you add more internal frames in the future.
This answer is based on the answer by #mKorbel. This example shows one of the ways to detect focus between internal frames as is demonstrated here:
package com.apexroot.sandbox;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
/**
* author grants unlimited license to modify, reuse and redistribute. based on
* the suggestion by #mKorbel on stackoverflow at
* http://stackoverflow.com/questions/7219860/jinternalframe-selection
* please keep a URL to the original version in the source code.
* http://javajon.blogspot.com/2015/08/windowfocuslistener-for-jinternalframe.html
*
* #author Apexroot
*/
public class InternalFrameFocusListenerExample {
public static final String INTERNAL_FRAME_FOCUS_EVENT_PROPERTY = "selected";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
final JFrame jFrame = new JFrame();
final JDesktopPane jDesktopPane = new JDesktopPane();
final JInternalFrame[] jInternalFrames = new FocusInternalFrame[3];
for (int i = 0; i < jInternalFrames.length; i++) {
jInternalFrames[i] = new FocusInternalFrame();
}
jFrame.dispose();
jFrame.setContentPane(jDesktopPane);
jDesktopPane.setPreferredSize(new Dimension(400, 200));
jFrame.pack();
jFrame.setVisible(true);
for (int i = 0; i < jInternalFrames.length; i++) {
jDesktopPane.add(jInternalFrames[i]);
jInternalFrames[i].setLocation(10 + 60 * i, 10 + 40 * i);
jInternalFrames[i].setVisible(true);
}
}
});
}
public static class FocusInternalFrame extends JInternalFrame {
public FocusInternalFrame() {
final JLabel jLabel = new JLabel("placeholder for pack();");
setContentPane(jLabel);
pack();
this.addPropertyChangeListener(
INTERNAL_FRAME_FOCUS_EVENT_PROPERTY,
new LabelFocusListener(jLabel));
}
}
private static class LabelFocusListener implements PropertyChangeListener {
private final JLabel jLabel;
public LabelFocusListener(JLabel jLabel) {
this.jLabel = jLabel;
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
// please keep a URL to the original version in the source code.
// http://javajon.blogspot.com/2015/08/windowfocuslistener-for-jinternalframe.html
if (INTERNAL_FRAME_FOCUS_EVENT_PROPERTY.equals(
evt.getPropertyName())) {
final Object oldValue = evt.getOldValue();
final Object newValue = evt.getNewValue();
if (oldValue instanceof Boolean
&& newValue instanceof Boolean) {
boolean wasInFocus = (Boolean) oldValue;
boolean isInFocus = (Boolean) newValue;
if (isInFocus && !wasInFocus) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
// focus gained
jLabel.setText("focus gained");
}
});
} else if (wasInFocus && !isInFocus) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
// focus lost
jLabel.setText("focus lost");
}
});
}
}
}
}
}
}

Categories

Resources