I'm new in programming with java swing and we were just assigned to program an applet with only the swing class. I'm not sure what's the best way to program this so I started with creating a JPanel that holds the JButtons. I tested it by adding it on a JFrame and it showed, but when I placed it on a JApplet, I can't see it! What am I doing wrong? Could somebody help me, please?
This is my program (it's quite lengthy). I'm not sure if this is the right way to do it or if there's a better way. I got some parts of it by imitating other programs. It's still not finished but right now, I'm stumped. Any tips would help.
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
class Index extends JPanel
{
Image image;
BufferedImage background;
MyButton intro, precol, american;
Vector introImages, precolImages, americanImages;
ActionListener myActionListener;
Index(Image img, ActionListener actionListener)
{
image = img;
myActionListener = actionListener;
loadImage();
createBufferedImage();
setSize(image.getWidth(this), image.getHeight(this));
layButtons();
}
public void loadImage()
{
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try { mt.waitForID(0); }
catch (InterruptedException ie) { return; }
if (mt.isErrorID(0)) return;
introImages = initImages("intro");
precolImages = initImages("colonial");
americanImages = initImages("american");
}
public void createBufferedImage()
{
background = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = background.createGraphics();
g2.drawImage(image, 0, 0, this);
g2.dispose();
}
public Vector initImages(String name)
{
Vector temp = new Vector();
for(int i = 1; i <= 3; i++)
{
temp.addElement(new ImageIcon
(this.getClass().getResource("images/" + name + i + ".gif")));
}
return temp;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
int x = (width - imageWidth)/2;
int y = (height - imageHeight)/2;
g.drawImage(background, x, y, this);
}
public Dimension getPreferredSize()
{
return new Dimension(image.getWidth(this), image.getHeight(this));
}
public void layButtons()
{
setLayout(null);
int x = 95, y = 175;
int w = 145, h = 25;
int gap = 35;
// initialize buttons
intro = new MyButton(introImages);
precol = new MyButton(precolImages);
american = new MyButton(americanImages);
// set actionCommands on buttons
intro.setActionCommand("introduction");
precol.setActionCommand("pre-colonial");
american.setActionCommand("american");
// add buttons to panel
add(intro);
add(precol);
add(american);
// arrange buttons on panel
intro.reshape(x, y, w, h);
precol.reshape(x, y+gap, w, h);
american.reshape(x, y+2*gap, w, h);
// register listener to buttons
intro.addActionListener(myActionListener);
precol.addActionListener(myActionListener);
american.addActionListener(myActionListener);
}
}
class MyButton extends JButton
{
MyButton(Vector images)
{
super((ImageIcon) images.elementAt(0));
setRolloverIcon((ImageIcon) images.elementAt(1));
setPressedIcon((ImageIcon) images.elementAt(2));
setSelectedIcon((ImageIcon) images.elementAt(2));
setFocusPainted(false);
setBorderPainted(false);
setContentAreaFilled(false);
setSize(150,30);
}
}
I tested it with the following code, and it turned out all right (at least, it does what I expected it to do)...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Test extends JApplet
{
public void init()
{
Index index = new Index(getImage(getDocumentBase(), "images/index.gif"),new ActionListener() { public void actionPerformed(ActionEvent e){}});
getContentPane().add(index);
}
}
...but when I used my Index class in this JApplet, I can't see my Index.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class History extends JApplet
{
Index index;
Image paper;
Image title;
public void init()
{
paper = getImage(getDocumentBase(), "images/AreaT.gif");
title = getImage(getDocumentBase(), "images/title.gif");
index = new Index(getImage(getDocumentBase(), "images/index.gif"),
new SysActionListener());
createGUI();
}
public void paint(Graphics g)
{
// this is just to put the images on the background of the applet
Dimension d = getSize();
setBackground(Color.black);
g.drawImage(title, 20, 10, d.width-40, 100, this);
g.drawImage(paper, 360, 150, this);
}
private void createGUI()
{
Container pane = getContentPane();
pane.setLayout(null);
Dimension d = index.getPreferredSize();
index.setBounds(20, 150, d.width, d.height);
}
class SysActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{/* */}
}
Comments
[code]
public void init()
{
Container contentPane = getContentPane();
contentPane.add(yourComponent);
}
[/code]