I am currently in the process of learning Java. One of my projects for school requires me to build onto a .class we have been working on called CyberPet.class. The interface for this class is called CyberPetApplet.class.
the code for the applet is as follows
________________________________________
/*
* CyberPetApplet.java
*
* Created on September 30, 2003, 10:47 AM
*/
package School;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author t0936062
*/
public class CyberPetApplet extends java.applet.Applet implements ActionListener
{
private CyberPet pet1;
private Label namelabel;
private TextField statefield;
private Button eatbutton, sleepbutton, dreambutton;
private Image eatImage, sleepImage, dreamImage;
private String petState;
/** Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init()
{
pet1 = new CyberPet("Bob", "Spider"
;
namelabel = new Label("Hi, my name is " + pet1.getName() +
" and i am currently "
;
statefield = new TextField(12);
eatbutton = new Button("Eat!"
;
eatbutton.addActionListener(this);
sleepbutton = new Button("Sleep!"
;
sleepbutton.addActionListener(this);
dreambutton = new Button("Dream!"
;
dreambutton.addActionListener(this);
eatImage = getImage(getCodeBase(),"eatImage.gif"
;
sleepImage = getImage(getCodeBase(),"sleepImage.gif"
;
dreamImage = getImage(getCodeBase(),"dreamImage.gif"
;
petState = pet1.getState();
statefield.setText(pet1.getState());
statefield.setEditable(false);
add(namelabel);
add(statefield);
add(eatbutton);
add(sleepbutton);
add(dreambutton);
setSize(300, 200);
}
public void paint(Graphics g)
{
if (petState.equals("eating"
)
g.drawImage(eatImage, 20, 100, this);
else if (petState.equals("sleeping"
)
g.drawImage(sleepImage, 20, 100, this);
else if (petState.equals("dreaming"
)
g.drawImage(dreamImage, 20, 100, this);
}
public void actionPerformed(java.awt.event.ActionEvent e)
{
if (e.getSource() == eatbutton)
{
pet1.eat();
}
else if (e.getSource() == sleepbutton)
{
pet1.sleep();
}
else if (e.getSource() == dreambutton)
{
pet1.dream();
}
statefield.setText(pet1.getState());
repaint();
}
}
_______________________________________________
my problem is i can not get the Graphics eatImage, sleepImage, and happyImage to load into the applet and display. I have the .gif images loaded into the same directory as the applet class file. Can anyone tell me what i am doing wrong in my code?
Thanks
GTLoco
the code for the applet is as follows
________________________________________
/*
* CyberPetApplet.java
*
* Created on September 30, 2003, 10:47 AM
*/
package School;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author t0936062
*/
public class CyberPetApplet extends java.applet.Applet implements ActionListener
{
private CyberPet pet1;
private Label namelabel;
private TextField statefield;
private Button eatbutton, sleepbutton, dreambutton;
private Image eatImage, sleepImage, dreamImage;
private String petState;
/** Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init()
{
pet1 = new CyberPet("Bob", "Spider"
namelabel = new Label("Hi, my name is " + pet1.getName() +
" and i am currently "
statefield = new TextField(12);
eatbutton = new Button("Eat!"
eatbutton.addActionListener(this);
sleepbutton = new Button("Sleep!"
sleepbutton.addActionListener(this);
dreambutton = new Button("Dream!"
dreambutton.addActionListener(this);
eatImage = getImage(getCodeBase(),"eatImage.gif"
sleepImage = getImage(getCodeBase(),"sleepImage.gif"
dreamImage = getImage(getCodeBase(),"dreamImage.gif"
petState = pet1.getState();
statefield.setText(pet1.getState());
statefield.setEditable(false);
add(namelabel);
add(statefield);
add(eatbutton);
add(sleepbutton);
add(dreambutton);
setSize(300, 200);
}
public void paint(Graphics g)
{
if (petState.equals("eating"
g.drawImage(eatImage, 20, 100, this);
else if (petState.equals("sleeping"
g.drawImage(sleepImage, 20, 100, this);
else if (petState.equals("dreaming"
g.drawImage(dreamImage, 20, 100, this);
}
public void actionPerformed(java.awt.event.ActionEvent e)
{
if (e.getSource() == eatbutton)
{
pet1.eat();
}
else if (e.getSource() == sleepbutton)
{
pet1.sleep();
}
else if (e.getSource() == dreambutton)
{
pet1.dream();
}
statefield.setText(pet1.getState());
repaint();
}
}
_______________________________________________
my problem is i can not get the Graphics eatImage, sleepImage, and happyImage to load into the applet and display. I have the .gif images loaded into the same directory as the applet class file. Can anyone tell me what i am doing wrong in my code?
Thanks
GTLoco