Gylander
06-12-2009, 01:53 AM
Hey guys, i very new to java so please dont be to harsh.
My problem is that my current image will not change in my play method. I figured out the problem; the repaint(); is not working. I have no idea why not, but the panel never changes the image, it never even goes down to the paint method (only once at the beggining).
Thank you
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class Final_Panel extends JPanel
{
private JButton Red, Blue, Green, Yellow;
private Image image1, image2, image3, image4, image5, image6;
private Image currentImage;
private Random rand;
//--------------------------------------------------------------------------
// Loads the images and sets the panel characteristics.
//--------------------------------------------------------------------------
public Final_Panel()
{
addKeyListener(new KeyboardListener());
image1 = (new ImageIcon("SimonSaysbackground.jpg")).getImage();
image2 = (new ImageIcon("SimonSaysbackgroundYellow.jpg")).getImage();
image3 = (new ImageIcon("SimonSaysbackgroundRed.jpg")).getImage();
image4 = (new ImageIcon("SimonSaysbackgroundBlue.jpg")).getImage();
image5 = (new ImageIcon("SimonSaysbackgroundGreen.jpg")).getImage();
image6 = (new ImageIcon("MovingBeans.jpg")).getImage();
currentImage = image1;
rand = new Random();
setBackground(Color.black);
setPreferredSize(new Dimension(422, 422));
setFocusable(true);
}
public void play(){
int x = 0;
int counter = 0;
int i;
int[] data = new int[1000];
int[] Sequence = new int[1000];
while (x == 0)
{
while (counter < 999)
{
counter = counter + 1;
data[counter] = (int)(4* Math.random() + 1);
for (i = 0; i <= counter; i=i+1)
{
System.out.print(data[i]);
if (data[i] == 1)
{
currentImage = image2;
repaint();
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
currentImage = image1;
repaint();
}
else if (data[i] == 2)
{
currentImage = image3;
repaint();
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
currentImage = image1;
repaint();
}
else if (data[i] == 3)
{
currentImage = image4;
repaint();
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
currentImage = image1;
repaint();
}
else
{
currentImage = image5;
repaint();
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
currentImage = image1;
repaint();
}
}
System.out.println(" ");
}
}
}
//--------------------------------------------------------------------------
// Draws the image on the panel.
//--------------------------------------------------------------------------
public void paintComponent(Graphics gc)
{
super.paintComponent(gc);
gc.drawImage(currentImage, 0, 0, null);
}
//**************************************************************************
// The listener for keyboard presses.
//**************************************************************************
private class KeyboardListener implements KeyListener
{
//-----------------------------------------------------------------------
// Switches the current image based on the key pressed.
//-----------------------------------------------------------------------
public void keyPressed(KeyEvent event)
{
switch (event.getKeyChar())
{
case '1':
currentImage = image1;
break;
case '2':
currentImage = image2;
break;
case '3':
currentImage = image3;
break;
case '4':
currentImage = image4;
break;
case '5':
currentImage = image5;
break;
case '6':
currentImage = image6;
break;
case ' ':
switch (rand.nextInt(6))
{
case 0:
currentImage = image1;
break;
case 1:
currentImage = image2;
break;
case 2:
currentImage = image3;
}
}
repaint();
}
public void keyReleased(KeyEvent event) { }
public void keyTyped(KeyEvent event) { }
}
}
My problem is that my current image will not change in my play method. I figured out the problem; the repaint(); is not working. I have no idea why not, but the panel never changes the image, it never even goes down to the paint method (only once at the beggining).
Thank you
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class Final_Panel extends JPanel
{
private JButton Red, Blue, Green, Yellow;
private Image image1, image2, image3, image4, image5, image6;
private Image currentImage;
private Random rand;
//--------------------------------------------------------------------------
// Loads the images and sets the panel characteristics.
//--------------------------------------------------------------------------
public Final_Panel()
{
addKeyListener(new KeyboardListener());
image1 = (new ImageIcon("SimonSaysbackground.jpg")).getImage();
image2 = (new ImageIcon("SimonSaysbackgroundYellow.jpg")).getImage();
image3 = (new ImageIcon("SimonSaysbackgroundRed.jpg")).getImage();
image4 = (new ImageIcon("SimonSaysbackgroundBlue.jpg")).getImage();
image5 = (new ImageIcon("SimonSaysbackgroundGreen.jpg")).getImage();
image6 = (new ImageIcon("MovingBeans.jpg")).getImage();
currentImage = image1;
rand = new Random();
setBackground(Color.black);
setPreferredSize(new Dimension(422, 422));
setFocusable(true);
}
public void play(){
int x = 0;
int counter = 0;
int i;
int[] data = new int[1000];
int[] Sequence = new int[1000];
while (x == 0)
{
while (counter < 999)
{
counter = counter + 1;
data[counter] = (int)(4* Math.random() + 1);
for (i = 0; i <= counter; i=i+1)
{
System.out.print(data[i]);
if (data[i] == 1)
{
currentImage = image2;
repaint();
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
currentImage = image1;
repaint();
}
else if (data[i] == 2)
{
currentImage = image3;
repaint();
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
currentImage = image1;
repaint();
}
else if (data[i] == 3)
{
currentImage = image4;
repaint();
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
currentImage = image1;
repaint();
}
else
{
currentImage = image5;
repaint();
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
currentImage = image1;
repaint();
}
}
System.out.println(" ");
}
}
}
//--------------------------------------------------------------------------
// Draws the image on the panel.
//--------------------------------------------------------------------------
public void paintComponent(Graphics gc)
{
super.paintComponent(gc);
gc.drawImage(currentImage, 0, 0, null);
}
//**************************************************************************
// The listener for keyboard presses.
//**************************************************************************
private class KeyboardListener implements KeyListener
{
//-----------------------------------------------------------------------
// Switches the current image based on the key pressed.
//-----------------------------------------------------------------------
public void keyPressed(KeyEvent event)
{
switch (event.getKeyChar())
{
case '1':
currentImage = image1;
break;
case '2':
currentImage = image2;
break;
case '3':
currentImage = image3;
break;
case '4':
currentImage = image4;
break;
case '5':
currentImage = image5;
break;
case '6':
currentImage = image6;
break;
case ' ':
switch (rand.nextInt(6))
{
case 0:
currentImage = image1;
break;
case 1:
currentImage = image2;
break;
case 2:
currentImage = image3;
}
}
repaint();
}
public void keyReleased(KeyEvent event) { }
public void keyTyped(KeyEvent event) { }
}
}