freelanceprogrammers.org Forum Index » Java
JPanel problem
Joined: 17 Apr 2004
Posts: 28
JPanel problem
I`m having a problem with my JPanel. I tell it to repaint() at the end
of the constructor and then I have animation (one sprite moving on the
screen), but the JPanel doesn`t paint itself until the sprite has gone
through its animation and reached its new point on the screen. I pasted
my code below. I`ve never used a JPanel for custom graphics before.
What am I doing wrong?
-Michael Sullivan-
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
class Battle extends JPanel implements KeyListener
{
AllyParty ap;
boolean showMenu = false;
BufferedImage image;
EnemyParty ep;
Graphics screen;
Image allyPic[], enemyPic[];
int enemyCount;
int mtCount = 0;
int turn[];
MediaTracker mt;
Random rand;
public Battle (AllyParty allyparty)
{
/*Initial JPanel subclass setup*/
setSize(800, 600);
setBackground(Color.black);
/*Create our ally party*/
ap = new AllyParty();
ap = allyparty;
ap.setCallingObject(this);
/*Create randomizer and random number variable*/
long seed = System.currentTimeMillis();
rand = new Random(seed);
/*Use rand to select a number of enemies*/
enemyCount = rand.nextInt(12) + 1;
enemyCount = 12; //TEMP
ep = new EnemyParty(enemyCount);
/*Create the individual enemies in ep and place them*/
for (int i = 0; i < enemyCount; i++)
ep.enemy[i] = new Enemy(1);
ep.setCallingObject(this);
/*Initialize images and set them to their starting values*/
allyPic = new Image[4];
enemyPic = new Image[enemyCount];
/* for (int i = 0; i < 4; i++)
{
ap.ally[i].setCurrentImage(Ally.STAND)
allyPic[i] = ap.ally[i].getCurrentImage();
}
for (int i = 0; i < enemyCount; i++)
{
enemyPic[i] = ep.enemy[i].getImage();
*/
/*Set battle placement*/
ap.setPos(0);
ep.setPos(0);
//Create the Buffered Image
image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
//Set up the Media Tracker
mt = new MediaTracker(this);
for (int i = 0; i < 4; i++)
mt.addImage(allyPic[i], mtCount++);
for (int i = 0; i < ep.getEnemyCount(); i++)
mt.addImage(enemyPic[i], mtCount++);
mt.addImage(image, mtCount++);
try { mt.waitForAll(); } catch (Exception e) {}
/*Temporary Section*/
findTurns();
repaint();
// ap.ally[0].advance();
advance();
}
public void findTurns()
{
int total = ep.getEnemyCount() + 4; //Enemies + allies
int used[] = new int[total];
turn = new int[total];
int next = rand.nextInt(total);
for (int j = 0; j < total; j++)
used[j] = -1;
for (int j = 0; j < total; j++)
{
for (int k = 0; k < j; k++)
{
if (used[k] == next)
{
k = -1;
next = rand.nextInt(total);
}
}
turn[j] = next;
used[j] = next;
}
}
public void advance()
{
while (ap.ally[0].getXPos() > 350)
{
ap.ally[0].moveLeft(20);
ap.ally[0].setCurrentImage(Ally.WALK);
repaint();
try { Thread.sleep(200); } catch (Exception e) {}
ap.ally[0].setCurrentImage(Ally.STAND);
repaint();
try { Thread.sleep(200); } catch (Exception e) {}
}
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
screen = image.getGraphics();
for (int i = 0; i < 4; i++)
allyPic[i] = ap.ally[i].getCurrentImage();
for (int i = 0; i < ep.getEnemyCount(); i++)
enemyPic[i] = ep.enemy[i].getCurrentImage();
for (int i = 0; i < 4; i++)
screen.drawImage(allyPic[i], ap.ally[i].getXPos(),
ap.ally[i].getYPos(), this);
for (int i = 0; i < enemyCount; i++)
screen.drawImage(enemyPic[i], ep.enemy[i].getXPos(),
ep.enemy[i].getYPos(), 100, 75, this);
g.drawImage(image, 0, 0, this);
}
public void keyPressed (KeyEvent k) {}
public void keyReleased (KeyEvent k) {}
public void keyTyped (KeyEvent k) {}
}
Joined: 17 Apr 2004
Posts: 28
JPanel problem
I think I`ve isolated the problem. Being new to Swing I didn`t realize
that Thread.sleep(int) was not swing friendly. I`ve decided to
implement a javax.swing.timer to repaint the JPanel. The problem is I
can`t think of anything to keep my animation paused until the user has
had time to see the new frame. I was using Thread.sleep(200) for this,
but I can`t use Thread.sleep in a Swing interface because it ties up the
entire program. I`ve tried while (someBoolean == false) {}, but that
had the same effect as Thread.sleep(). Any ideas on how to do this?
I`ve pasted my new code snippets below.
-Michael Sullivan-
doNow = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
repaint();
}
};
timer = new javax.swing.Timer(200, doNow);
timer.start();
advance();
public void advance()
{
while (ap.ally[0].getXPos() > 350)
{
ap.ally[0].moveLeft(20);
ap.ally[0].setCurrentImage(Ally.WALK);
//Need to wait at this point for user to see new image
ap.ally[0].setCurrentImage(Ally.STAND);
repaint();
//Likewise here;
}
}
On Sun, 2004-04-25 at 23:27, Michael Sullivan wrote:
> I`m having a problem with my JPanel. I tell it to repaint() at the end
> of the constructor and then I have animation (one sprite moving on the
> screen), but the JPanel doesn`t paint itself until the sprite has gone
> through its animation and reached its new point on the screen. I pasted
> my code below. I`ve never used a JPanel for custom graphics before.
> What am I doing wrong?
>
> -Michael Sullivan-
>
> import java.awt.*;
> import java.awt.event.*;
> import java.awt.image.*;
> import java.util.*;
> import javax.swing.*;
>
> class Battle extends JPanel implements KeyListener
> {
> AllyParty ap;
>
> boolean showMenu = false;
>
> BufferedImage image;
>
> EnemyParty ep;
>
> Graphics screen;
>
> Image allyPic[], enemyPic[];
>
> int enemyCount;
> int mtCount = 0;
> int turn[];
>
> MediaTracker mt;
>
> Random rand;
>
> public Battle (AllyParty allyparty)
> {
>
> /*Initial JPanel subclass setup*/
> setSize(800, 600);
> setBackground(Color.black);
>
> /*Create our ally party*/
> ap = new AllyParty();
> ap = allyparty;
> ap.setCallingObject(this);
>
> /*Create randomizer and random number variable*/
> long seed = System.currentTimeMillis();
> rand = new Random(seed);
>
> /*Use rand to select a number of enemies*/
> enemyCount = rand.nextInt(12) + 1;
>
> enemyCount = 12; //TEMP
> ep = new EnemyParty(enemyCount);
>
>
> /*Create the individual enemies in ep and place them*/
> for (int i = 0; i < enemyCount; i++)
> ep.enemy[i] = new Enemy(1);
>
> ep.setCallingObject(this);
>
>
>
>
> /*Initialize images and set them to their starting values*/
> allyPic = new Image[4];
> enemyPic = new Image[enemyCount];
>
> /* for (int i = 0; i < 4; i++)
> {
> ap.ally[i].setCurrentImage(Ally.STAND)
> allyPic[i] = ap.ally[i].getCurrentImage();
> }
>
> for (int i = 0; i < enemyCount; i++)
> {
> enemyPic[i] = ep.enemy[i].getImage();
> */
> /*Set battle placement*/
> ap.setPos(0);
> ep.setPos(0);
>
> //Create the Buffered Image
> image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
>
> //Set up the Media Tracker
> mt = new MediaTracker(this);
> for (int i = 0; i < 4; i++)
> mt.addImage(allyPic[i], mtCount++);
>
> for (int i = 0; i < ep.getEnemyCount(); i++)
> mt.addImage(enemyPic[i], mtCount++);
>
> mt.addImage(image, mtCount++);
>
> try { mt.waitForAll(); } catch (Exception e) {}
>
> /*Temporary Section*/
> findTurns();
>
> repaint();
> // ap.ally[0].advance();
> advance();
> }
>
> public void findTurns()
> {
> int total = ep.getEnemyCount() + 4; //Enemies + allies
>
> int used[] = new int[total];
> turn = new int[total];
>
> int next = rand.nextInt(total);
>
> for (int j = 0; j < total; j++)
> used[j] = -1;
>
> for (int j = 0; j < total; j++)
> {
> for (int k = 0; k < j; k++)
> {
> if (used[k] == next)
> {
> k = -1;
> next = rand.nextInt(total);
> }
> }
>
> turn[j] = next;
> used[j] = next;
>
> }
> }
>
> public void advance()
> {
> while (ap.ally[0].getXPos() > 350)
> {
> ap.ally[0].moveLeft(20);
> ap.ally[0].setCurrentImage(Ally.WALK);
> repaint();
> try { Thread.sleep(200); } catch (Exception e) {}
> ap.ally[0].setCurrentImage(Ally.STAND);
> repaint();
> try { Thread.sleep(200); } catch (Exception e) {}
> }
> }
>
>
> public void paintComponent (Graphics g)
> {
> super.paintComponent(g);
> screen = image.getGraphics();
>
> for (int i = 0; i < 4; i++)
> allyPic[i] = ap.ally[i].getCurrentImage();
>
> for (int i = 0; i < ep.getEnemyCount(); i++)
> enemyPic[i] = ep.enemy[i].getCurrentImage();
>
> for (int i = 0; i < 4; i++)
> screen.drawImage(allyPic[i], ap.ally[i].getXPos(),
> ap.ally[i].getYPos(), this);
>
> for (int i = 0; i < enemyCount; i++)
> screen.drawImage(enemyPic[i], ep.enemy[i].getXPos(),
> ep.enemy[i].getYPos(), 100, 75, this);
>
> g.drawImage(image, 0, 0, this);
> }
>
>
>
>
> public void keyPressed (KeyEvent k) {}
> public void keyReleased (KeyEvent k) {}
> public void keyTyped (KeyEvent k) {}
> }
>
Joined: 16 Apr 2004
Posts: 15
JPanel problem
I would say read up on invokeLater() method. I never got it straight,
but I believe it might help you some ...
>>> michael@... 4/26/2004 10:20:59 AM >>>
I think I`ve isolated the problem. Being new to Swing I didn`t
realize
that Thread.sleep(int) was not swing friendly. I`ve decided to
implement a javax.swing.timer to repaint the JPanel. The problem is I
can`t think of anything to keep my animation paused until the user has
had time to see the new frame. I was using Thread.sleep(200) for
this,
but I can`t use Thread.sleep in a Swing interface because it ties up
the
entire program. I`ve tried while (someBoolean == false) {}, but that
had the same effect as Thread.sleep(). Any ideas on how to do this?
I`ve pasted my new code snippets below.
-Michael Sullivan-
doNow = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
repaint();
}
};
timer = new javax.swing.Timer(200, doNow);
timer.start();
advance();
public void advance()
{
while (ap.ally[0].getXPos() > 350)
{
ap.ally[0].moveLeft(20);
ap.ally[0].setCurrentImage(Ally.WALK);
//Need to wait at this point for user to see new image
ap.ally[0].setCurrentImage(Ally.STAND);
repaint();
//Likewise here;
}
}
On Sun, 2004-04-25 at 23:27, Michael Sullivan wrote:
> I`m having a problem with my JPanel. I tell it to repaint() at the
end
> of the constructor and then I have animation (one sprite moving on
the
> screen), but the JPanel doesn`t paint itself until the sprite has
gone
> through its animation and reached its new point on the screen. I
pasted
> my code below. I`ve never used a JPanel for custom graphics before.
> What am I doing wrong?
>
> -Michael Sullivan-
>
> import java.awt.*;
> import java.awt.event.*;
> import java.awt.image.*;
> import java.util.*;
> import javax.swing.*;
>
> class Battle extends JPanel implements KeyListener
> {
> AllyParty ap;
>
> boolean showMenu = false;
>
> BufferedImage image;
>
> EnemyParty ep;
>
> Graphics screen;
>
> Image allyPic[], enemyPic[];
>
> int enemyCount;
> int mtCount = 0;
> int turn[];
>
> MediaTracker mt;
>
> Random rand;
>
> public Battle (AllyParty allyparty)
> {
>
> /*Initial JPanel subclass setup*/
> setSize(800, 600);
> setBackground(Color.black);
>
> /*Create our ally party*/
> ap = new AllyParty();
> ap = allyparty;
> ap.setCallingObject(this);
>
> /*Create randomizer and random number variable*/
> long seed = System.currentTimeMillis();
> rand = new Random(seed);
>
> /*Use rand to select a number of enemies*/
> enemyCount = rand.nextInt(12) + 1;
>
> enemyCount = 12; //TEMP
> ep = new EnemyParty(enemyCount);
>
>
> /*Create the individual enemies in ep and place them*/
> for (int i = 0; i < enemyCount; i++)
> ep.enemy[i] = new Enemy(1);
>
> ep.setCallingObject(this);
>
>
>
>
> /*Initialize images and set them to their starting values*/
> allyPic = new Image[4];
> enemyPic = new Image[enemyCount];
>
> /* for (int i = 0; i < 4; i++)
> {
> ap.ally[i].setCurrentImage(Ally.STAND)
> allyPic[i] = ap.ally[i].getCurrentImage();
> }
>
> for (int i = 0; i < enemyCount; i++)
> {
> enemyPic[i] = ep.enemy[i].getImage();
> */
> /*Set battle placement*/
> ap.setPos(0);
> ep.setPos(0);
>
> //Create the Buffered Image
> image = new BufferedImage(800, 600,
BufferedImage.TYPE_INT_RGB);
>
> //Set up the Media Tracker
> mt = new MediaTracker(this);
> for (int i = 0; i < 4; i++)
> mt.addImage(allyPic[i], mtCount++);
>
> for (int i = 0; i < ep.getEnemyCount(); i++)
> mt.addImage(enemyPic[i], mtCount++);
>
> mt.addImage(image, mtCount++);
>
> try { mt.waitForAll(); } catch (Exception e) {}
>
> /*Temporary Section*/
> findTurns();
>
> repaint();
> // ap.ally[0].advance();
> advance();
> }
>
> public void findTurns()
> {
> int total = ep.getEnemyCount() + 4; //Enemies + allies
>
> int used[] = new int[total];
> turn = new int[total];
>
> int next = rand.nextInt(total);
>
> for (int j = 0; j < total; j++)
> used[j] = -1;
>
> for (int j = 0; j < total; j++)
> {
> for (int k = 0; k < j; k++)
> {
> if (used[k] == next)
> {
> k = -1;
> next = rand.nextInt(total);
> }
> }
>
> turn[j] = next;
> used[j] = next;
>
> }
> }
>
> public void advance()
> {
> while (ap.ally[0].getXPos() > 350)
> {
> ap.ally[0].moveLeft(20);
> ap.ally[0].setCurrentImage(Ally.WALK);
> repaint();
> try { Thread.sleep(200); } catch (Exception e) {}
> ap.ally[0].setCurrentImage(Ally.STAND);
> repaint();
> try { Thread.sleep(200); } catch (Exception e) {}
> }
> }
>
>
> public void paintComponent (Graphics g)
> {
> super.paintComponent(g);
> screen = image.getGraphics();
>
> for (int i = 0; i < 4; i++)
> allyPic[i] = ap.ally[i].getCurrentImage();
>
> for (int i = 0; i < ep.getEnemyCount(); i++)
> enemyPic[i] = ep.enemy[i].getCurrentImage();
>
> for (int i = 0; i < 4; i++)
> screen.drawImage(allyPic[i], ap.ally[i].getXPos(),
> ap.ally[i].getYPos(), this);
>
> for (int i = 0; i < enemyCount; i++)
> screen.drawImage(enemyPic[i], ep.enemy[i].getXPos(),
> ep.enemy[i].getYPos(), 100, 75, this);
>
> g.drawImage(image, 0, 0, this);
> }
>
>
>
>
> public void keyPressed (KeyEvent k) {}
> public void keyReleased (KeyEvent k) {}
> public void keyTyped (KeyEvent k) {}
> }
>
------------------------ Yahoo! Groups Sponsor
Yahoo! Groups Links
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







