Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

image in panel

 
Michael Calder
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to extend Panel, override paint(), and use it for the class of your panel1 object.
 
Michael Calder
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
myImage = getImage(getCodeBase(), "jar.gif");
//myGrap = getGraphics(myImage);
this.setVisible(true);
this.setLayout(borderLayout1);
MediaTracker mt = new MediaTracker(this);
mt.addImage(myImage, 0);
try {
mt.waitForAll(); // blocks here until Image is fully downloaded
}
catch (InterruptedException e) {
}
lower.setLayout(cardLayout1);
game.setLayout(gridLayout1);
game.setBackground(Color.white);
panel1.setBackground(Color.white);
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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anybody?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic