freelanceprogrammers.org Forum Index » Java
Canvas won`t paint
Joined: 17 Apr 2004
Posts: 28
Canvas won`t paint
I recently had to trash the Battle.java file on the game I`m working on
and restart it from scratch. It inherits from Canvas and is added to
the content pane in another class based on JFrame. I haven`t made any
significant changes to the JFrame, but now my Canvas won`t show up. Can
anyone help me find the problem? Is there something I`m missing? I`ve
pasted the code for both classes below...
-Michael Sullivan-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainBattle extends JFrame
{
public static Battle battle;
public MainBattle()
{
this.setSize(600, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
Ally us[] = new Ally[4];
us[0] = new Ally(0, "Michael");
us[1] = new Ally(1, "Fox");
us[2] = new Ally(3, "Fawn");
us[3] = new Ally(4, "Sierra");
AllyParty party = new AllyParty(us);
Container pane = getContentPane();
battle = new Battle(party);
pane.add(battle);
setContentPane(pane);
this.addKeyListener(battle);
// battle.doBattle();
}
public static void main(String args[])
{ new MainBattle(); }
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
class Battle extends Canvas implements KeyListener
{
AllyParty ap;
boolean showMenu = false;
EnemyParty ep;
Image allyPic[], enemyPic[];
int enemyCount;
int turn[];
Random rand;
public Battle (AllyParty allyparty)
{
/*Initial Canvas subclass setup*/
setSize(600, 600);
setBackground(Color.black);
/*Create our ally party*/
ap = new AllyParty();
ap = allyparty;
/*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);
/*Initialize images and set them to their starting values*/
allyPic = new Image[4];
enemyPic = new Image[enemyCount];
for (int i = 0; i < 4; i++)
allyPic[i] = ap.ally[i].getStandPic();
for (int i = 0; i < enemyCount; i++)
enemyPic[i] = ep.enemy[i].getImage();
/*Set battle placement*/
ap.setPos(0);
ep.setPos(0);
/*Temporary Section*/
findTurns();
findActions();
repaint();
}
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 findActions()
{
int total = ep.getEnemyCount() + 4;
for (int i = 0; i < total; i++)
{
if (turn[i] < 4) //Ally
{
if (ap.ally[i].isDead() == false) //They`re alive
{
showMenu = true;
while (showMenu == true)
{
try { Thread.sleep(200); } catch (Exception e) {}
}
}
}
}
}
public void paint(Graphics g)
{
for (int i = 0; i < 4; i++)
g.drawImage(allyPic[i], ap.ally[i].getXPos(),
ap.ally[i].getYPos(), this);
for (int i = 0; i < enemyCount; i++)
g.drawImage(enemyPic[i], ep.enemy[i].getXPos(),
ep.enemy[i].getYPos(), this);
}
public void keyPressed (KeyEvent k) {}
public void keyReleased (KeyEvent k) {}
public void keyTyped (KeyEvent k) {}
}
Joined: 16 Apr 2004
Posts: 15
Canvas won`t paint
Michael,
No matter what anyone tells you :), you should never mix awt and swing.
Even it it works sometimes, or works for a while - it`s gonna break at
some point. Especially when it comes to painting things.
I would try changing JFrame to Frame and add battle straight instead of
adding it to the content pane of the JFrame. That should do it.
Thanx,
- Kalman
>>> michael@... 4/21/2004 4:02:32 PM >>>
I recently had to trash the Battle.java file on the game I`m working
on
and restart it from scratch. It inherits from Canvas and is added to
the content pane in another class based on JFrame. I haven`t made any
significant changes to the JFrame, but now my Canvas won`t show up.
Can
anyone help me find the problem? Is there something I`m missing?
I`ve
pasted the code for both classes below...
-Michael Sullivan-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainBattle extends JFrame
{
public static Battle battle;
public MainBattle()
{
this.setSize(600, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
Ally us[] = new Ally[4];
us[0] = new Ally(0, "Michael");
us[1] = new Ally(1, "Fox");
us[2] = new Ally(3, "Fawn");
us[3] = new Ally(4, "Sierra");
AllyParty party = new AllyParty(us);
Container pane = getContentPane();
battle = new Battle(party);
pane.add(battle);
setContentPane(pane);
this.addKeyListener(battle);
// battle.doBattle();
}
public static void main(String args[])
{ new MainBattle(); }
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
class Battle extends Canvas implements KeyListener
{
AllyParty ap;
boolean showMenu = false;
EnemyParty ep;
Image allyPic[], enemyPic[];
int enemyCount;
int turn[];
Random rand;
public Battle (AllyParty allyparty)
{
/*Initial Canvas subclass setup*/
setSize(600, 600);
setBackground(Color.black);
/*Create our ally party*/
ap = new AllyParty();
ap = allyparty;
/*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);
/*Initialize images and set them to their starting values*/
allyPic = new Image[4];
enemyPic = new Image[enemyCount];
for (int i = 0; i < 4; i++)
allyPic[i] = ap.ally[i].getStandPic();
for (int i = 0; i < enemyCount; i++)
enemyPic[i] = ep.enemy[i].getImage();
/*Set battle placement*/
ap.setPos(0);
ep.setPos(0);
/*Temporary Section*/
findTurns();
findActions();
repaint();
}
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 findActions()
{
int total = ep.getEnemyCount() + 4;
for (int i = 0; i < total; i++)
{
if (turn[i] < 4) //Ally
{
if (ap.ally[i].isDead() == false) //They`re alive
{
showMenu = true;
while (showMenu == true)
{
try { Thread.sleep(200); } catch (Exception e) {}
}
}
}
}
}
public void paint(Graphics g)
{
for (int i = 0; i < 4; i++)
g.drawImage(allyPic[i], ap.ally[i].getXPos(),
ap.ally[i].getYPos(), this);
for (int i = 0; i < enemyCount; i++)
g.drawImage(enemyPic[i], ep.enemy[i].getXPos(),
ep.enemy[i].getYPos(), this);
}
public void keyPressed (KeyEvent k) {}
public void keyReleased (KeyEvent k) {}
public void keyTyped (KeyEvent k) {}
}
------------------------ Yahoo! Groups Sponsor
Yahoo! Groups Links
Joined: 17 Apr 2004
Posts: 28
Canvas won`t paint
The reason MainBattle is a JFrame is because I made a swing interface
that contained Text and Combo boxes to allow the user to create a custom
party and I got tired of going through the selection process every time
I wanted to test battle. Is there a swing substitute for Canvas? Could
I use a JPanel instead???
-Michael Sullivan-
On Wed, 2004-04-21 at 15:09, Kalman Hazins (2) wrote:
> Michael,
>
> No matter what anyone tells you :), you should never mix awt and
> swing.
> Even it it works sometimes, or works for a while - it`s gonna break at
> some point. Especially when it comes to painting things.
>
> I would try changing JFrame to Frame and add battle straight instead
> of
> adding it to the content pane of the JFrame. That should do it.
>
> Thanx,
> - Kalman
>
> >>> michael@... 4/21/2004 4:02:32 PM >>>
> I recently had to trash the Battle.java file on the game I`m working
> on
> and restart it from scratch. It inherits from Canvas and is added to
> the content pane in another class based on JFrame. I haven`t made any
> significant changes to the JFrame, but now my Canvas won`t show up.
> Can
> anyone help me find the problem? Is there something I`m missing?
> I`ve
> pasted the code for both classes below...
>
> -Michael Sullivan-
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
>
> public class MainBattle extends JFrame
> {
> public static Battle battle;
> public MainBattle()
> {
> this.setSize(600, 600);
> this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> this.show();
>
>
> Ally us[] = new Ally[4];
> us[0] = new Ally(0, "Michael");
> us[1] = new Ally(1, "Fox");
> us[2] = new Ally(3, "Fawn");
> us[3] = new Ally(4, "Sierra");
>
> AllyParty party = new AllyParty(us);
> Container pane = getContentPane();
> battle = new Battle(party);
> pane.add(battle);
> setContentPane(pane);
> this.addKeyListener(battle);
> // battle.doBattle();
> }
>
>
> public static void main(String args[])
> { new MainBattle(); }
>
> }
>
>
> import java.awt.*;
> import java.awt.event.*;
> import java.util.*;
> import javax.swing.*;
>
> class Battle extends Canvas implements KeyListener
> {
> AllyParty ap;
>
> boolean showMenu = false;
>
> EnemyParty ep;
>
>
> Image allyPic[], enemyPic[];
>
> int enemyCount;
> int turn[];
>
> Random rand;
>
> public Battle (AllyParty allyparty)
> {
>
> /*Initial Canvas subclass setup*/
> setSize(600, 600);
> setBackground(Color.black);
>
> /*Create our ally party*/
> ap = new AllyParty();
> ap = allyparty;
>
> /*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);
>
>
> /*Initialize images and set them to their starting values*/
> allyPic = new Image[4];
> enemyPic = new Image[enemyCount];
>
> for (int i = 0; i < 4; i++)
> allyPic[i] = ap.ally[i].getStandPic();
>
> for (int i = 0; i < enemyCount; i++)
> enemyPic[i] = ep.enemy[i].getImage();
>
> /*Set battle placement*/
> ap.setPos(0);
> ep.setPos(0);
>
>
>
> /*Temporary Section*/
> findTurns();
> findActions();
>
> repaint();
> }
>
> 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 findActions()
> {
> int total = ep.getEnemyCount() + 4;
>
> for (int i = 0; i < total; i++)
> {
> if (turn[i] < 4) //Ally
> {
> if (ap.ally[i].isDead() == false) //They`re alive
> {
> showMenu = true;
> while (showMenu == true)
> {
> try { Thread.sleep(200); } catch (Exception e) {}
> }
> }
> }
> }
> }
>
>
> public void paint(Graphics g)
> {
> for (int i = 0; i < 4; i++)
> g.drawImage(allyPic[i], ap.ally[i].getXPos(),
> ap.ally[i].getYPos(), this);
>
> for (int i = 0; i < enemyCount; i++)
> g.drawImage(enemyPic[i], ep.enemy[i].getXPos(),
> ep.enemy[i].getYPos(), this);
>
> }
>
>
> public void keyPressed (KeyEvent k) {}
> public void keyReleased (KeyEvent k) {}
> public void keyTyped (KeyEvent k) {}
> }
>
>
>
>
> ------------------------ Yahoo! Groups Sponsor
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
> ______________________________________________________________________
> Yahoo! Groups Links
> * To visit your group on the web, go to:
> http://groups.yahoo.com/group/beginnersclub/
>
> * To unsubscribe from this group, send an email to:
> beginnersclub-unsubscribe@yahoogroups.com
>
> * Your use of Yahoo! Groups is subject to the Yahoo! Terms of
> Service.
>
Joined: 16 Apr 2004
Posts: 15
Canvas won`t paint
Absolutely!!!
JPanel is the swing equivalent of Canvas. But ...
1) The method is not paint though - it`s paintComponent() for JPanel
2) The first line of paintComponent should be this
Graphics2D g2 = (Graphics2D)g;
Frome there on - use g2 and not g.
You might need to change some more stuff.
This link should be very helpful -
http://webdev.apl.jhu.edu/~rbe/java/Java_2D/index_files/frame.html
Enjoy,
- Kalman
>>> michael@... 4/21/2004 4:21:19 PM >>>
The reason MainBattle is a JFrame is because I made a swing interface
that contained Text and Combo boxes to allow the user to create a
custom
party and I got tired of going through the selection process every
time
I wanted to test battle. Is there a swing substitute for Canvas?
Could
I use a JPanel instead???
-Michael Sullivan-
On Wed, 2004-04-21 at 15:09, Kalman Hazins (2) wrote:
> Michael,
>
> No matter what anyone tells you :), you should never mix awt and
> swing.
> Even it it works sometimes, or works for a while - it`s gonna break
at
> some point. Especially when it comes to painting things.
>
> I would try changing JFrame to Frame and add battle straight instead
> of
> adding it to the content pane of the JFrame. That should do it.
>
> Thanx,
> - Kalman
>
> >>> michael@... 4/21/2004 4:02:32 PM >>>
> I recently had to trash the Battle.java file on the game I`m working
> on
> and restart it from scratch. It inherits from Canvas and is added
to
> the content pane in another class based on JFrame. I haven`t made
any
> significant changes to the JFrame, but now my Canvas won`t show up.
> Can
> anyone help me find the problem? Is there something I`m missing?
> I`ve
> pasted the code for both classes below...
>
> -Michael Sullivan-
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
>
> public class MainBattle extends JFrame
> {
> public static Battle battle;
> public MainBattle()
> {
> this.setSize(600, 600);
> this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> this.show();
>
>
> Ally us[] = new Ally[4];
> us[0] = new Ally(0, "Michael");
> us[1] = new Ally(1, "Fox");
> us[2] = new Ally(3, "Fawn");
> us[3] = new Ally(4, "Sierra");
>
> AllyParty party = new AllyParty(us);
> Container pane = getContentPane();
> battle = new Battle(party);
> pane.add(battle);
> setContentPane(pane);
> this.addKeyListener(battle);
> // battle.doBattle();
> }
>
>
> public static void main(String args[])
> { new MainBattle(); }
>
> }
>
>
> import java.awt.*;
> import java.awt.event.*;
> import java.util.*;
> import javax.swing.*;
>
> class Battle extends Canvas implements KeyListener
> {
> AllyParty ap;
>
> boolean showMenu = false;
>
> EnemyParty ep;
>
>
> Image allyPic[], enemyPic[];
>
> int enemyCount;
> int turn[];
>
> Random rand;
>
> public Battle (AllyParty allyparty)
> {
>
> /*Initial Canvas subclass setup*/
> setSize(600, 600);
> setBackground(Color.black);
>
> /*Create our ally party*/
> ap = new AllyParty();
> ap = allyparty;
>
> /*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);
>
>
> /*Initialize images and set them to their starting values*/
> allyPic = new Image[4];
> enemyPic = new Image[enemyCount];
>
> for (int i = 0; i < 4; i++)
> allyPic[i] = ap.ally[i].getStandPic();
>
> for (int i = 0; i < enemyCount; i++)
> enemyPic[i] = ep.enemy[i].getImage();
>
> /*Set battle placement*/
> ap.setPos(0);
> ep.setPos(0);
>
>
>
> /*Temporary Section*/
> findTurns();
> findActions();
>
> repaint();
> }
>
> 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 findActions()
> {
> int total = ep.getEnemyCount() + 4;
>
> for (int i = 0; i < total; i++)
> {
> if (turn[i] < 4) //Ally
> {
> if (ap.ally[i].isDead() == false) //They`re alive
> {
> showMenu = true;
> while (showMenu == true)
> {
> try { Thread.sleep(200); } catch (Exception e) {}
> }
> }
> }
> }
> }
>
>
> public void paint(Graphics g)
> {
> for (int i = 0; i < 4; i++)
> g.drawImage(allyPic[i], ap.ally[i].getXPos(),
> ap.ally[i].getYPos(), this);
>
> for (int i = 0; i < enemyCount; i++)
> g.drawImage(enemyPic[i], ep.enemy[i].getXPos(),
> ep.enemy[i].getYPos(), this);
>
> }
>
>
> public void keyPressed (KeyEvent k) {}
> public void keyReleased (KeyEvent k) {}
> public void keyTyped (KeyEvent k) {}
> }
>
>
>
>
> ------------------------ Yahoo! Groups Sponsor
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
______________________________________________________________________
> Yahoo! Groups Links
> * To visit your group on the web, go to:
> http://groups.yahoo.com/group/beginnersclub/
>
> * To unsubscribe from this group, send an email to:
> beginnersclub-unsubscribe@yahoogroups.com
>
> * Your use of Yahoo! Groups is subject to the Yahoo! Terms of
> Service.
>
------------------------ 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







