• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Displaying image in applet
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Displaying image in applet


  • Subject: Displaying image in applet
  • From: Richard Rehl <email@hidden>
  • Date: Thu, 8 Mar 2007 11:41:08 -0500

I'm trying to both learn Java and XCode at the same time, so bear with me.....

I've created a Java AWT Applet project and am attempting to display an image in the applet. I've declared the variable:

Image logo;

and in the init() method have assigned an image to the variable:

logo = getImage(getDocumentBase(), "ski.jpg");

and in the paint() method have instructed the applet to display the image:

g.drawImage(logo,125,160,this);

and finally, I've added the image to the project from within XCode by using Project > Add to Project, choosing the image ski.jpg and checking the box to "Copy items into destination group's folder (if needed)", and have verified that the image has been added to the root of the Project folder.

The code compiles and displays in the Applet Viewer, but the image doesn't display.

Any ideas? Here's the complete code:

//
//  BodyMassAWTApplet.java
//  BodyMassAWTApplet
//
//  Created by me on 3/6/07.
//  Copyright (c) 2007 __MyCompanyName__. All rights reserved.
//  A simple Java applet
//

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class BodyMassAWTApplet extends Applet implements ActionListener
{

    Image logo;
    int inches, pounds;
    double meters, kilograms, index;

//construct components
Label companyLabel = new Label("THE SUN FITNESS CENTER BODY MASS INDEX CALCULATOR");
Label heightLabel = new Label("Enter your height to the nearest inch: ");
TextField heightField = new TextField(10);
Label weightLabel = new Label("Enter your weight to the nearest pound: ");
TextField weightField = new TextField(10);
Button calcButton = new Button("Calculate");
Label outputLabel = new Label("Click the Calculate button to see your Body Mass Index.");


public void init()
{
setForeground(Color.red);
add(companyLabel);
add(heightLabel);
add(heightField);
add(weightLabel);
add(weightField);
add(calcButton);
calcButton.addActionListener(this);
add(outputLabel);
logo = getImage(getDocumentBase(), "ski.jpg");
}

public void actionPerformed(ActionEvent e)
{
//
inches = Integer.parseInt(heightField.getText());
pounds = Integer.parseInt(weightField.getText());
meters = inches / 39.36;
kilograms = pounds / 2.2;
index = kilograms / Math.pow(meters, 2);
outputLabel.setText("YOUR BODY MASS INDEX IS " + Math.round (index) + ".");
}


    public void paint (Graphics g)
    {
        g.drawImage(logo, 125,160,this);
    }
}

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Displaying image in applet
      • From: Richard Rehl <email@hidden>
  • Prev by Date: Re: Looking for info on data manipulation on files
  • Next by Date: Re: Displaying image in applet
  • Previous by thread: Re: Looking for info on data manipulation on files
  • Next by thread: Re: Displaying image in applet
  • Index(es):
    • Date
    • Thread