create animation using JFrame - java

I have a problem, creating my Java frame application.
I can't call repaint of component in a loop;
This is the part of mo class:
if(e.getSource()==drop){
//check if the row has space
if(currentGame.isFull(choosePosition)){
return;
}
else{
int row = currentGame.placeFigure(choosePosition, turn);
ImageIcon temp;
if(turn)
temp = firstIcon;
else
temp = secondIcon;
for(int i = 0; i != row + 1; ++i){
cells[i][choosePosition].setIcon(temp);
if(i != 0)
cells[i - 1][choosePosition].setIcon(emptyIcon);
gameBoardPanel.repaint();
gameBoardPanel.revalidate();
Graphics myGraphics = getGraphics();
// Draw as appropriate using myGraphics
myGraphics.dispose();
paint(myGraphics);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
//Handle exception
System.out.println("can't start animation");
}
}
repaint();
}
}

Start learning how to use the paintComponent method. And here's how to use Timers:
public class your_class_name extends if_you_need_to_extend implments ActionListener
{
//In the constructor...
Timer t = new Timer(milliseconds,this);
t.start();
//In the main class...
#Override
public void actionPerformed(ActionEvent e)
{
//animations here
repaint(); //calls paintComponent again
}
#Override
public void paintComponent(Graphics g)
{
//default drawings here
}
}

Related

Swing GUI freezes while using threads

I am writing a breakout Game and everything is working fine except one little thing.
I have a ball object which extends from thread and RepainterThread which can either implement Runnable or extends Thread which calls the paint method which re-renders the game field with bricks, a paddle, and a Ball
I have a Singelton GameController which connects all things.
I have a GameState with an isActive boolean to decide if the game should be paused or not.
I start the game and I can play and the program behaves as it should.
The Frame gets drawn the ball moves the bricks break when the ball hits everything fine.
Then I pause the game via a Button where I set is Active to false.
The Ball stops as it should. Then I hit the continue Button and isActive is again true. The ball object starts running again and the running method of Repainter Thread is allso triggered but the Swing Frame Freezes completely.
I tried various things these are my nearest approaches i have.
I spent days on it please help
public class Ball extends MovingObject {
private double hSpeed; // Horizontal velocity
private double vSpeed; // Vertical velocity
public Ball() {
this.color = Color.MAGENTA;
this.height = GameSettings.ballSize;
this.width = GameSettings.ballSize;
this.position = new Point2D.Double(GameSettings.defaultBallX, GameSettings.defaultBallY);
this.hSpeed = GameSettings.ballHSpeed;
this.vSpeed = GameSettings.ballYSpeed;
}
public Ball(Ball ball) {
color = ball.color;
height = ball.height;
width = ball.width;
position = ball.position;
hSpeed = ball.hSpeed;
vSpeed = ball.vSpeed;
}
public double getHSpeed() {
return this.hSpeed;
}
public double getVSpeed() {
return this.vSpeed;
}
public void run() {
try {
while (GameController.getInstance().getGameState().isActive()) {
System.out.println("Ich run im Ball");
Thread.sleep(10);
this.meetingWall();
this.meetingBrick();
this.position.setLocation(this.getPosition().getX() + this.hSpeed,
this.getPosition().getY() + this.vSpeed);
if (this.meetingPaddle()) {
this.newDirection();
}
if (this.out()) {
GameController.getInstance().stopGame();
this.stopThread();
}
this.position = this.getPosition();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stopThread() {
GameController.getInstance().getGameState().setActive(false);
}
public void startThreadAgain() {
this.run();
}
#Override
public synchronized void start() {
this.position.setLocation(GameSettings.defaultBallX, GameSettings.defaultBallY);
super.start();
}
class GamePanel extends JPanel {
private static final long serialVersionUID = 1L;
private final Color backgroundColor = Color.BLACK;
GameState gameState = GameController.getInstance().getGameState();
public GamePanel() {
super();
this.addKeyListener(new KeyListener() {
// Dieser KeyListener soll auf Inputs der Pfeiltasten nach links
// <- und rechts -> hoeren und eine entsprechende Bewegung des
// Schlaegers erwirken, aber nur, wenn das Spiel nicht
// pausiert/gestoppt ist.
public void keyPressed(KeyEvent keyEvent) {
if (gameState.isActive()) {// gameState.isActive()
if (keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
gameState.getPaddle().setPositionRigth();
}
if (keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {
gameState.getPaddle().setPositionLeft();
}
}
}
public void keyReleased(KeyEvent keyEvent) {
// TODO
}
public void keyTyped(KeyEvent arg0) {
}
});
}
public void paint(Graphics g) {
Graphics2D graphics2D = (Graphics2D) g;
graphics2D.setColor(this.backgroundColor);
graphics2D.fillRect(0, 0, this.getWidth(), this.getHeight());
for (int i = 0; i < gameState.getBricks().length; i++) {
for (int j = 0; j < gameState.getBricks()[i].length; j++) {
if ((gameState.getBricks()[i][j] != null)) {
graphics2D.setColor(gameState.getBricks()[i][j].getColor());
graphics2D.fillRect(
(i * GameSettings.brickWidth) + (i + 1) * (GameSettings.spaceAroundBrick + 1),
(j * GameSettings.brickHeight) + (j + 1) * GameSettings.spaceAroundBrick,
GameSettings.brickWidth, GameSettings.brickHeight);
}
scoreLabel.setText(this.gameState.getScore() + "");
gameState.getPaddle().draw(graphics2D);
gameState.getBall().draw(graphics2D);
}
}
}
}
// First Approach
private class RepainterThread implements Runnable {
public RepainterThread() {
System.out.println("RepainThreadCOntructor");
}
private void updateGUI() {
System.out.println("before invoke later");
System.out.println("repaint");
getGamePanel().requestFocus();
getGamePanel().repaint();
}
#Override
public void run() {
System.out.println("Repainter run");
System.out.println(GameController.getInstance().getGameState().isActive());
while (GameController.getInstance().getGameState().isActive()) {
System.out.println("inside while");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
updateGUI();
}
}
}
// Second Approach
private class RepainterThread implements Runnable {
public RepainterThread() {
System.out.println("RepainThreadCOntructor");
}
private void updateGUI(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("repaint");
getGamePanel().requestFocus();
getGamePanel().repaint();
}
});
}
#Override
public void run() {
System.out.println("Repainter run");
System.out.println(GameController.getInstance().getGameState().isActive());
while (GameController.getInstance().getGameState().isActive()) {
System.out.println("inside while");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
updateGUI();
}
}
}
Frame should no longer freeze
please help
This is kind of a wild guess, but since according to your description the program works correctly until you pause and unpause it, the problem likely lies with the startThreadAgain method:
public void startThreadAgain() {
this.run();
}
Your code does not show how you call this method, but in that method you just call this.run(), i.e. you call the run method as a regular, blocking method, while probably still in the UI thread. Instead, you should call start() to start a proper new thread executing the run method.
public void startThreadAgain() {
super.start(); // not this start, otherwise it resets the ball's location
}

Java animation:can't see anything

I am doing animation with Java and I'm using NetBeans. My applet has started but I don't see anything in the applet viewer? Can anybody see any problem with my code and this is my first programming course as U can see =)! Thanks for advance!!
Problem.1 Do animation with 10 gif pictures by using MediaTracker,addImage and thread.sleep.
(Code)
import java.applet.Applet;
import java.awt.*;
public class Animaatio extends Applet implements Runnable
{
Image images[] = null;
MediaTracker tracker = null;
Thread animaatio;
Graphics g;
#Override
public void init()
{
tracker = new MediaTracker(this);
images = new Image[10];
for (int i=0; i < 10; i++)
{
images[i] = getImage( getCodeBase(),"T" + (i+1) + ".gif");
tracker.addImage(images[i],0);
}
try{
tracker.waitForAll();
}catch (InterruptedException e){}
}
#Override
public void start() {
if (animaatio == null) {
animaatio = new Thread(this);
animaatio.start();
}
}
#Override
public void paint (Graphics g)
{
super.paint(g);
g.drawImage(images[10], 0, 0, this);
}
#Override
public void run(){
while(true){
repaint();
try{
Thread.sleep(1000);
}
catch (InterruptedException e) {}
}
}
}

Opening a new JFrame using a JButton

I have two classes (Sampling and Stacker). The Sampling class (my Main class) is extends JFrame and has a JButton with an ActionListener to open the Stacker class.
The problem is when the button is clicked, the Stacker class will open but only a frame without any components. When I switch the main method into the Stacker class, the program works fine. What is the problem?
Here is the code:
The Sampling class:
public class Sampling extends JFrame implements ActionListener
{
private JButton openStacker;
Stacker st;
public Sampling()
{
setSize(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setLocationRelativeTo(null);
openStacker = new JButton("Start Stacker!");
add(openStacker);
openStacker.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
dispose();
st = new Stacker();
}
public static void main (String args[])
{
new Sampling();
}
}
The Stacker game class:
public class Stacker extends JFrame implements KeyListener
{
int iteration = 1;
double time = 200;
int last = 0;
int m = 10;
int n = 20;
JButton b[][];
int length[] = {5,5};
int layer = 19;
int deltax[] = {0,0};
boolean press = false;
boolean forward = true;
boolean start = true;
public Stacker()
{
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400,580);
this.setUndecorated(false);
this.setLocationRelativeTo(null);
b = new JButton [m][n];
setLayout(new GridLayout(n,m));
for (int y = 0;y<n;y++)
{
for (int x = 0;x<m;x++)
{
b[x][y] = new JButton(" ");
b[x][y].setBackground(Color.DARK_GRAY);
add(b[x][y]);
b[x][y].setEnabled(false);
}//end inner for
}
this.setFocusable(true);
this.pack();
this.addKeyListener(this);
this.setVisible(true);
go();
}
public void go()
{
int tmp = 0;
Component temporaryLostComponent = null;
do{
if (forward == true)
{
forward();
} else {
back();
}
if (deltax[1] == 10-length[1])
{
forward = false;
} else if (deltax[1] == 0)
{
forward = true;
}
draw();
try
{
Thread.sleep((long) time);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}while(press == false);
if (layer>12)
{
time= 150-(iteration*iteration*2-iteration);
} else
{
time = time - 2.2;
}
iteration++;
layer--;
press = false;
tmp = check();
length[0] = length[1];
length[1] = tmp;
if (layer == -1)
{
JOptionPane.showMessageDialog(temporaryLostComponent, "Congratulations! You beat the game!");
repeat();
}
if (length[1] <= 0)
{
JOptionPane.showMessageDialog(temporaryLostComponent, "Game over! You reached line "+(18-layer)+"!");
repeat();
}
last = deltax[1];
start = false;
go();
}
public int check()
{
if (start == true)
{
return length[1];
}
else if (last<deltax[1])
{
if (deltax[1]+length[1]-1 <= last+length[0]-1)
{
return length[1];
}
else
{
return length[1]-Math.abs((deltax[1]+length[1])-(last+length[0]));
}
}
else if (last>deltax[1])
{
return length[1]-Math.abs(deltax[1]-last);
}
else
{
return length[1];
}
}
public void forward()
{
deltax[0] = deltax[1];
deltax[1]++;
}
public void back()
{
deltax[0] = deltax[1];
deltax[1]--;
}
public void draw()
{
for (int x = 0;x<length[1];x++)
{
b[x+deltax[0]][layer].setBackground(Color.DARK_GRAY);
}
for (int x = 0;x<length[1];x++)
{
b[x+deltax[1]][layer].setBackground(Color.CYAN);
}
}
public void repeat()
{
if(JOptionPane.showConfirmDialog(null, "PLAY AGAIN?","WARNING",JOptionPane.YES_NO_OPTION)== JOptionPane.YES_OPTION)
{
dispose();
new Stacker();
}else{
System.exit(0);
}
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
press = true;
}
}
public void keyReleased(KeyEvent arg0)
{
}
public void keyTyped(KeyEvent arg0)
{
}
}
Just to put all my comments into an answer, and give you somewhere to start with:
Comment 1:
Take out go(); see that happens. I tested it and it will work. If you leave it there, even the frame's close button is jammed. You're blocking the edt with the while->Thread.sleep junk. You'll want to do some refactoring. You're code it hard to follow and I have no idea what you're trying to do, so I didn't even attempt it
Comment 2:
If you're wondering why it works when you just run the main from the Stacker class, it's probably because you are running it outside the EDT,
public static void main(String[] args) { new Stacker(); }. What happens when you click the button, that action is performed within the EDT, and hence your new Stacker() will be run on the EDT. In which case the EDT gets blocked by your while loop. If you try run the program from the Stacker class, but wrap it in a SwingUtilities.invokeLater, you will also notice the program fails to work. Swing programs should be run on the EDT though.
Comment 2: Read the first few sections on Concurrency with Swing
So what you can do is use a Swing Timer (which operates on the EDT) for the game loop. What I did was refactor your code a bit. It doesn't operate the way you want it to yet, only because I didn't really understand the logic of your code. So I couldn't get it to work. What I did though, is put some of the logic into the Timer.
Timer timer = new Timer((int)time, new ActionListener(){
public void actionPerformed(ActionEvent event) {
if (forward == true) {
forward();
} else {
back();
}
if (deltax[1] == 10 - length[1]) {
forward = false;
} else if (deltax[1] == 0) {
forward = true;
}
draw();
}
});
And when the go() method is called, it just starts the timer by calling timer.start(). Basically what you need to know about the timer, is that every tick (the milliseconds you pass it), the actionPerformed will be called. So you can update the game state in that method, just like you did in the while loop each iteration.
Take some time to go over How to Use Swing Timers
To get the game working properly, you still need to make some adjustments, but this should give you a head start.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Sampling extends JFrame implements ActionListener {
private JButton openStacker;
Stacker st;
public Sampling() {
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setLocationRelativeTo(null);
openStacker = new JButton("Start Stacker!");
add(openStacker);
openStacker.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
dispose();
st = new Stacker();
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new Sampling();
}
});
}
}
class Stacker extends JFrame implements KeyListener {
int iteration = 1;
double time = 200;
int last = 0;
int m = 10;
int n = 20;
JButton b[][];
int length[] = {5, 5};
int layer = 19;
int deltax[] = {0, 0};
boolean press = false;
boolean forward = true;
boolean start = true;
Timer timer = new Timer((int)time, new ActionListener(){
public void actionPerformed(ActionEvent event) {
if (forward == true) {
forward();
} else {
back();
}
if (deltax[1] == 10 - length[1]) {
forward = false;
} else if (deltax[1] == 0) {
forward = true;
}
draw();
}
});
public Stacker() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400, 580);
this.setUndecorated(false);
this.setLocationRelativeTo(null);
b = new JButton[m][n];
setLayout(new GridLayout(n, m));
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
b[x][y] = new JButton(" ");
b[x][y].setBackground(Color.DARK_GRAY);
add(b[x][y]);
b[x][y].setEnabled(false);
}//end inner for
}
this.setFocusable(true);
this.pack();
JPanel panel = (JPanel)getContentPane();
panel.addKeyListener(this);
this.setVisible(true);
panel.requestFocusInWindow();
go();
}
public void go() {
int tmp = 0;
Component temporaryLostComponent = null;
timer.start();
if (layer > 12) {
time = 150 - (iteration * iteration * 2 - iteration);
} else {
time = time - 2.2;
}
iteration++;
layer--;
press = false;
tmp = check();
length[0] = length[1];
length[1] = tmp;
if (layer == -1) {
JOptionPane.showMessageDialog(temporaryLostComponent, "Congratulations! You beat the game!");
repeat();
}
if (length[1] <= 0) {
JOptionPane.showMessageDialog(temporaryLostComponent, "Game over! You reached line " + (18 - layer) + "!");
repeat();
}
last = deltax[1];
start = false;
//go();
}
public int check() {
if (start == true) {
return length[1];
} else if (last < deltax[1]) {
if (deltax[1] + length[1] - 1 <= last + length[0] - 1) {
return length[1];
} else {
return length[1] - Math.abs((deltax[1] + length[1]) - (last + length[0]));
}
} else if (last > deltax[1]) {
return length[1] - Math.abs(deltax[1] - last);
} else {
return length[1];
}
}
public void forward() {
deltax[0] = deltax[1];
deltax[1]++;
}
public void back() {
deltax[0] = deltax[1];
deltax[1]--;
}
public void draw() {
for (int x = 0; x < length[1]; x++) {
b[x + deltax[0]][layer].setBackground(Color.DARK_GRAY);
}
for (int x = 0; x < length[1]; x++) {
b[x + deltax[1]][layer].setBackground(Color.CYAN);
}
}
public void repeat() {
if (JOptionPane.showConfirmDialog(null, "PLAY AGAIN?", "WARNING", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
dispose();
new Stacker();
} else {
System.exit(0);
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
System.out.println("Pressed");
press = true;
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
}
Notice the SwingUtilities.invokeLater in the main. That's how you can start up the program on the EDT. The link on Concurrency In Swing will give you more information.

Java Window pulse-fader works once then breaks

Ok, I've got some code I setup to create a simple little overlay window to use as an alert message for a program I'm working on. Everything works fine the first run through, but trying to run through it again, it freezes the whole thing, forcing me to terminate it via the debugger or task manager. I know I'm doing something wrong, I'm just not sure what, due to my limited experience with Java.
Below is the code I use to setup my window and place it in the lower-right corner above the taskbar:
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public static JWindow alertWindow() {
JWindow newWin = new JWindow();
JPanel panel = new JPanel();
BufferedImage img = null;
try {
img = ImageIO.read(Main.class.getResource("/images/test.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
JLabel imgLbl = new JLabel(new ImageIcon(img));
panel.add(imgLbl);
newWin.setContentPane(panel);
newWin.pack();
Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(newWin.getGraphicsConfiguration());
int taskBar = scnMax.bottom;
int x = screenSize.width - newWin.getWidth();
int y = screenSize.height - taskBar - newWin.getHeight();
newWin.setLocation(x,y);
newWin.setVisible(true);
final PulseWindow pulseWin = new PulseWindow(newWin);
pulseWin.getWindow().addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent click) {
if(SwingUtilities.isRightMouseButton(click)) {
pulseWin.stopPulsing();
pulseWin.destroyPulse();
} else {
System.out.println(pulseWin.isPulsing());
if(pulseWin.isPulsing()) {pulseWin.stopPulsing();}
else {pulseWin.startPulse();}
}
}
#Override
public void mouseEntered(MouseEvent arg0) {}
#Override
public void mouseExited(MouseEvent arg0) {}
#Override
public void mousePressed(MouseEvent arg0) {}
#Override
public void mouseReleased(MouseEvent arg0) {}
});
pulseWin.startPulsing();
return newWin;
}
And below is the code I've setup to make it pulse to draw the user's attention:
import javax.swing.JWindow;
public class PulseWindow {
private boolean pulse = true;
private boolean doPulse = true;
private Float floor = 0.50f;
private JWindow win;
public PulseWindow(JWindow win) {
this.win = win;
}
public void startPulsing() {
pulse = true;
boolean decreasing = true;
double inc2 = 0.03;
double current = win.getOpacity();
while(pulse) {
if(doPulse) {
if(decreasing) {
current = current - inc2;
if((float) current <= floor) {
current = floor;
win.setOpacity((float) current);
decreasing = false;
} else {
win.setOpacity((float) current);
}
} else {
current = current + inc2;
if((float) current >= 1.0f) {
current = 1.0;
win.setOpacity((float) current);
decreasing = true;
} else {
win.setOpacity((float) current);
}
}
} else {
current = 1.0;
win.setOpacity(1.0f);
decreasing = true;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
win.setOpacity(1.0f);
}
public void destroyPulse() {
pulse = false;
win.dispose();
}
public boolean isPulsing() { return doPulse; }
public void setFloor(float floor) { this.floor = floor; }
public void stopPulsing() { doPulse = false; }
public void startPulse() { doPulse = true; }
public JWindow getWindow() { return win; }
}
Anyway, like I mentioned, it works fine for the first use, but as soon as you close the window via the right-click then attempt to re-run it later (whether by calling the startPulsing() method or by completely reinitializing the whole class with a new JWindow by calling alertWindow() again), the whole program freezes. Any ideas why this is?
Like I said, I'm still a bit of a newbie to Java, so if you see anything else I'm doing wrong/inefficiently, as well, feel free to point it out so I can do it correctly.
Edit:
I'm starting to think the issue is with JWindows, now. I setup some other code for a different method of displaying the alert and, while it doesn't freeze this time, it doesn't work as intended, either.
public class AlertWindow extends JWindow {
private static Border compound = BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder());
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public AlertWindow() {
JPanel panel = new JPanel();
panel.setBorder(compound);
panel.setBackground(Color.RED);
JLabel imgLbl = new JLabel("Enter Alert Msg Here!");
imgLbl.setFont(new Font(null,Font.BOLD,16));
panel.add(imgLbl);
setContentPane(panel);
pack();
this.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent click) {
if(SwingUtilities.isLeftMouseButton(click)) {
scrollOff();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
scrollOn();
}
}
#Override
public void mouseEntered(MouseEvent arg0) {}
#Override
public void mouseExited(MouseEvent arg0) {}
#Override
public void mousePressed(MouseEvent arg0) {}
#Override
public void mouseReleased(MouseEvent arg0) {}
});
scrollOn();
}
public void scrollOn() {
Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
int taskBar = scnMax.bottom;
int x = screenSize.width - getWidth();
int yEnd = screenSize.height - taskBar - getHeight();
int yStart = screenSize.height;
setLocation(x,yStart);
setVisible(true);
int current = yStart;
while(current > yEnd) {
current-=2;
System.out.println(current);
setLocation(x,current);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void scrollOff() {
int x = screenSize.width - getWidth();
int yEnd = screenSize.height;
int yStart = this.getBounds().y;
setLocation(x,yStart);
int current = yStart;
while(current < yEnd) {
current+=2;
setLocation(x,current);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
setVisible(false);
}
}
Just like the pulsing window issue, it works as intended the first time, then breaks on subsequent uses. In this case, the only thing that breaks is the scrollOn() command. It scrolls on while invisible, then becomes visible once it reaches its destination. The console output of the position clearly shows that it's moving, but you can't see it until it stops moving.
Edit 2:
And back to feeling dumb... I found the issue (actually found it some time ago but forgot to update this...). The issue ended up being that I was only using the runnable and not placing it inside of a new Thread() object. For some reason I was thinking runnable objects created their own new threads, but once I figured out my mistake, it was an easy fix. Obviously I still have a long ways to go in learning Java...
Edit:
Ok, now I'm annoyed... apparently it still breaks if you attempt to run it from an action listener of some kind. My most recent version of the PulseAlert class (below) that calls into the PulseWindow class shown in the original answer further below:
public class PulseAlert {
private static Border compound = BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createLoweredBevelBorder());
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public void runAlert() throws InterruptedException {
final PulseWindow pulseWin = new PulseWindow(alertWindow());
pulseWin.getWindow().addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent click) {
if(SwingUtilities.isRightMouseButton(click)) {
pulseWin.stopPulsing();
pulseWin.destroyPulse();
} else if(SwingUtilities.isLeftMouseButton(click) && pulseWin.isPulsing()) {
pulseWin.stopPulsing();
} else if(SwingUtilities.isLeftMouseButton(click) && !pulseWin.isPulsing()) {
pulseWin.startPulsing();
}
}
#Override
public void mouseEntered(MouseEvent arg0) {}
#Override
public void mouseExited(MouseEvent arg0) {}
#Override
public void mousePressed(MouseEvent arg0) {}
#Override
public void mouseReleased(MouseEvent arg0) {}
});
try {
pulseWin.startPulse();
} catch (Exception e) {
e.printStackTrace();
}
while(pulseWin.pulserActive()) {
Thread.sleep(100);
}
System.out.println("done with second SW");
}
public static JWindow alertWindow() {
System.out.println("Start");
JWindow newWin = new JWindow();
JPanel panel = new JPanel();
panel.setBorder(compound);
panel.setBackground(Color.RED);
JLabel imgLbl = new JLabel("Enter Alert Msg Here!");
imgLbl.setFont(new Font(null,Font.BOLD,16));
panel.add(imgLbl);
newWin.setContentPane(panel);
newWin.pack();
Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(newWin.getGraphicsConfiguration());
int taskBar = scnMax.bottom;
int x = screenSize.width - newWin.getWidth();
int y = screenSize.height - taskBar - newWin.getHeight();
newWin.setLocation(x,y);
newWin.setVisible(true);
return newWin;
}
}
And below is how I can call up the alert window - repeatedly, if I like, as long as it's outside of an action listener.
PulseAlert alertPulse = new PulseAlert();
alertPulse.runAlert();
The above code works flawlessly until placed into an action listener of some kind such as:
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
alertPulse.runAlert();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
});
Once the runAlert() method is called from an action listener, the whole thing freezes like it did previously. Runs perfectly fine until then. Any ideas what is causing this? Is this a bug in Java or am I doing something wrong?
Original Answer:
Ok, I feel pretty dumb, now. All I had to do to fix the issue was place the startPulsing() contents into a new runnable and it all works, and as many times as I need it to.
public void startPulsing() throws Exception {
new Runnable() {
#Override
public void run() {
pulse = true;
win.setVisible(true);
boolean decreasing = true;
double inc = 0.05;
double current = win.getOpacity();
while(pulse) {
if(doPulse) {
if(decreasing) {
current = current - inc;
if((float) current <= floor) {
current = floor;
win.setOpacity((float) current);
decreasing = false;
} else {
win.setOpacity((float) current);
}
} else {
current = current + inc;
if((float) current >= 1.0f) {
current = 1.0;
win.setOpacity((float) current);
decreasing = true;
} else {
win.setOpacity((float) current);
}
}
} else {
current = 1.0;
win.setOpacity(1.0f);
decreasing = true;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
win.setOpacity(1.0f);
}
}.run();
}

Arrow Sorrow Game using multiple java concepts

This is my code for a simple arrow sequencing application.
It generates a random sequence of size 4 which comprise of UP, DOWN, LEFT and/or RIGHT arrow keys and displays them one at a time. If they user repeats the sequence correctly, it displays another sequence of 5 keys. The size of the sequence keeps incrementing as long as the user enters the correct sequence but decrements if an invalid sequence has been entered.
The problem I'm encountering is, the first execution is flawless, it even displays the sequence the second time, but it doesn't accept my keyboard inputs during the second iteration.
For a better understanding of the problem I have broken it down into the main action performing blocks and the full code as well at the end.
Sequence Generation
for(int flag=1;flag<size;flag++)
{
random = randomGenerator.nextInt(4);
generated[flag]=random;
setVisible(true);
switch(random)
{
case 0:
left();
break;
case 1:
up();
break;
case 2:
right();
break;
case 3:
down();
break;
}
delaybig();
}
User Input Sequence
public void keyPressed(KeyEvent e)
{
if(cflag<=size)
{
if(e.getKeyCode()==37)
{
entered[cflag]=0;
cflag++;
Keys.setText("LEFT");
left();
}
else if(e.getKeyCode()==38)
{
entered[cflag]=1;
cflag++;
Keys.setText("UP");
up();
}
else if(e.getKeyCode()==39)
{
entered[cflag]=2;
cflag++;
Keys.setText("RIGHT");
right();
}
else if(e.getKeyCode()==40)
{
entered[cflag]=3;
cflag++;
Keys.setText("DOWN");
down();
}
else
{
Keys.setText("INVALID");
}
}
Sequence Comparision using Arrays.equals
if(cflag==size)
{
boolean check = Arrays.equals(generated, entered);
if(check)
{
delaysmall();
Keys.setText("PERFECT");
size++;
cflag=1;
delaysmall();
Keys.setText("GO AGAIN");
delaybig();
restart();
}
else
{
delaysmall();
Keys.setText("FAILED");
if(size>5)
{
delaybig();
restart();
size--;
cflag=1;
}
}
}
Looping Thread
public void restart()
{
Thread goagain = new Thread()
{
#Override
public void run()
{
launchframe();
}
};
goagain.start();
}
I've spent quite some time on this to no avail so here is the full code just incase you suspect the error likes elsewhere.
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Arrays;
class ArrowSorrow extends Frame implements KeyListener{
int flag;
Image img;
int random;
int cflag=1,size=5;
boolean ShowImage=true;
int entered[]=new int[50];
int generated[]=new int[50];
TextField Keys=new TextField(8);
Random randomGenerator = new Random();
MediaTracker mt = new MediaTracker(this);
public ArrowSorrow(String title)
{
super(title);
}
public void restart()
{
// Create a new thread
Thread goagain = new Thread()
{
// Override run() to provide the running behavior of this thread.
#Override
public void run()
{
launchframe();
}
};
goagain.start();
}
public void launchframe()
{
for(int flag=1;flag<1;flag++)
generated[flag]=0;
for(int flag=1;flag<1;flag++)
entered[flag]=0;
splash();
add(Keys);
delaybig();
setSize(400,400);
Keys.setEditable(false);
Keys.setText("MEMORIZE");
Keys.addKeyListener(this);
setBackground(Color.black);
setLayout(new FlowLayout());
for(int flag=1;flag<size;flag++)
{
random = randomGenerator.nextInt(4);
generated[flag]=random;
setVisible(true);
switch(random)
{
case 0:
left();
break;
case 1:
up();
break;
case 2:
right();
break;
case 3:
down();
break;
}
delaybig();
}
String sequence=new String("");
for(flag=1;flag<size;flag++)
{
sequence=sequence+(Integer.toString(generated[flag]));
}
Keys.setText(sequence);
delaysmall();
Keys.setText("REPEAT");
delaysmall();
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
dispose();
}
});
}
public void splash()
{
img = ToolKit.getDefaultToolkit().getImage("image address for splashscreen.jpg");
mt.addImage(img,0);
repaint();
setVisible(true);
}
public void left()
{
img = Toolkit.getDefaultToolkit().getImage("image address for left.jpg");
mt.addImage(img,0);
repaint();
setVisible(true);
}
public void right()
{
img = Toolkit.getDefaultToolkit().getImage("image address for right.jpg");
mt.addImage(img,0);
repaint();
setVisible(true);
}
public void up()
{
img = Toolkit.getDefaultToolkit().getImage("image address for up.jpg");
mt.addImage(img,0);
repaint();
setVisible(true);
}
public void down()
{
img = Toolkit.getDefaultToolkit().getImage("image address down.jpg");
mt.addImage(img,0);
repaint();
setVisible(true);
}
//minor delay
public void delaysmall()
{
try
{
Thread.sleep(600);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
//major delay
public void delaybig()
{
try
{
Thread.sleep(1200);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
public void paint(Graphics g)
{
super.paint(g);
if(img != null)
g.drawImage(img,70,70, this);
else
g.clearRect(0, 0, getSize().width, getSize().height);
}
public void keyPressed(KeyEvent e)
{
if(cflag<size)
{
if(e.getKeyCode()==37)
{
entered[cflag]=0;
cflag++;
Keys.setText("LEFT");
left();
}
else if(e.getKeyCode()==38)
{
entered[cflag]=1;
cflag++;
Keys.setText("UP");
up();
}
else if(e.getKeyCode()==39)
{
entered[cflag]=2;
cflag++;
Keys.setText("RIGHT");
right();
}
else if(e.getKeyCode()==40)
{
entered[cflag]=3;
cflag++;
Keys.setText("DOWN");
down();
}
else
{
Keys.setText("INVALID");
}
}
//comparing generated sequence and user input sequence
if(cflag==size)
{
boolean check = Arrays.equals(generated, entered);
if(check)
{
delaysmall();
Keys.setText("PERFECT");
size++;
cflag=1;
delaysmall();
Keys.setText("GO AGAIN");
delaybig();
restart();
}
else
{
delaysmall();
Keys.setText("FAILED");
if(size>5)
{
delaybig();
restart();
size--;
cflag=1;
}
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
class ArrowSorrowLaunch{
public static void main(String args[])
{
ArrowSorrow instance=new ArrowSorrow("Arrow Sorrow");
instance.launchframe();
}
}
You call launchFrame to start each iteration of the game. However, you are doing a lot of work in launchFrame that should be done only once. You should move that initialization code out of launchFrame and do it only once. In particular you should not repeatedly call Keys.addKeyListener(this); or add multiple window listeners.
change Thread.sleep(int) to Swing Timer, otherwise you'll bothering with Concurency in Swing
Thread.sleep(int) freeze Swing GUI until ended, during this sleep any Mouse or Key events aren't dispatched or consumed
don't paint directly to the JFrame, put there JPanel or JComponent
for JPanel or JComponent have to use paintComponent instead of paint()
don't use KeyListener, use Keybindings instead, otherwise have to setFocusable()
Solution by OP.
Fix, thanks to Ted Hopp.
Created this new method by moving them out of launchframe()
void oneTime()
{
add(Keys);
setSize(400,400);
Keys.requestFocus();
Keys.setEditable(false);
Keys.setText("MEMORIZE");
Keys.addKeyListener(this);
setBackground(Color.black);
setLayout(new FlowLayout());
launchframe();
}
Replaced
instance.launchframe();//this
instance.oneTime();//with this in main

Categories