Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with some code

Status
Not open for further replies.

GTLoco99

Programmer
Sep 2, 2003
59
US
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
 
You forget to give the CyberPet.java
I can run your code(Remember to upload the gif and CyberPetApplet.class and your html to a server)
call to the htm on the server
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 int tempPet;
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");
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 (tempPet == 1)
g.drawImage(eatImage, 20, 100, this);
else
g.drawImage(sleepImage, 20, 100, this);
/*
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)
{
tempPet = 1;
// pet1.eat();
}
else if (e.getSource() == sleepbutton)
{
tempPet = 2;
// pet1.sleep();
}
else if (e.getSource() == dreambutton)
{
tempPet = 3;
// pet1.dream();
}

// statefield.setText(pet1.getState());
repaint();

}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top