Skip to main content
GameDev.net gamedev.net
🔒 Locked

My Java Problem: Can only draw background OR components

Started by gandolf1212 Sep 26, 2005 at 8:10 AM 2 replies 550+ views
Original Post
gandolf1212
gandolf1212
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");

	}

}

I think there's to much blood in my caffeine system.
Dave Hunt
Dave Hunt
Try moving the background drawing to the paintComponent method instead of the paint method.
Zahlman
Zahlman
// old//		super.paint(g);//		g.drawImage(background,0,0,this);// new		g.drawImage(background,0,0,this);		super.paint(g);


The "super must be first" rule only applies to constructors (where the specified super call replaces the default super call of the super's default ctor). At least, that's what my tests indicate.
Dave Hunt
Dave Hunt
I'm pretty sure that super.paint() is going to call paintComponent() (or drawComponent, I forget what it's called) which clears the background.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.