While loop with delay makes JFrame unresponsive - java

So, my JFrame becomes unresponsive when I run this code. I managed to trace it back to the while loop under gameLoop(). Regardless of using delay(1000/FRAMERATE) which calls Thread.sleep() within it, it will not allow the Key or Mouse Listeners to do their job.
Full code below, problem exists in gameLoop()
package me.LemmingsGame;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Game extends JFrame implements KeyListener, MouseListener{
private World wrld;//reference to current world
private WorldFile loader=null;//world data
private int gateCounter;
private int width,height; //width and height of level
private int mx,my;
private int tool = Lemming.CLIMB;
private Image dbImage; private Graphics dbg; //backbuffer
private Image [] sprites;//all images used in game
private Lemming[] lemmings; //array of all Lemmings in level
private int nextLem;//next Lemming to be received from Gate class
private int running;//state of game
private static final int FRAMERATE=180;//assigned framerate
public Game(WorldFile loader){
super("Lemmings");
setLocation(50,40);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
width=3+loader.x*10;height=29+loader.y*10;
loadImages();
lemmings=new Lemming[loader.noLemmings*loader.gates().length];
setSize(width,height);
setVisible(true);
addMouseListener(this);
addKeyListener(this);
this.loader=loader;
running=2;
dbImage= createImage(width,height);
dbg=dbImage.getGraphics();
wrld=new World(loader,createImage(width,height), sprites, this);
gameLoop();
}
public void loadImages(){
sprites=new Image[2];
sprites[0]=new ImageIcon("Resources/toolbar.png").getImage();
sprites[1]=new ImageIcon("Resources/selector.png").getImage();
}
public static void delay(long len){
try{
Thread.sleep(len);
}catch(InterruptedException e){
System.out.println(e);
}
}
public void moveLemmings(){
if(nextLem>0)
for(int i = 0;i<nextLem;i++)
lemmings[i].cycle();
}
public void gameLoop(){
wrld.openGates();
while(running>0){
delay(1000/FRAMERATE);
if(running==2){
gateCounter++;
if(gateCounter>FRAMERATE*2){
wrld.cycleGates();
gateCounter=0;
}
moveLemmings();
if(nextLem>0)
wrld.checkPositions(lemmings,nextLem);
}
repaint();
//paint(getGraphics());
}
}
public void paint(Graphics g){
if(wrld!=null){
dbg.setColor(Color.BLACK);
dbg.fillRect(0, 0, width, height);
wrld.draw(dbg);
if(nextLem>0)
for(int i=0;i<nextLem;i++){
lemmings[i].draw(dbg);
}
dbg.drawImage(sprites[0],0,0,null);
dbg.drawImage(sprites[1],tool-3*39,0,null);
g.drawImage(dbImage,3,29,this);
}
}
public void addLemming(Lemming lemmy) {
lemmings[nextLem]=lemmy;
lemmy.spawn();
nextLem++;
}
public void goal(){
running=0;
dispose();
new Menu();
}
public void fail(){
running=0;
dispose();
new Menu();
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void keyTyped(KeyEvent e) {}
public void mousePressed(MouseEvent e) {
System.out.println("boop");
mx=e.getX();
my=e.getY();
if(my<40)
if(mx<40)
tool=Lemming.CLIMB;
else if(mx>39&&mx<=39*2)
tool=Lemming.CHUTE;
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent e) {
System.out.println("boop2");
}
public void keyReleased(KeyEvent e) {
}
}
If it matters the program begins here and goes to the Game class
package me.LemmingsGame;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class Menu extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -1448646591011984524L;
private JComboBox worldList;
private JButton launch, worldEditor;
private String [] worldPaths;
private String [] worldNames;
private int currentWorld;
public Menu(){
super("Lemmings! By: Jordan and Seth");
this.setLocation(new Point(550,400));
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
File listDir[] = new File("./Worlds").listFiles();
int x=0;
for (int i = 0; i < listDir.length; i++)
if (listDir[i].isDirectory())
x++;
worldPaths=new String[x];
worldNames=new String[x];
x=0;
for (int i = 0; i < listDir.length; i++)
if (listDir[i].isDirectory()){
worldPaths[x]=listDir[i].getPath().substring(2);
worldNames[x]=worldPaths[x].substring(7);
x++;
}
worldList=new JComboBox(worldNames);
worldList.addActionListener(this);
worldEditor=new JButton("Open World Editor");
worldEditor.addActionListener(this);
launch = new JButton("Play");
launch.addActionListener(this);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(worldEditor);
cp.add(worldList);
cp.add(launch);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==worldEditor){
dispose();
new MapEditor();
}else if(e.getSource()==launch){
dispose();
try {
new Game(new WorldFile(worldPaths[currentWorld]));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(e.getSource()==worldList){
currentWorld=worldList.getSelectedIndex();
}
}
public static void main(String [] args){
new Menu();
}
}
Probably irrelevant, but here's a plug to the github repo https://github.com/cybnetsurfe3011/Lemmings-Computer-Science/

You are performing Thread.sleep() in your main thread which in this case is the EDT (Event Dispatcher Thread). This thread is responsible for painting your graphics and listening to events. By doing Thread.sleep() you are essentially pausing it, thus not allowing it to do it's job.
It seems that you want to refresh your GUI at intervals (at least that is what I am guessing). If this is the case, you will need to move your logic to a new separate thread and then, call what ever update methods you will need from the thread you will have spawned off. To make changes visible, you will need to call the event dispatcher thread again by using the SwingUtilities.invokeLater() method.

Related

Java.awt.event classes management

I started writing a game (also taking code parts online) in Java all in one class and now I'm trying to break it into multiple classes but awt.event is giving me problems.
I want the class MainGUI to implement awt.event classes but methods keyPressed and actionPerformed must only contain a call to a method that is in another class that contains the code to be executed in correspondence with the occurrence of the event .
I am doing this because I want the class mainGUI to only realize the game window , leaving the management of controls to another class .
I tried to transfer the code and implement the methods to call and everything works except the method requestFocus .
When I call it indirectly from the class used for the control, asking him to move the focus to the game window, it does not work .
So, the class I want to break is MainGUI and the class I want to manage the controls is Logic:
public class MainGUI extends JFrame implements ComponentListener, ActionListener, FocusListener, KeyListener {
//STATIC ATTRIBUTES
private final static int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3, STOP = 4;
private final static int NUMBER_OF_ROWS = 70;
private final static int NUMBER_OF_COLUMNS = 70;
private static int direction;
private static int currentX, currentY;
//INSTANCE ATTRIBUTES
private BoardPanel drawPanel;
private JPanel bottomPanel;
private JLabel message;
private Timer timer;
//CONSTRUCTOR
public MainGUI() {
super("Grid");
createGUI();
}
//PUBLIC INSTANCE METHODS
public void createbottomPanel(){
this.bottomPanel=new JPanel();
this.message = new JLabel("To START, Press \"ENTER\"", JLabel.CENTER);
this.message.setBackground(Color.LIGHT_GRAY);
this.message.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
this.bottomPanel.add(this.message);
}
public void createGUI(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(800, 600));
this.drawPanel = new BoardPanel();
createbottomPanel();
addComponentListener(this);
addKeyListener(this);
addFocusListener(this);
this.message.addKeyListener(this);
Container contPane = getContentPane();
contPane.setLayout(new BorderLayout());
contPane.add(this.drawPanel,BorderLayout.CENTER);
contPane.add(this.bottomPanel, BorderLayout.SOUTH);
this.currentX = 50;
this.currentY = 30;
this.direction = STOP;
this.message.requestFocus();
pack();
}
public void componentResized(ComponentEvent e) {
this.drawPanel.setGridUnit();
}
public void componentHidden(ComponentEvent e) {
//do-nothing
}
public void componentMoved(ComponentEvent e) {
//do-nothing
}
public void componentShown(ComponentEvent e) {
//do-nothing
}
public void actionPerformed(ActionEvent e) {
switch (direction) {
case UP:
if (currentY > 0)
currentY--;
BoardPanel.fillCell(currentX,currentY);
this.drawPanel.repaint();
break;
case DOWN:
if (currentY < NUMBER_OF_ROWS-1)
currentY++;
BoardPanel.fillCell(currentX,currentY);
this.drawPanel.repaint();
break;
case RIGHT:
if (currentX < NUMBER_OF_COLUMNS-1)
currentX++;
BoardPanel.fillCell(currentX,currentY);
this.drawPanel.repaint();
break;
case LEFT:
if (currentX > 0)
currentX--;
BoardPanel.fillCell(currentX,currentY);
this.drawPanel.repaint();
break;
}
}
public void focusGained(FocusEvent e) {
direction=STOP;
message.setText("To PAUSE, Press \"P\"");
timer = new Timer(50,this);
timer.start();
}
public void focusLost(FocusEvent e) {
if (timer != null)
timer.stop();
timer = null;
message.setText("To START, Press \"ENTER\"");
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_LEFT)
direction = LEFT;
else if (code == KeyEvent.VK_RIGHT)
direction = RIGHT;
else if (code == KeyEvent.VK_UP)
direction = UP;
else if (code == KeyEvent.VK_DOWN)
direction = DOWN;
else if (code == KeyEvent.VK_P)
message.requestFocus();
else if (code == KeyEvent.VK_ENTER)
this.requestFocus();
}
public void keyReleased(KeyEvent e) {
//do-nothing
}
public void keyTyped(KeyEvent e) {
//do-nothing
}
//STATIC METHODS
public static void openWindow() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run () {
new MainGUI().setVisible(true);
}
});
}
} // end class
The class Logic:
public class Logic {
//STATIC ATTRIBUTES
private static MainGUI test = new MainGUI();
//STATIC METHODS
public static void initGame(){
test.openWindow();
}
//This method must be called from actionPerformed in MainGUI and
//contain the code to be executed
public static void logicPerforms(){
//empty-for-now
}
//This method must be called from keyPressed in MainGUI and
//contain the code to be executed
public static void logicPress(){
//empty-for-now
}
} //end-class
The classes are in different packages but I omitted the various imports.
The class Boardpanel draws a grid and colors the cell coordinates (x, y) through its method fillCells.
The main class is:
public class Main {
public static void main(String[] args) {
Logic.initGame();
}
} //end-class
I transferred the necessary variables in the class Logic, copied the code from the methods actionPerformed and keyPressed of MainGUI and created static methods ( for example, to run the repaint ) to be called from logicPerforms and logicPress to solve call problems but these lines are not working:
else if (code == KeyEvent.VK_P)
test.askMessageFocus();
else if (code == KeyEvent.VK_ENTER)
test.askGUIFocus();
where askMessageFocus e askGUIFocus are two of those static methods mentioned earlier (they are in the class MainGUI) whose code is:
this.message.requestFocus();
for askMessageFocus and
this.requestFocus();
for the other one.
I apologize for my English and other mistakes I may have made ​​in the code, I hope someone can help me!
Almost any advice will be welcome , even aimed at optimizing other parts of code!
Thank you!!

How to write some 'pause' in method?

Here I have a method
public static Color pickColor(){
final aero.colorpicker.Frame frame = new aero.colorpicker.Frame();
new Thread(new Runnable() {
#Override
public void run() {
while (!frame.isSelected()) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return frame.getColorPicked();
}
isSelected() is flag that makes us know that user CHOOSED the color he want.
The problem is - if I use it like
Color c = pickColor(); in c will write default (black) color of aero.colorpicker.Frame class.
I need pause here, which will wait for select.
package aero.colorpicker;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by Aero on 28.12.2014.
*/
public class Frame extends JFrame {
//MAINFIELD
private Color colorPicked = Color.black;
private boolean selected = false;
//GUIFIELDS
private JPanel rightPanel;
private JLabel R;
private JLabel G;
private JLabel B;
private JTextField RData;
private JTextField GData;
private JTextField BData;
private JPanel RPanel;
private JPanel GPanel;
private JPanel BPanel;
private ColorPanel colorPanel;
private JButton pick;
private BufferedImage colors;
private JLabel imageLabel;
public Frame(){
initFrame();
setVisible(true);
}
private void initComponents(){
rightPanel = new JPanel();
R = new JLabel("R");
G = new JLabel("G");
B = new JLabel("B");
RData = new JTextField();
GData = new JTextField();
BData = new JTextField();
RPanel = new JPanel();
GPanel = new JPanel();
BPanel = new JPanel();
colorPanel = new ColorPanel();
pick = new JButton("Pick");
}
private void addListeners(){
RData.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getRed();
colorPanel.repaint();
}
});
GData.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getGreen();
colorPanel.repaint();
}
});
BData.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getBlue();
colorPanel.repaint();
}
});
pick.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
getRed();
getBlue();
getGreen();
Frame.this.setVisible(false);
}
});
imageLabel.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
int x, y;
x = e.getX();
y = e.getY();
setColorPicked(new Color(colors.getRGB(x, y)));
colorPanel.repaint();
RData.setText(Integer.toString(getColorPicked().getRed()));
BData.setText(Integer.toString(getColorPicked().getBlue()));
GData.setText(Integer.toString(getColorPicked().getGreen()));
}
});
}
private void getRed(){
int r;
try {
r = Integer.parseInt(RData.getText());
}catch (NumberFormatException nfe){
RData.setText("0");
r = 0;
}
setColorPicked(r, getColorPicked().getGreen(), getColorPicked().getBlue());
}
private void getGreen(){
int g;
try {
g = Integer.parseInt(GData.getText());
}catch (NumberFormatException nfe){
GData.setText("0");
g = 0;
}
setColorPicked(getColorPicked().getRed(), g, getColorPicked().getBlue());
}
private void getBlue(){
int b;
try {
b = Integer.parseInt(BData.getText());
}catch (NumberFormatException nfe){
BData.setText("0");
b = 0;
}
setColorPicked(getColorPicked().getRed(), getColorPicked().getGreen(), b);
}
private void initFrame(){
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.setLayout(new BorderLayout());
this.setSize(369, 194);
this.setTitle("Color picker");
this.setResizable(false);
initComponents();
InputStream input = Frame.class.getClassLoader().getResourceAsStream("colorGradient.jpg");
try {
colors = ImageIO.read(input);
} catch (IOException e) {
e.printStackTrace();
}
imageLabel = new JLabel(new ImageIcon(colors));
this.add(imageLabel, BorderLayout.CENTER);
rightPanel.setLayout(new GridLayout(5, 1));
RPanel.setLayout(new GridLayout(1, 2));
GPanel.setLayout(new GridLayout(1, 2));
BPanel.setLayout(new GridLayout(1, 2));
RPanel.add(R);
RPanel.add(RData);
GPanel.add(G);
GPanel.add(GData);
BPanel.add(B);
BPanel.add(BData);
rightPanel.add(RPanel);
rightPanel.add(GPanel);
rightPanel.add(BPanel);
rightPanel.add(colorPanel);
rightPanel.add(pick);
this.add(rightPanel, BorderLayout.EAST);
this.repaint();
addListeners();
}
public Color getColorPicked() {
return colorPicked;
}
private void setColorPicked(Color colorPicked) {
this.colorPicked = colorPicked;
}
private void setColorPicked(int r, int g, int b){
this.colorPicked = new Color(r, g, b);
}
private void setSelected(){
selected = true;
}
public boolean isSelected(){
return selected;
}
private class ColorPanel extends JPanel{
public void paintComponent(Graphics g){
g.setColor(colorPicked);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}
Your issue here is that your method pickColor creates the thread that waits for a colour to be picked, starts it and then exits. In other words pickColor isn't waiting for the thread to complete. The thread itself does nothing when it does complete.
One solution would be to wait until the thread completes before exiting the method. That would still be poor design, however. It is possible (in fact, likely) that you are calling pickColor within the thread processing UI events (i.e. the dispatch thread) which will make the UI unresponsive. It also uses a unnecessary busy wait.
If this is what you want to do then you'll need to understand how to create a new event queue. You also need to understand how to use semaphores to block the thread. I'll provide the code here but I wouldn't recommend dumping it into your application without understanding what you are doing. This is designed to be a subclass of your dialog class so that the method used to close the dialog can be overwritten to notify the blocked thread.
For the method that shows the colour picking dialog:
public synchronized Color pickColor() {
// code to create and show the dialog
EventQueue tempEventQueue = new EventQueue();
Toolkit.getDefaultToolkit().getSystemEventQueue().push(tempEventQueue);
try {
wait();
} catch (InterruptedException ex) {
// stop waiting on interrupt
} finally {
tempEventQueue.pop();
}
// return colour from dialog
}
For the method that is called when the user closes the dialog:
public synchronized void closeChooser() {
notifyAll();
super.closeChooser(); // or whatever it's called
}
The correct solution is to have two methods: one to open the colour pick dialog and then a second which is called when the dialog closes. I don't know what capabilities aero.colorpicker.Frame has but if it doesn't provide this then you could subclass it and override whatever method is called when the user accepts or cancels.

Why won't my JFrame respond to mouse and window changes?

Here is my code:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class wind extends JFrame implements ComponentListener, MouseListener
{
JButton button;
JLabel label;
public wind()
{
// initialise instance variables
setTitle("My First Window!");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.addComponentListener(this);
content.addMouseListener(this);
label = new JLabel("My First Window");
content.add(label);
label.addComponentListener(this);
button = new JButton("Click If You Wish To Live!");
button.addMouseListener(this);
content.add(button)
setContentPane(content);
}
public void componentHidden(ComponentEvent e){
try{wait(100);}
catch(InterruptedException error){}
button.setText("Hidden!");
}
public void componentShown(ComponentEvent e){
try{wait(100);}
catch(InterruptedException error){}
button.setText("Shown!");
}
public void componentResized(ComponentEvent e){
try{wait(100);}
catch(InterruptedException error){}
button.setText("Resized!");
}
public void componentMoved(ComponentEvent e){
try{wait(100);}
catch(InterruptedException error){}
button.setText("Moved!");
}
public void mouseExited(MouseEvent e){
try{wait(100);}
catch(InterruptedException error){}
label.setText("Exited!");
}
public void mouseEntered(MouseEvent e){
try{wait(100);}
catch(InterruptedException error){}
label.setText("Entered!");
}
public void mousePressed(MouseEvent e){
try{wait(100);}
catch(InterruptedException error){}
label.setText("pressed at: "+e.getX()+" "+e.getY());
}
public void mouseReleased(MouseEvent e){
try{wait(100);}
catch(InterruptedException error){}
label.setText("Released!");
label.setLocation(e.getX(), e.getY());
}
public void mouseClicked(MouseEvent e){}
}
It won't respond to the mouse or window re-sizing, hiding, or moving. Furthermore the button is not being displayed. fixed! I am just starting to learn about Java's JFrame and other graphics so I have no idea what's wrong with my code, although I suspect it has something to do with the way I made the button and added the listeners to the objects. Could someone please explain why it does this, and how to fix it. Thank you in advance!
Your problem is that you are using the wait function not correctly. Try to use the class javax.swing.Timer also known as a Swing Timer for delays in Swing programs, for simple animations and for repetitive actions. For more information see this example on stackoverflow: Java Wait Function
One possible way to add a ActionListener to a JButton:
// You are adding an ActionListener to the button
//Using the method addActionListener and a anonymous inner class
button.addActionListener(new ActionListener() {//anonymous inner class
#Override
public void actionPerformed(ActionEvent arg0)
{
button.setText("Text modified by an event called ActionEvent!");
}
});
I decided to play with similar code and came up with this bit of code that tries to show the state of things in a status bar at the bottom:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
#SuppressWarnings({ "serial"})
// so the compiler won't complain
public class MyWindPanel extends JPanel {
private static final int PREF_W = 1200;
private static final int PREF_H = 600;
private static final String MOUSE_LOCATION = "Mouse Location [%04d, %04d]";
private static final String COMPONENT_STATE = "Component: %-15s";
private static final String TIMER_LABEL = "Elapsed Time: %02d:%02d:%02d:%03d";
private static final int TIMER_DELAY = 20;
private static final String MOUSE_STATE = "Mouse State: %-15s";
public static final String BUTTON_TEXT = "Set MyWindPanel %s";
private JLabel mouseLocation = new JLabel(
String.format(MOUSE_LOCATION, 0, 0));
private JLabel mouseState = new JLabel(String.format(MOUSE_STATE, ""));
private JLabel componentState = new JLabel(
String.format(COMPONENT_STATE, ""));
private JLabel timerLabel = new JLabel(
String.format(TIMER_LABEL, 0, 0, 0, 0));
private long startTime = System.currentTimeMillis();
private Action buttonAction = new MyButtonAction(String.format(BUTTON_TEXT, "Invisible"));
private JPanel statusPanel;
public MyWindPanel() {
setBackground(Color.pink);
Font font = new Font(Font.MONOSPACED, Font.BOLD, 14);
mouseLocation.setFont(font);
mouseState.setFont(font);
componentState.setFont(font);
timerLabel.setFont(font);
statusPanel = new JPanel();
statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.LINE_AXIS));
statusPanel.add(mouseLocation);
statusPanel.add(Box.createHorizontalStrut(25));
statusPanel.add(mouseState);
statusPanel.add(Box.createHorizontalStrut(25));
statusPanel.add(componentState);
statusPanel.add(Box.createHorizontalStrut(25));
statusPanel.add(timerLabel);
new Timer(TIMER_DELAY, new TimerListener()).start();
MouseAdapter myMouseAdapter = new MyMouseAdapter();
addMouseMotionListener(myMouseAdapter);
addMouseListener(myMouseAdapter);
addComponentListener(new MyComponentListener());
setLayout(new BorderLayout());
// add(statusPanel, BorderLayout.PAGE_END);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public Action getButtonAction() {
return buttonAction;
}
public JComponent getStatusPanel() {
return statusPanel;
}
private class TimerListener implements ActionListener {
private static final int SECONDS_PER_MIN = 60;
private static final int MSEC_PER_SEC = 1000;
private static final int MIN_PER_HOUR = 60;
#Override
public void actionPerformed(ActionEvent evt) {
if (!MyWindPanel.this.isDisplayable()) {
((Timer) evt.getSource()).stop(); // so timer will stop when program
// over
}
long currentTime = System.currentTimeMillis();
long diff = currentTime - startTime;
int hours = (int) (diff / (MIN_PER_HOUR * SECONDS_PER_MIN * MSEC_PER_SEC));
int minutes = (int) (diff / (SECONDS_PER_MIN * MSEC_PER_SEC))
% MIN_PER_HOUR;
int seconds = (int) ((diff / MSEC_PER_SEC) % SECONDS_PER_MIN);
int mSec = (int) diff % MSEC_PER_SEC;
timerLabel.setText(String.format(TIMER_LABEL, hours, minutes, seconds,
mSec));
}
}
private class MyComponentListener extends ComponentAdapter {
#Override
public void componentHidden(ComponentEvent e) {
componentState.setText(String.format(COMPONENT_STATE, "Hidden"));
}
#Override
public void componentMoved(ComponentEvent e) {
componentState.setText(String.format(COMPONENT_STATE, "Moved"));
}
#Override
public void componentResized(ComponentEvent e) {
componentState.setText(String.format(COMPONENT_STATE, "Resized"));
}
#Override
public void componentShown(ComponentEvent e) {
componentState.setText(String.format(COMPONENT_STATE, "Shown"));
}
}
private class MyButtonAction extends AbstractAction {
public MyButtonAction(String name) {
super(name);
}
#Override
public void actionPerformed(ActionEvent e) {
boolean visible = MyWindPanel.this.isVisible();
String text = visible ? "Visible" : "Invisible";
((AbstractButton) e.getSource()).setText(String.format(BUTTON_TEXT, text));
MyWindPanel.this.setVisible(!MyWindPanel.this.isVisible());
Window win = SwingUtilities.getWindowAncestor(MyWindPanel.this);
win.revalidate();
win.repaint();
}
}
private class MyMouseAdapter extends MouseAdapter {
#Override
public void mouseMoved(MouseEvent e) {
mouseLocation.setText(String.format(MOUSE_LOCATION, e.getX(), e.getY()));
}
#Override
public void mouseDragged(MouseEvent e) {
mouseState.setText(String.format(MOUSE_STATE, "Dragged"));
mouseLocation.setText(String.format(MOUSE_LOCATION, e.getX(), e.getY()));
}
public void mousePressed(MouseEvent e) {
mouseState.setText(String.format(MOUSE_STATE, "Pressed"));
};
public void mouseReleased(MouseEvent e) {
mouseState.setText(String.format(MOUSE_STATE, "Released"));
};
public void mouseEntered(MouseEvent e) {
mouseState.setText(String.format(MOUSE_STATE, "Entered"));
};
public void mouseExited(MouseEvent e) {
mouseState.setText(String.format(MOUSE_STATE, "Exited"));
};
}
private static void createAndShowGui() {
MyWindPanel mainPanel = new MyWindPanel();
JPanel topPanel = new JPanel();
topPanel.add(new JButton(mainPanel.getButtonAction()));
JFrame frame = new JFrame("MyWind");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.getContentPane().add(topPanel, BorderLayout.PAGE_START);
frame.getContentPane().add(mainPanel.getStatusPanel(), BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

timer t = new timer(50, b); t.stop(); is not working

My t.stop(); method is not working. I am going crazy trying to figure out why my stop method is not working.
I'm using the a timer in my code and I can't get it to stop. Can anyone take a look at it and tell me what's going on?:
/*Gilberto Rose*/
package homework2;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MultipleBalls extends JFrame implements ActionListener
{
int dx = 2;
int dy = 2;
int x = 1;
int y = 1;
int i = 0;
public static void main(String[] args)
{
Runnable balls = new Ball2();
Thread thread1 = new Thread(balls);
thread1.run();
}
#Override
public void actionPerformed(ActionEvent arg0)
{
repaint();
System.out.println(i++);
}
}// End of Ball class
class Ball2 extends JPanel implements Runnable
{
MultipleBalls b = new MultipleBalls();
JButton g = new JButton("resume");
JButton f = new JButton("suspend");
JButton e = new JButton("-1");
JButton d = new JButton("+1");
List<Ball2> L = new ArrayList<Ball2>();
Timer t = new Timer(50, b);
public int x = 6;
public void loopstop()
{
t.stop();
}// end of loopstop method
Ball2()
{
controller4();
controller3();
controller2();
controller();
add(d);
add(e);
add(f);
add(g);
}// End of Ball2 constructor
public void run()
{
Ball2 c = new Ball2();
b.setSize(500, 500);
b.setVisible(true);
b.add(c);
t.start();
} // End of run method
public void controller()
{
d.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
L.add(new Ball2());
}
});
}// End of controller
public void controller2()
{
e.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("subtracter");
L.remove(L.size()-1);
}
});
}// End of controller2
public void controller3()
{
f.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
loopstop();
}
});
}// End of controller3
public void controller4()
{
g.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Resume");
}
});
}// End of controller4
public void paintComponent(Graphics g)
{
if(L.size() > 0)
{
int i = 0;
do
{
g.fillOval(L.get(i).ballx(), L.get(i).bally(), 90, 90);
i++;
}while(i < L.size() && true ); // End of Do while loop
}// End of if statement
}// End of paintComponent
MultipleBalls bb = new MultipleBalls();
public int ballx()
{
if (bb.x == 0 || bb.x == 500)
{
bb.dx *= -1;
} // End of if statement
bb.x += bb.dx;
return bb.x;
}
public int bally()
{
if (bb.y == 0 || bb.y == 500 )
{
bb.dy *= -1;
}// end of if statement
bb.y += bb.dy;
return bb.y;
}// End of bally
}// End of Ball2 class
Your code is extremely convoluted, I believe that it's suffering from something called cyclomatic complexity, so much so, it is difficult for you or us to see what object is creating what other object, and what is running what. And this is your problem. You have at least two MultipleBall objects, two Ball2 objects, and you're starting the Timer for one of the Ball2 objects and stopping it for the other.
The solution: simplify this code greatly.
Create one MultipleBalls object, just one.
Don't have MultipleBalls implement ActionListener. Rather use an anonymous inner class for your ActionListener and create it on the spot where you need it.
Create just one Ball2 object, just one.
Also note that you almost never call run() on a Thread object but rather start(), but having said that, I'm not even sure that you should be using a Thread object where you're using it.
Edit
My main class would be simple, and would simply have a main method and supporting method that gets things started. Something like:
public class MultipleBalls {
private static void createAndShowGui() {
BallsPanel mainPanel = new BallsPanel();
JFrame frame = new JFrame("Multiple Balls");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Edit
For an example of a separation of concerns:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;
public class MultipleBallsZ {
private static void createAndShowGui() {
BallsPanelZ ballsPanel = new BallsPanelZ();
new Control(ballsPanel);
JFrame frame = new JFrame("Multiple Balls");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(ballsPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#SuppressWarnings("serial")
class BallsPanelZ extends JPanel {
private static final int TIMER_DELAY = 200;
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private Timer timer = new Timer(TIMER_DELAY, new TimerListener());
private int counter = 0;
private Control control = null;
public BallsPanelZ() {
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public Timer getTimer() {
return timer;
}
private class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
counter++;
System.out.printf("Count: %03d%n", counter);
}
}
public void setControl(Control control) {
this.control = control;
for (Action action : control) {
add(new JButton(action));
}
}
}
#SuppressWarnings("serial")
class Control implements Iterable<Action> {
private List<Action> actionList = new ArrayList<>();
private BallsPanelZ ballsPanel;
public Control(BallsPanelZ ballsPanel) {
actionList.add(new PauseAction());
actionList.add(new ResumeAction());
this.ballsPanel = ballsPanel;
ballsPanel.setControl(this);
}
private class PauseAction extends AbstractAction {
public PauseAction() {
super ("Timer Pause");
putValue(MNEMONIC_KEY, KeyEvent.VK_P);
}
#Override
public void actionPerformed(ActionEvent e) {
ballsPanel.getTimer().stop();
}
}
private class ResumeAction extends AbstractAction {
public ResumeAction() {
super("Timer Resume");
putValue(MNEMONIC_KEY, KeyEvent.VK_R);
putValue(DISPLAYED_MNEMONIC_INDEX_KEY, 6);
}
#Override
public void actionPerformed(ActionEvent e) {
ballsPanel.getTimer().restart();
}
}
#Override
public Iterator<Action> iterator() {
return actionList.iterator();
}
}

JFrame.setVisible(false) and Robot.createScreenCapture timing

I'm trying to capture the screen without including my application's window. To do this I first call setVisible(false), then I call the createScreenCapture method, and finally I call setVisible(true). This isn't working however and I'm still getting my applications window in the screen capture. If I add a call to sleep this seems to resolve the issue, but I know this is bad practice. What is the right way to do this?
Code:
setVisible(false);
BufferedImage screen = robot.createScreenCapture(rectScreenSize);
setVisible(true);
Have you tried to use SwingUtilities.invokeLater() and run the capture inside of the runnable passed as an argument? My guess is that the repaint performed to remove your application is performed right after the end of the current event in the AWT-EventQueue and thus invoking the call immediately still captures your window. Invoking the createCapture in a delayed event through invokeLater should fix this.
you have to delay this action by implements Swing Timer, for example
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
public class CaptureScreen implements ActionListener {
private JFrame f = new JFrame("Screen Capture");
private JPanel pane = new JPanel();
private JButton capture = new JButton("Capture");
private JDialog d = new JDialog();
private JScrollPane scrollPane = new JScrollPane();
private JLabel l = new JLabel();
private Point location;
private Timer timer1;
public CaptureScreen() {
capture.setActionCommand("CaptureScreen");
capture.setFocusPainted(false);
capture.addActionListener(this);
capture.setPreferredSize(new Dimension(300, 50));
pane.add(capture);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(pane);
f.setLocation(100, 100);
f.pack();
f.setVisible(true);
createPicContainer();
startTimer();
}
private void createPicContainer() {
l.setPreferredSize(new Dimension(700, 500));
scrollPane = new JScrollPane(l,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBackground(Color.white);
scrollPane.getViewport().setBackground(Color.white);
d.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
d.add(scrollPane);
d.pack();
d.setVisible(false);
d.addWindowListener(new WindowListener() {
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
f.setVisible(true);
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
});
}
private void startTimer() {
timer1 = new Timer(1000, new AbstractAction() {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
capture.doClick();
f.setVisible(false);
}
});
}
});
timer1.setDelay(500);
timer1.setRepeats(false);
timer1.start();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("CaptureScreen")) {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); // gets the screen size
Robot r;
BufferedImage bI;
try {
r = new Robot(); // creates robot not sure exactly how it works
Thread.sleep(1000); // waits 1 second before capture
bI = r.createScreenCapture(new Rectangle(dim)); // tells robot to capture the screen
showPic(bI);
saveImage(bI);
} catch (AWTException e1) {
e1.printStackTrace();
} catch (InterruptedException e2) {
e2.printStackTrace();
}
}
}
private void saveImage(BufferedImage bI) {
try {
ImageIO.write(bI, "JPG", new File("screenShot.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
private void showPic(BufferedImage bI) {
ImageIcon pic = new ImageIcon(bI);
l.setIcon(pic);
l.revalidate();
l.repaint();
d.setVisible(false);
//location = f.getLocationOnScreen();
//int x = location.x;
//int y = location.y;
//d.setLocation(x, y + f.getHeight());
d.setLocation(150, 150);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
d.setVisible(true);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
CaptureScreen cs = new CaptureScreen();
}
});
}
}

Categories