954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Add image to applet

Ok, I did search on this site and found nothing that really helps with my issue. I am doing assignment - JPartyPlanner - I have the applet complete, it compiles and shows in IE great, BUT when I add the image code it still compiles but I get nothing showing at all so I have done something wrong in the image area. Here is my code - your help is greatly appreciated.

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

public class JPartyPlanner extends JApplet implements ActionListener
{
JLabel label1 = new JLabel("Event Handlers Incorporated");
JLabel label2 = new JLabel(" PartyPlanner ");
Font font = new Font("Ariel", Font.ITALIC, 36);
JLabel guestsLabel = new JLabel("How many guests will be attending?");
JTextField guestsField = new JTextField("",9);
JTextField int2 = new JTextField("",9);
JButton button1 = new JButton("Calculate per guest");
JButton button2 =new JButton("Calculate total cost");
FlowLayout flow = new FlowLayout();
Container con = getContentPane();
JLabel answer = new JLabel("");
int[] range = {24, 49, 99, 199, 499, 999};
int[] price = {27, 25, 22, 19, 17, 14, 11};

private ImageIcon image = new ImageIcon("birthday.jpg");
private int width , heigth;
Container picture = getContentPane();
int x = 30;
int y = 30;

public void init()
{
con.setLayout(flow);
label1.setFont(font);
label2.setFont(font);
answer.setFont(font);
width = image.getIconWidth();


con.add(label1);
con.add(label2);
con.add(guestsLabel);
con.add(guestsField);
con.add(button1);
con.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
button1.requestFocus();
con.add(answer);
}
public void paint(Graphics g)
{
super.paint(g);
g.drawString("Luxurious", x, y);
g.drawImage(image.getImage(), x, y + 20, width, heigth, this);
}
public void stop()
{
guestsField.setText("");
}

public void actionPerformed(ActionEvent e)
{
String guestsString = guestsField.getText();
int guests = Integer.parseInt(guestsString);
int x, pr, sub = range.length;
String message;
for(x =0; x < range.length; ++x)
if(guests <= range[x])
{
sub = x;
x = range.length;
}
pr = price[sub];
Object o = e.getSource();
if(o == button1)
message = "Per person cost is $" + pr +".00";
else
message = "Event cost is $" + (pr * guests) + ".00";
answer.setText(message);

}

}

kdgeiger
Newbie Poster
11 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

with applets i think i usuall use getCodeBase or getDocumentBase to fetch images.

for example

Image myImage = getImage(getDocumentBase(), "myfile.jpg");


My understanding of this is that applets are run on web, like www.mydomain.com/appletfolder and i think this tells it to start looking on the web along that file path starting from the link that is where the applet is such as above URL.

Mike

adams161
Posting Whiz in Training
281 posts since May 2008
Reputation Points: 31
Solved Threads: 27
 

Am I addig this to the code or replacing something?

kdgeiger
Newbie Poster
11 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

I would replace:

private ImageIcon image = new ImageIcon("birthday.jpg");


with that line, and instead of g.drawImage(image.getImage() just use the actual image you populated with that line there, i.e. you wont need getImage, as you have an Image variable now.

Note in my example i changed the name from image to myImage. Also i don't have a lot of experience with paint, i use paintComponents, but i googled and it seemed both can be used. If any further trouble verify that paint is called, i.e. try making anything happen there, a background color or something ( if any other trouble).

adams161
Posting Whiz in Training
281 posts since May 2008
Reputation Points: 31
Solved Threads: 27
 

Got it - It is now working - Thank You sooooo much - Have a GREAT DAY!!!!

kdgeiger
Newbie Poster
11 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: