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()
          width = image.getIconWidth();

  public void paint(Graphics g)
  g.drawString("Luxurious", x, y);
  g.drawImage(image.getImage(), x, y + 20, width, heigth, this);
  public  void stop()

  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";
            message =  "Event cost is $" + (pr * guests) + ".00";     



Recommended Answers

All 4 Replies

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.


Am I addig this to the code or replacing something?

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).

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.