Original Post
Who would have thought a cool background would ever be so hard? Basically, I the paint method, I can't get the background and my components to paint. I need a way to get super to paint on my existing canvas, or drawImage to not overwrite my existing components. Anyone know what I should do?
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
public class IceCreamShop extends JFrame{
BufferedImage background;
public JLabel catagories[];
public JTextField fields[];
public final String catagoryNames[] = new String[] { "First Name", "Last Name", "Street Address", "City", "State", "Zip Code" };
public void RandomBackground() {
background = new BufferedImage(800,600, BufferedImage.TYPE_3BYTE_BGR);
Graphics g = background.getGraphics();
g.setColor(new Color(100, 50, 200));
g.fillRect(0, 0, 800, 600);
g.setColor(new Color(100, 100, 200));
for(int t=0; t<8; t++){
int x = (int)(Math.random() * 800) - 100;
int y = (int)(Math.random() * 600) - 100;
int size = (int)(Math.random() * 300);
g.fillOval(x, y, size, size);
}
g.dispose();
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(background,0,0,this);
//getContentPane().paint(g);
//g.dispose();
}
public IceCreamShop(String name){
super(name);
setSize(800, 600);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
RandomBackground();
getContentPane().setLayout(null);
catagories = new JLabel[6];
fields = new JTextField[6];
for(int t=0; t<6; t++){
catagories[t] = new JLabel(catagoryNames[t]);
fields[t] = new JTextField();
catagories[t].setHorizontalAlignment(JLabel.RIGHT);
catagories[t].setBounds(100, 100+t*40, 100, 25);
getContentPane().add(catagories[t]);
fields[t].setBounds(200, 100+t*40, 100, 25);
fields[t].setVisible(true);
getContentPane().add(fields[t]);
}
setVisible(true);
}
public static void main(String args[]){
IceCreamShop iceCreamShop = new IceCreamShop(":: Ben and Jerry's® :: Online Ice Cream Ordering System");
}
}