Skip to main content
gamedev.net
🔒 Locked

Storing random background in Java

Started by gandolf1212 Sep 19, 2005 at 1:40 PM 3 replies 850+ views
Original Post
gandolf1212
gandolf1212
My program creates a background randomly in java. for(int t=0; t<8; t++){ g.setColor(new Color(100, 100, 200)); 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); } Problem is it doesn't store the coords, so it can't repaint. Is there some way I could store the background in a secondary canvas, akin to a dx back buffer, and then copy the secondary canvas back over the first in the paint method?
I think there's to much blood in my caffeine system.
H_o_p_s
H_o_p_s
What you want to do is create a BufferedImage, draw your random background to it, and then every paint() just copy the backround BufferedImage to your main container.

If you need help implementing this just let me know.
BRING BACK THE BLACK (or at least something darker)
gandolf1212
gandolf1212
Thinking I'm going to need about all the code for that :/

I tried doing something similiar, but using Image. It worked about 50% of the time, I have no idea what determined whether or not it worked, but I think it might have to do with either the ovals writing outside the given canvas or the form taking to long / not logn enough to turn visible.

Not often a language makes you miss C++ :(
I think there's to much blood in my caffeine system.
H_o_p_s
H_o_p_s
Here you go!

import	java.awt.image.BufferedImage;import	java.awt.Graphics;import	java.awt.Color;import	java.awt.Graphics2D;import	javax.swing.JFrame;public class RandomBackground extends JFrame {	BufferedImage	background;		public RandomBackground() {		//Create a new Image		background = new BufferedImage(800,600, BufferedImage.TYPE_3BYTE_BGR);		//Create a Graphics Context to the Image		Graphics g = background.getGraphics();		//Generate randome background		for(int t=0; t<8; t++){			g.setColor(new Color(100, 100, 200));			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);		}		//Dispose of Graphics		g.dispose();	}		public void paint(Graphics g) {		//Copy the background to the screen		g.drawImage(background,0,0,null);		//Dispose of Graphics		g.dispose();	}		public static void main(String[] args) {		RandomBackground rb = new RandomBackground();		rb.setTitle("Random Background Generator");		rb.setSize(800,600);		rb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		rb.setResizable(false);		rb.setVisible(true);	}}
If you have any questions about this, just let me know!

H_o_p_s
BRING BACK THE BLACK (or at least something darker)
gandolf1212
gandolf1212
Wow! Looks great so far. I'll add my code to it today and see if it starts crashing again =(

Also, I love not having to add a window close handler... I suck at java :/

Thanks a ton, this is a huge help!
I think there's to much blood in my caffeine system.

Topic Locked

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

Sign in to reply to this topic.