freelanceprogrammers.org Forum Index » Java

Canvas won`t paint 2.1


View user's profile Post To page top
msulli1355 Posted: Sat Apr 24, 2004 10:42 am


Joined: 17 Apr 2004

Posts: 28
Canvas won`t paint 2.1
I found a JPanel image example on the java.sun.com website and
downloaded it and compiled it. It ran just fine. I began modifying it
into the Battle class for my game, but I`ve run into a problem that
makes no sense to me. I changed the path to the examples original
picture to point to an image on my hard drive. It still worked fine.
Then I added a line to the paintComponent method that would paint a
particular sprite returned by my AllyParty object. The sprite the
object returns is the exact same copy of the sprite that worked with the
absolute path, but the sprite retrieved by the object didn`t paint.
Before I trashed my previous Battle class the AllyParty modules were
working fine. The code hasn`t changed since then. I`ve pasted the code
for the four affected classes below. I hope someone can help me. This
is becoming very aggravating...

-Michael Sullivan-

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

class Battle extends JPanel {
Image image;
Toolkit kit;
AllyParty ap;

public Battle(AllyParty party)
{
this.setBackground(Color.black);
kit = Toolkit.getDefaultToolkit();
this.image =
kit.getImage("/home/michael/myFantasy/sprites/ally/ffight.gif");
ap = party;
party.setPos(0);
for (int i = 0; i < 4; i++)
{
System.out.println("Ally #" + i + "`s XPos is " +
ap.ally[i].getXPos() );
System.out.println("Ally #" + i + "`s YPos is " +
ap.ally[i].getYPos() );
}
}

public void paintComponent(Graphics g) {
super.paintComponent(g); //paint background

//Draw image at its natural size first.
g.drawImage(image, 0, 0, this);
g.drawImage(ap.ally[0].getStandPic(), 450, 0, this); //This line
doesn`t work!

}
}

import java.awt.*;
import java.applet.*;
import javax.swing.*;

public class AllyParty extends Party
{
public Ally ally[];

public int active;

public AllyParty(Ally c[])
{
ally = new Ally[4];
for (int i = 0; i < 4; i++)
ally[i] = c[i];
}

public AllyParty() {}

public void update()
{
for (int i = 0; i < 4; i++)
{
if (ally[i].getCurrentHP() < ally[i].getMaxHP() / 3)
ally[i].isHurt(true);

if (ally[i].getCurrentHP() <= 0)
ally[i].isDead(true);
}
}

public int activeSprite() { return active; }
public void activeSprite(int a) { active = a; }

public void setPos(int state)
{
switch (state)
{
case 0: //Regular battle
for (int i = 0; i < 4; i++)
ally[i].setXPos(450);
break;
case 1: //Back attack
for (int i = 0; i < 4; i++)
ally[i].setXPos(0);
break;
case 2: //Attacked from both sides
for (int i = 0; i < 4; i++)
ally[i].setXPos(250);
break;
case 3: //Pincer attack
ally[0].setXPos(0);
ally[1].setXPos(0);
ally[2].setXPos(450);
ally[3].setXPos(450);
break;
}
for (int i = 0; i < 4; i++)
ally[i].setYPos(80 * i);
if (state == 3)
{
ally[1].setYPos(0);
ally[2].setYPos(320);
}
}
}



import java.awt.*;

class Ally extends Character
{

//Main Character variables
private String name;

//Status variables
private int STR;
private int AGL;
private int INT;
private int VIT;
private int LUCK;
private int DAMAGE;
private int HITRATE;
private int ABSORB;
private int EVADERATE;

//Abilities
//Magic[] magic;
private int spellCount;

//Miscellaneous variables
private boolean dead = false;
private boolean hurt = false;
private boolean defend = false;
private int targetPos = 0;
private int cursorpos;
private Class type;
private Image standPic, walkPic, raisePic, strikePic, hurtPic, deadPic;
private Image runPic, stopPic;



public Ally(int kind, String id)
{
type = new Class(kind);
classNum = kind;
STR = type.getSTR();
AGL = type.getAGL();
INT = type.getINT();
VIT = type.getVIT();
LUCK = type.getLUCK();
maxHP = type.getMaxHP();
maxMP = type.getMaxMP();
abilities = type.getAbilities();
abilityCount = type.getAbilityCount();
HITRATE = type.getHITRATE();
EVADERATE = type.getEVADERATE();
name = id;
DAMAGE = (int) STR / 2;
if (STR == 1)
DAMAGE = 1;

ABSORB = 0;
//magic = new Magic[spellCount]
spellCount = 0;
xPos = 450;
currentHP = maxHP;
currentMP = maxMP;
standPic = type.getStandPic(kind);
walkPic = type.getWalkPic(kind);
raisePic = type.getRaisePic(kind);
strikePic = type.getStrikePic(kind);
hurtPic = type.getHurtPic(kind);
deadPic = type.getDeadPic(kind);
runPic = type.getRunPic(kind);
stopPic = type.getStopPic(kind);
}
public String getName() { return name; }
public void setName(String id) { name = id; }

public int getSTR() { return STR; }
public void setSTR(int value) { STR = value; }
public void giveSTR(int value) { STR += value; }
public void takeSTR(int value) { STR -= value; }

public int getAGL() { return AGL; }
public void setAGL(int value) { AGL = value; }
public void giveAGL(int value) { AGL += value; }
public void takeAGL(int value) { AGL -= value; }

public int getINT() { return INT; }
public void setINT(int value) { INT = value; }
public void giveINT(int value) { INT += value; }
public void takeINT(int value) { INT -= value; }

public int getVIT() { return VIT; }
public void setVIT(int value) { VIT = value; }
public void giveVIT(int value) { VIT += value; }
public void takeVIT(int value) { VIT -= value; }

public int getLUCK() { return LUCK; }
public void setLUCK(int value) { LUCK = value; }
public void giveLUCK(int value) { LUCK += value; }
public void takeLUCK(int value) { LUCK -= value; }

public int getHITRATE() { return HITRATE; }
public void setHITRATE(int value) { HITRATE = value; }
public void giveHITRATE(int value) { HITRATE += value; }
public void takeHITRATE(int value) { HITRATE -= value; }

public int getEVADERATE() { return EVADERATE; }
public void setEVADERATE(int value) { EVADERATE = value; }
public void giveEVADERATE(int value) { EVADERATE += value; }
public void takeEVADERATE(int value) { EVADERATE -= value; }

public int getDamage() { return DAMAGE; }

public int getSpellCount() { return spellCount; }

//public void equipWeapon(Weapon w)
//public void equipArmor(Armor a)
//public void equipAccessory(Accessory a)
//public void giveMagic(Magic m)

public int getTargetPos() { return targetPos; }
public void setTargetPos(int p) { targetPos = p; }
public Image getStandPic() { return standPic; }
public Image getWalkPic() { return walkPic; }
public Image getRaisePic() { return raisePic; }
public Image getStrikePic() { return strikePic; }
public Image getHurtPic() { return hurtPic; }
public Image getDeadPic() { return deadPic; }
public Image getRunPic() { return runPic; }
public Image getStopPic() { return stopPic; }

public boolean getDefend() { return defend; }
public void setDefend(boolean d) { defend = d; }

public boolean isHurt() { return hurt; }
public void isHurt(boolean h) { hurt = h; }

public int cursorPos() { return cursorpos; }
public void cursorPos(int p) { cursorpos = p; }
}



import java.awt.*;
import java.net.*;
import java.applet.*;

public class Class
{
private static final String loc =
"/home/michael/myFantasy/sprites/ally";

//Classes
// Lower classes
public static final int FIGHTER = 0;
public static final int THIEF = 1;
public static final int BLACKBELT = 2;
public static final int REDMAGE = 3;
public static final int WHITEMAGE = 4;
public static final int BLACKMAGE = 5;
// Upper classes
public static final int KNIGHT = 6;
public static final int NINJA = 7;
public static final int GRANDMASTER = 8;
public static final int REDWIZARD = 9;
public static final int WHITEWIZARD = 10;
public static final int BLACKWIZARD = 11;

private int STR;
private int AGL;
private int INT;
private int VIT;
private int LUCK;
private int HITRATE;
private int EVADERATE;
private int maxHP;
private int maxMP;
private String abilities[];
private int abilityCount;

static Image standPic, walkPic, raisePic, strikePic, hurtPic, deadPic;
static Image runPic, stopPic;
Toolkit kit;

Class (int c)
{
kit = Toolkit.getDefaultToolkit();
switch(c)
{
case FIGHTER:
STR = 20;
AGL = 5;
INT = 1;
VIT = 10;
LUCK = 5;
maxHP = 35;
maxMP = 0;
HITRATE = 10;
EVADERATE = 53;
standPic = kit.getImage(loc + "ffight.gif");
walkPic = kit.getImage(loc + "fwalk.gif");
raisePic = kit.getImage(loc + "fweapon2.gif");
strikePic = kit.getImage(loc + "fweapon.gif");
hurtPic = kit.getImage(loc + "fhurt.gif");
deadPic = kit.getImage(loc + "fdead.gif");
runPic = kit.getImage(loc + "frun.gif");
stopPic = kit.getImage(loc + "frun2.gif");
abilities = new String[2];
abilities[0] = "ATTACK";
abilities[1] = "ITEM";
abilityCount = 2;
break;
case THIEF:
STR = 5;
AGL = 10;
INT = 5;
VIT = 5;
LUCK = 15;
maxHP = 30;
maxMP = 0;
HITRATE = 5;
EVADERATE = 58;
standPic = kit.getImage(loc + "tfight.gif");
walkPic = kit.getImage(loc + "twalk.gif");
raisePic = kit.getImage(loc + "tweapon2.gif");
strikePic = kit.getImage(loc + "tweapon.gif");
hurtPic = kit.getImage(loc + "thurt.gif");
deadPic = kit.getImage(loc + "tdead.gif");
runPic = kit.getImage(loc + "trun.gif");
stopPic = kit.getImage(loc + "trun2.gif");
abilities = new String[3];
abilities[0] = "ATTACK";
abilities[1] = "SKILL";
abilities[2] = "ITEM";
abilityCount = 3;
break;
case BLACKBELT:
STR = 5;
AGL = 5;
INT = 5;
VIT = 20;
LUCK = 5;
maxHP = 33;
maxMP = 0;
HITRATE = 5;
EVADERATE = 53;
standPic = kit.getImage(loc + "bbfight.gif");
walkPic = kit.getImage(loc + "bbwalk.gif");
raisePic = kit.getImage(loc + "bbweapon2.gif");
strikePic = kit.getImage(loc + "bbweapon.gif");
hurtPic = kit.getImage(loc + "bbhurt.gif");
deadPic = kit.getImage(loc + "bbdead.gif");
abilities = new String[3];
runPic = kit.getImage(loc + "bbrun.gif");
stopPic = kit.getImage(loc + "bbrun2.gif");
abilities[0] = "ATTACK";
abilities[1] = "ITEM";
abilityCount = 2;
break;
case REDMAGE:
STR = 10;
AGL = 10;
INT = 10;
VIT = 5;
LUCK = 5;
maxHP = 30;
maxMP = 4;
HITRATE = 7;
EVADERATE = 58;
standPic = kit.getImage(loc + "rmfight.gif");
walkPic = kit.getImage(loc + "rmwalk.gif");
raisePic = kit.getImage(loc + "rmweapon2.gif");
strikePic = kit.getImage(loc + "rmweapon.gif");
hurtPic = kit.getImage(loc + "rmhurt.gif");
deadPic = kit.getImage(loc + "rmdead.gif");
runPic = kit.getImage(loc + "rmrun.gif");
stopPic = kit.getImage(loc + "rmrun2.gif");
abilities = new String[3];
abilities[0] = "ATTACK";
abilities[1] = "MAGIC";
abilities[2] = "ITEM";
abilityCount = 3;
break;
case WHITEMAGE:
STR = 5;
AGL = 5;
INT = 15;
VIT = 10;
LUCK = 5;
maxHP = 28;
maxMP = 4;
HITRATE = 5;
EVADERATE = 53;
standPic = kit.getImage(loc + "wmfight.gif");
walkPic = kit.getImage(loc + "wmwalk.gif");
raisePic = kit.getImage(loc + "wmweapon2.gif");
strikePic = kit.getImage(loc + "wmweapon.gif");
hurtPic = kit.getImage(loc + "wmhurt.gif");
deadPic = kit.getImage(loc + "wmdead.gif");
runPic = kit.getImage(loc + "wmrun.gif");
stopPic = kit.getImage(loc + "wmrun2.gif");
abilities = new String[3];
abilities[0] = "ATTACK";
abilities[1] = "MAGIC";
abilities[2] = "ITEM";
abilityCount = 3;
break;
case BLACKMAGE:
STR = 1;
AGL = 10;
INT = 20;
VIT = 1;
LUCK = 10;
maxHP = 25;
maxMP = 4;
HITRATE = 5;
EVADERATE = 58;
standPic = kit.getImage(loc + "bmfight.gif");
walkPic = kit.getImage(loc + "bmwalk.gif");
raisePic = kit.getImage(loc + "bmweapon2.gif");
strikePic = kit.getImage(loc + "bmweapon.gif");
hurtPic = kit.getImage(loc + "bmhurt.gif");
deadPic = kit.getImage(loc + "bmdead.gif");
runPic = kit.getImage(loc + "bmrun.gif");
stopPic = kit.getImage(loc + "bmrun2.gif");
abilities = new String[3];
abilities[0] = "ATTACK";
abilities[1] = "MAGIC";
abilities[2] = "ITEM";
abilityCount = 3;
break;
}

}
public int getSTR() { return STR; }
public int getAGL() { return AGL; }
public int getINT() { return INT; }
public int getVIT() { return VIT; }
public int getLUCK() { return LUCK; }
public int getMaxHP() { return maxHP; };
public int getMaxMP() { return maxMP; };
public int getHITRATE() { return HITRATE; }
public int getEVADERATE() { return EVADERATE; }
public static Image getStandPic(int c)
{
Class dummy = new Class(c);
return dummy.standPic;
}
public static Image getWalkPic(int c)
{
Class dummy = new Class(c);
return dummy.walkPic;
}
public static Image getRaisePic(int c)
{
Class dummy = new Class(c);
return dummy.raisePic;
}
public static Image getStrikePic(int c)
{
Class dummy = new Class(c);
return dummy.strikePic;
}

public static Image getHurtPic(int c)
{
Class dummy = new Class(c);
return dummy.hurtPic;
}

public static Image getDeadPic(int c)
{
Class dummy = new Class(c);
return dummy.deadPic;
}

public static Image getRunPic(int c)
{
Class dummy = new Class(c);
return dummy.runPic;
}

public static Image getStopPic(int c)
{
Class dummy = new Class(c);
return dummy.stopPic;
}

public String[] getAbilities() { return abilities; }
public int getAbilityCount() { return abilityCount; }



}
Reply with quote
Send private message
View user's profile Post To page top
nitrusrit Posted: Sat Apr 24, 2004 9:00 pm


Joined: 10 Apr 2004

Posts: 19
Canvas won`t paint 2.1
I do my image loading from different places. For instance, when I get an image
I do the following:

grassTile = ( new ImageIcon( "grass_tile.gif" ) ).getImage();

And then I draw the image using:

gd.drawImage( grassTile, xpos, ypos, null );

where gd is the Graphics object from the BufferedImage I use as a double buffer.

Try that out and see if it works for you. I actually have a little program I
wrote and played with that I could send you if you`re interested. Don`t expect
it to look nice though. :)

Dustin

Michael Sullivan <michael@...> wrote:
I found a JPanel image example on the java.sun.com website and
downloaded it and compiled it. It ran just fine. I began modifying it
into the Battle class for my game, but I`ve run into a problem that
makes no sense to me. I changed the path to the examples original
picture to point to an image on my hard drive. It still worked fine.
Then I added a line to the paintComponent method that would paint a
particular sprite returned by my AllyParty object. The sprite the
object returns is the exact same copy of the sprite that worked with the
absolute path, but the sprite retrieved by the object didn`t paint.
Before I trashed my previous Battle class the AllyParty modules were
working fine. The code hasn`t changed since then. I`ve pasted the code
for the four affected classes below. I hope someone can help me. This
is becoming very aggravating...

-Michael Sullivan-


---------------------------------
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢

[Non-text portions of this message have been removed]
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