This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Applets and the fly likes placing images in applets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "placing images in applets" Watch "placing images in applets" New topic
Author

placing images in applets

David Record
Greenhorn

Joined: Jun 14, 2001
Posts: 7
how do I place an image in an applet like a jpeg or gif, and have the applet place the image where I click the mouse?
thanks guys
Frank Ying
Greenhorn

Joined: Jul 02, 2001
Posts: 5
You can use my ImageLoader program:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
public class ImageLoader extends Canvas {
Image image;
int w;
int h;
public ImageLoader(){}
public ImageLoader(URL i) {
load(i);
}
public void load(URL url) {
MediaTracker tracker;
try {
image = getToolkit().getImage(url);
tracker = new MediaTracker(this);
tracker.addImage(image,0);
tracker.waitForID(0);
}catch (InterruptedException e) {
e.printStackTrace();
}
w=image.getWidth(this);
h=image.getHeight(this);
setSize(w,h);
}
public void paint(Graphics g) {
g.drawImage(image,0,0,this);
}
public int getWidth() { return w; }
public int getHeight() { return h; }
}
In your calling applet, put the following code:
ImageButton btnEntry = new ImageButton
(new URL(getCodeBase(), "off_entry.gif"),
new URL(getCodeBase(), "on_entry.gif"));
add(btnEntry);
btnEntry.setBounds(0,0,145,21);
Hope that works on your program.
Frank
David Record
Greenhorn

Joined: Jun 14, 2001
Posts: 7
hey thanks I'll give it a shot and get back to you.
David Record
Greenhorn

Joined: Jun 14, 2001
Posts: 7
THis is what I have so far, and I'm getting an instantiation error.
//<applet code="AppletApp.class" width=200 height=300></applet>
import java.awt.*; //<-- added code
import java.applet.*; //<-- added code
import java.awt.event.*; //<-- added code
abstract class AppletApp extends Applet implements MouseListener{

private Image duke;
private int xPos,yPos;

public void init() {

duke = getImage(getDocumentBase(),"duke.gif");

addMouseListener( this );

}
public void paint(Graphics g) {

setBackground(Color.white);
g.drawImage(duke, xPos, yPos, this);
}

public void mousePressed(MouseEvent e) {
xPos = e.getX();
yPos = e.getY();
repaint();
}

}
 
Don't get me started about those stupid light bulbs.
 
subject: placing images in applets