jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes Help with paint to panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help with paint to panel" Watch "Help with paint to panel" New topic

Help with paint to panel

Mike Calder

Joined: Mar 28, 2004
Posts: 1
Hi i need to paint a gif(myImage) onto a panel(panel1) in my applet. I want my program to use only AWT components no Swing. I can paint the gif no problem onto the applet canvas but just cant get it to paint onto panel1 or any other panel for that matter, it always just appears on the canvas even though i am referencing the panel. A reply from an earlier post was "You would have to extend Panel, override paint(), and use it for the class of your panel1 object." which i have tried without success. Can anybody show me how to do this? If somebody could edit my code below with the solution or point me in the direction of a post that has a simple example of this it would be very much apreciated.
Thanks Mike
package coursework;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.Image;
public class Applet1
extends Applet {
Image myImage;
private boolean isStandalone = false;
BorderLayout borderLayout1 = new BorderLayout();
Panel Lower = new Panel();
Panel Upper = new Panel();
CardLayout cardLayout1 = new CardLayout();
Panel Help = new Panel();
GridLayout gridLayout1 = new GridLayout();
Panel panel1 = new Panel();
Panel Game = new Panel();
//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 Applet1() {
//Initialize the applet
public void init() {
try {
catch (Exception e) {
//Component initialization
private void jbInit() throws Exception {
this.add(Lower, BorderLayout.CENTER);
Lower.add(Help, "panel1");
Lower.add(Game, "Game");
Game.add(panel1, null);
this.add(Upper, BorderLayout.NORTH);
cardLayout1.show(Lower, "Game");
myImage = getImage(getCodeBase(), "jar.gif");
MediaTracker mt = new MediaTracker(this.Lower);
mt.addImage(myImage, 0);
try {
mt.waitForAll(); // blocks here until Image is fully downloaded
catch (InterruptedException e) {
public void paint(Graphics g) {
g.drawImage(myImage,1, 1,this.Lower);
//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
Anybody? Is it even possible?
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

All you need to do is use a component like the following to display your image in your applet -

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
subject: Help with paint to panel
It's not a secret anymore!