this is a small example that, i hope reflects the larger project that i am working on.
i am making a board game that uses a combination of mouse listener and jbutton action listener for the player to move. when it is time for the computer to take its turn, however, i cannot seem to get the program to update the graphics in between moves. it will do so when the computer has done all of its moves and kicks control back to the player. i am not sure if it is the adding of jbuttons to my jframe that is causing it to repaint or not, but in this small example, it still repaints after the loop is complete even though there is no altering of the jframe or any of the components in it.
i have tried using revalidate() as well and it does not seem to have any effect on it.
here is the code, any insight would be greatly appreciated.
thank you
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;
public class testTwo extends JPanel{
Image ladyBug;
Image iLB;
cmonDoIt testPanel;
JScrollPane testScroll;
JFrame gameFrameTwo;
public static void gameFrameTwo(){
JFrame gameFrameTwo = new JFrame("test");
gameFrameTwo.setPreferredSize(new Dimension(400, 400));
gameFrameTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrameTwo.setResizable(false);
testTwo content = new testTwo();
content.setOpaque(true);
gameFrameTwo.setContentPane(content);
gameFrameTwo.pack();
gameFrameTwo.setVisible(true);
}
public testTwo(){
testPanel = new cmonDoIt();
add(testPanel);
testPanel.addMouseListener(testPanel);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
gameFrameTwo();
}
});
}
class cmonDoIt extends JPanel implements MouseListener{
Image currentView = Toolkit.getDefaultToolkit().getImage("images/LadyBug.GIF");
Image yellow = Toolkit.getDefaultToolkit().getImage("images/yellow.GIF");
boolean showYellow = true;
int yellowX = 100;
int yellowY = 100;
public cmonDoIt(){
}
public Dimension getPreferredSize() {
return new Dimension(600,600);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(currentView, 0, 0, this);
g.drawImage(yellow, yellowX, yellowY, this);
}
public void mousePressed(MouseEvent e) { }
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
wander();
showYellow = true;
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) { }
public void drawStuff(Graphics g, Image img, int x, int y){
g.drawImage(img, x, y, this);
}
public void wander(){
Random generator = new Random();
int randomInt;
for(int i = 0; i < 10; i++){
randomInt = generator.nextInt(8);
switch(randomInt){
case 0:
if(yellowY > 20){
yellowY -= 40;
}
break;
case 1:
if(yellowY > 20){
yellowY -= 40;
}
if(yellowX < 380){
yellowX += 40;
}
break;
case 2:
if(yellowX < 380){
yellowX += 40;
}
break;
case 3:
if(yellowY < 380){
yellowY += 40;
}
if(yellowX < 380){
yellowX += 40;
}
break;
case 4:
if(yellowY < 380){
yellowY += 40;
}
break;
case 5:
if(yellowY < 380){
yellowY += 40;
}
if(yellowX > 100){
yellowX -= 40;
}
break;
case 6:
if(yellowX > 100){
yellowX -= 40;
}
break;
case 7:
if(yellowY > 20){
yellowY -= 40;
}
if(yellowX > 100){
yellowX -= 40;
}
break;
default:
}
System.out.println("X: " + yellowX + ", Y: " + yellowY);
revalidate();
repaint();
Wait.halfSec();
}
}
}
}
i know its working because the println but no dice on the graphics update.