freelanceprogrammers.org Forum Index » Java

Canvas won`t paint 2.0


View user's profile Post To page top
msulli1355 Posted: Fri Apr 23, 2004 11:56 pm


Joined: 17 Apr 2004

Posts: 28
Canvas won`t paint 2.0
I changed my canvas to a JPanel and changed the paint method to
paintComponent, but now my sprites won`t paint. The paintComponent
method is not even being called. Is there anything else wrong with my
code?

-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(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);
battle.setVisible(true);
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 JPanel implements KeyListener
{
AllyParty ap;

boolean showMenu = false;

EnemyParty ep;

Graphics screen;

Image allyPic[], enemyPic[];

int enemyCount;
int turn[];

Random rand;

public Battle (AllyParty allyparty)
{

/*Initial Canvas subclass setup*/
this.setSize(600, 600);
this.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();

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 paintComponenet (Graphics g)
{
super.paintComponent(g);
for (int i = 0; i < 4; i++)
{
System.out.println("Painting ally #" + i);
g.drawImage(allyPic[i], ap.ally[i].getXPos(),
ap.ally[i].getYPos(), this);
}

for (int i = 0; i < enemyCount; i++)
{
System.out.println("Painting enemy #" + i);
g.drawImage(enemyPic[i], ep.enemy[i].getXPos(),
ep.enemy[i].getYPos(), this);
}

System.out.println("Screen painted in paint method");

}




public void keyPressed (KeyEvent k) {}
public void keyReleased (KeyEvent k) {}
public void keyTyped (KeyEvent k) {}
}
Reply with quote
Send private message
View user's profile Post To page top
msulli1355 Posted: Sat Apr 24, 2004 4:54 am


Joined: 17 Apr 2004

Posts: 28
Canvas won`t paint 2.0
Please ignore my post earlier today. In it I said that paintComponent
wasn`t being called. That was because the method name was misspelled in
my code. I`ve corrected it and I tested if it could use
Graphics.drawString. That worked. I can`t get it to draw Images
though. Below is my code. Please help!

-Michael Sullivan-

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

class Battle extends JPanel implements KeyListener
{
AllyParty ap;

boolean showMenu = false;

EnemyParty ep;

Graphics screen;

Image allyPic[], enemyPic[];

int enemyCount;
int turn[];

Random rand;

public Battle (AllyParty allyparty)
{

/*Initial Canvas subclass setup*/
this.setSize(600, 600);
this.setPreferredSize(new Dimension(800, 600));
this.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();

repaint();
}

public void update(Graphics g)
{}


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 paintComponent (Graphics screen)
{
super.paintComponent(screen);
Graphics2D g = (Graphics2D) screen;

g.setColor(Color.white);
g.drawString("paintComponent", 0, 100);
for (int i = 0; i < 4; i++)
{
System.out.println("Painting ally #" + i);
g.drawImage(allyPic[i], ap.ally[i].getXPos(),
ap.ally[i].getYPos(), this);
}

for (int i = 0; i < enemyCount; i++)
{
System.out.println("Painting enemy #" + i);
g.drawImage(enemyPic[i], ep.enemy[i].getXPos(),
ep.enemy[i].getYPos(), this);
}

System.out.println("Screen painted in paint method");

}




public void keyPressed (KeyEvent k) {}
public void keyReleased (KeyEvent k) {}
public void keyTyped (KeyEvent k) {}
}
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

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
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help