Sign in to follow this  
iccaros

Java Swing Question

Recommended Posts

ok I have a simple pick a number game..

I want the screen background to change color when they are close or too far (blue for close, red for far), I got this no problem

I set up an array of type color

private final Color color[] = { Color.RED, Color.BLUE, Color.GREEN,
Color.GRAY };

now I have tried two ways in my event handler to make it flash all the color (so they can be seen) before going to the information color (this is so you see that your input when in.. or feedback )

here is method one

  else if (compairNumber > numberToGuess
&& compairNumber <= numberToGuess + 2) {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 100000; y++)
;
getContentPane().setBackground(color[x]);
}
getContentPane().setBackground(color[1]);

method two

else if (compairNumber < numberToGuess
&& compairNumber < numberToGuess - 2)
{
getContentPane().setBackground(color[1]);
for(int x = 0; x > 1000000;x++)
;
getContentPane().setBackground(color[2]);
for(int x = 0; x < 1000000;x++)
;
getContentPane().setBackground(color[3]);
for(int x = 0; x < 1000000;x++)
;
getContentPane().setBackground(color[0]);
}

so what I need is for the windows to flash diffrent colors for a second before staying on one color

Edited by iccaros

Share this post


Link to post
Share on other sites

javax.swing.Timer. Write the animation object as a little state machine, set the timer interval to the update frequency of the animation. Each tick the timer calls actionPerformed() on the object, the object changes the background color and transitions the next state. When it exits the final state it stops the timer.

Share this post


Link to post
Share on other sites

??? I'll look up javax.swing.Timer

but I did not understand much of that..

This is what I think I understand is set a timer trigered by my event that usese background change as an animation. and when the animation is done let the use enter another number..

sorry I can't post more code untill monday..

Share this post


Link to post
Share on other sites
This is what I think I understand is set a timer trigered by my event that usese background change as an animation. and when the animation is done let the use enter another number..

Something like this (from memory, haven't tested it):

class C implements ActionListener {
private int state = 0;

public void actionPerformed(ActionEvent e) {
switch (state) {
case 0:
// set color
// increment state
break;
case 1:
// set color
// increment state
break;
case // last state
// set final color
// disable timer. ((Timer)e.getSource()).stop() might work.
}
}
}

// elsewhere
int interval = // update frequency in milliseconds
Timer t = new Timer(interval, new C());
t.start()

That's a classic procedural state machine. There are more object-oriented ways to do it but I think my fingers would fall off if I tried to use them.

Edit: A network somewhere in the back of my brain is screaming that this approach is all wrong but I've been doing so little OOP recently that I can't understand what it's saying. There's an obvious alternative that I'm missing....

Edited by jcl

Share this post


Link to post
Share on other sites

well here is my code.. had to wait as it was a schools assinment, but I wanted to make it better (I'm learnin here)

in case some one does not understand java, the file name need to be the same as the class or GuessGamePanel.java in this case.. the main method can be named what ever you want.

import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.util.Random;
import javax.swing.JOptionPane;

public class GuessGamePanel extends JFrame {
private JButton inButton;

private JButton restartButton;

private JTextField userInput;

private JLabel label1;

private JLabel blueLabel;

private JLabel redLabel;

private final Color color[] = { Color.RED, Color.BLUE, Color.GREEN,
Color.GRAY };

public int numberToGuess;

public int compairNumber;

public int counter;

// construtor
public GuessGamePanel() {
super("Number Guessing game by William Huskey");
setLayout(new FlowLayout());// set layout of JFrame componets
getContentPane().setBackground(color[3]);

counter = 1;

label1 = new JLabel("Enter a Guess from 1 to 10");
add(label1);

userInput = new JTextField(2);
userInput.setEditable(true);
add(userInput);

inButton = new JButton("OK");
add(inButton);

restartButton = new JButton("Restart Game");
add(restartButton);

numberToGuess = guessNumber();// create random number

redLabel = new JLabel("the Color Red Means you are not close");
add(redLabel);

blueLabel = new JLabel("The Color Blue means you are close");
add(blueLabel);

// create Handling for Buttons
ButtonHandler bHandler = new ButtonHandler();
inButton.addActionListener(bHandler);
restartButton.addActionListener(bHandler);

TextHandler tHandler = new TextHandler();
userInput.addActionListener(tHandler);

}// end GuessGamePanel Construtor

// Button events
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == inButton) {
checkWin();
}// endif

else if (event.getSource() == restartButton) {
userInput.setEditable(true);
numberToGuess = guessNumber();// create random number
counter = 1;
}
}
}// end ButtonHandler

// JText events --aka press enter
private class TextHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
checkWin();
}
}// end TextHandler

public int guessNumber() {
Random number = new Random();
return 1 + number.nextInt(10);
}// end methid GuessNumber

// check to see if user inputed a winning guess
public void checkWin() {
compairNumber = Integer.parseInt(userInput.getText());

if (compairNumber == numberToGuess) {
getContentPane().setBackground(color[2]);
userInput.setEditable(false);
JOptionPane.showMessageDialog(null, "Press Restart To Play Agian",
"You Win!!!", JOptionPane.PLAIN_MESSAGE);}

else if (compairNumber > numberToGuess
&& compairNumber > numberToGuess + 2)
{
repaint();
getContentPane().setBackground(color[0]);
}


else if (compairNumber < numberToGuess
&& compairNumber < numberToGuess - 2)
{
repaint();
getContentPane().setBackground(color[0]);
}


else if (compairNumber > numberToGuess
&& compairNumber <= numberToGuess + 2)
getContentPane().setBackground(color[1]);

else if (compairNumber < numberToGuess
&& compairNumber >= numberToGuess - 2)
getContentPane().setBackground(color[1]);

if (counter == 1) {

for (int x = 0; x < 4; x++)
getContentPane().setBackground(color[x]);
getContentPane().setBackground(color[0]);// overrides close
// colors on first try
counter = 0;
}
}
}// end Class GuessGamePanel

and here is the main class to run it

import javax.swing.JFrame;
public class william_huskey_program5
{
public static void main(String[] args)
{
GuessGamePanel guessGame = new GuessGamePanel();
guessGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guessGame.setSize(450,150);
guessGame.setVisible(true);

}//end main

}//end class

I know I need more comments..

I will try to add what you said JCL.. once I understand it.. that is..

Thanks

Edited by iccaros

Share this post


Link to post
Share on other sites

Little sample, simpler than what I posted above.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Blink
{
static class ColorChanger implements ActionListener
{
private Color[] colors_;
private Container pane_;
private int state_;

public ColorChanger(Container pane, Color[] colors)
{
pane_ = pane;
colors_ = colors;
state_ = 0;
}

public void actionPerformed(ActionEvent e)
{
pane_.setBackground(colors_[state_++]);
if (state_ >= colors_.length) {
((Timer)e.getSource()).stop();
}
}
}

public static void main(String[] args)
{
Runnable r = new Runnable() { public void run() { init(); } };
SwingUtilities.invokeLater(r);
}

public static void init()
{
JFrame.setDefaultLookAndFeelDecorated(true);

Color[] colors = { Color.RED, Color.BLUE, Color.GREEN };

JFrame frame = new JFrame("Blink");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ColorChanger changer = new ColorChanger(frame.getContentPane(), colors);
Timer t = new Timer(500, changer);
// Wait 5 seconds to let you find and resize the window
t.setInitialDelay(5000);
t.start();

frame.pack();
frame.setVisible(true);
}
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this