aspose file tools
The moose likes Beginning Java and the fly likes Put an Image into a Grid Bag Layout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Put an Image into a Grid Bag Layout" Watch "Put an Image into a Grid Bag Layout" New topic

Put an Image into a Grid Bag Layout

Pete Bullock

Joined: Apr 25, 2001
Posts: 4
I'm new to java and I've been trying for nearly a week to figure out how to add() an image into Grid Bag Layout using only AWT (no swing). I want to use it in an applet using jdk1.0.2 (since it would seem that that's the only version fully supported by all browsers). I'm vague on the difference between a Canvas and a Panel and I'm not sure whether I need either, both or neither to display a .gif or .jpg into the GBL. Sometimes when I try to compile using javac, I get error messages with getCodeBase(). It seem that I can position getCodeBase() in some parts of my applet, but not in others. At any rate, I see lots of places that say "make a panel (or whatever) and poke it here (or there)", but I just need some real simple code. Oh, and I see a lot of examples that use mediatracker, but I'm not sure that mediatracker is supported in 1.0.2. I'm just looking for the bare necessary code. Could anyone give me some code to put an image into a grid bag layout, please?
David Freels
Ranch Hand

Joined: Feb 01, 2001
Posts: 102
Here is a very simple example, I leave it up to you to research some the classes:
java.awt.Panel panel = new java.awt.Panel();
java.awt.Graphics g = panel.getGraphics();
g.drawImage(Image, 0, 0, width, height, bgcolor, ImageObserver);
The to 0s represent the x and y coordinates to start with, the width and height tell java how large to paint the Image and the bgcolor is the background color. The ImageObserver can be the applet.
Sun Certified Programmer for the Java2 Platform
Pete Bullock

Joined: Apr 25, 2001
Posts: 4
Okay, I checked out the 1.0.2 documentation at sun and apparently Mediatracker existed at that time. However I'm still having trouble seeing my entire image. I tried to implement the code that you mentioned, David, but I was unable to get it to work. So I'm attaching a portion of the code that I'm working on. As you can see after compiling it and running it, if the .gif graphic is of any size at all then you only see a teeny bit of its height and you see the top left part of the graphic for the same width as the "Clear Entries" button takes up. Can anyone spruce this code up so that myimage.gif can be seen entirely, even with a larger graphic?
import java.awt.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Font;
public class SimpleArgh2 extends java.applet.Applet {
Image pointsimage;
void buildConstraints(GridBagConstraints gbc, int gx, int gy,
int gw, int gh, int wx, int wy, int px, int py) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
gbc.ipadx = px;
gbc.ipady = py;
gbc.insets = new java.awt.Insets(5, 10, 5, 10);
public void init() {
Font f = new Font("Arial",Font.BOLD,12);
MediaTracker mt = new MediaTracker (this);
pointsimage = getImage(getCodeBase(),"images/myimage.gif");
catch(InterruptedException ie){}
int iWidth = pointsimage.getWidth(this);
int iHeight = pointsimage.getHeight(this);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
// Clear Entries Button
buildConstraints(constraints, 1, 3, 1, 1, 0, 0, 0, 0);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.CENTER;
Button clrb = new Button("Clear Entries");
gridbag.setConstraints(clrb, constraints);
// Points Image
buildConstraints(constraints, 1, 4, 1, 3, 0, 0, 0, 0);
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.CENTER;
MyPanel mp = new MyPanel(pointsimage);
Graphics g = mp.getGraphics();
gridbag.setConstraints(mp, constraints);
class MyPanel extends java.awt.Panel {
Image pImg;
MyPanel(Image imgIn) {
pImg = imgIn;

public void paint(Graphics g) {
g.drawImage(pImg, 5, 5, this);
Don't get me started about those stupid light bulbs.
subject: Put an Image into a Grid Bag Layout