File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes image in panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "image in panel" Watch "image in panel" New topic

image in panel

Michael Calder

Joined: Apr 14, 2004
Posts: 6
Hi i am trying to add an gif onto a panel in my applet and am using this to paint it.
public void paint(Graphics g) {
g.drawImage(myImage, 1, 1, this.panel1);
thi image doesn't appear on panel1 though, it instead appears underneath on the applet canvas and in the wrong place aswell. It appears in 1,1 on the applet canvas not 1,1 on the panel. How do i make it appear on panel1? Any help would be really good cos its annoying me now.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

You would have to extend Panel, override paint(), and use it for the class of your panel1 object.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Michael Calder

Joined: Apr 14, 2004
Posts: 6
Hi thanks for the reply, i have looked around messages for what you said to do, and have found some examples, but i have produced rubbish. Java is new to me and i am strugling. This is a coursework project, and i have produced a working version but it uses swing which they warned us against so i'm now trying with just AWT components. I added a bit in about extending the panel but really don't know what i'm doing. I've cut my code back to the basics so if someone could just show how to paint the gif on panel1 that would be a huge help. Thnks
package coursework;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.Image;
public class JamJars
extends Applet {
private boolean isStandalone = false;
Image myImage;
BorderLayout borderLayout1 = new BorderLayout();
Panel lower = new Panel();
Panel upper = new Panel();
CardLayout cardLayout1 = new CardLayout();
Panel game = new Panel();
GridLayout gridLayout1 = new GridLayout();
Panel panel1 = new Panel();
class paintPanel
extends Panel {
public void paintPanel() {
public void paint(Graphics g) {
g.drawImage(1, 1, myImage, panel1);
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
//Construct the applet
public JamJars() {
//Initialize the applet
public void init() {
try {
catch (Exception e) {
//Component initialization
private void jbInit() throws Exception {
myImage = getImage(getCodeBase(), "jar.gif");
//myGrap = getGraphics(myImage);
MediaTracker mt = new MediaTracker(this);
mt.addImage(myImage, 0);
try {
mt.waitForAll(); // blocks here until Image is fully downloaded
catch (InterruptedException e) {
this.add(lower, BorderLayout.CENTER);
this.add(upper, BorderLayout.NORTH);
lower.add(game, "game");
game.add(panel1, null);
//Start the applet
public void start() {
//Stop the applet
public void stop() {
//Destroy the applet
public void destroy() {
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
//Get parameter info
public String[][] getParameterInfo() {
return null;
Michael Calder

Joined: Apr 14, 2004
Posts: 6
I agree. Here's the link:
subject: image in panel
It's not a secret anymore!