12 Replies - 9763 Views - Last Post: 19 April 2011 - 08:20 PM Rate Topic: -----

#1 mcolli03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-April 11

Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 08:18 AM

import javax.swing.*; // for swing components
import java.awt.*; // for layout manager
import java.awt.event.*; // for the ActionListener interface
import java.util.Random; // for random number generation
public class TestMyDraw
{
public static void main( String[] args )
{
MyDraw panel = new MyDraw(); // create a GUI
JFrame application = new JFrame(); // create a JFrame
application.add( panel ); // add the GUI to JFrame
application.setSize( 600, 600 ); // set JFrame size
application.setVisible( true ); // make JFrame visible
}
}
class MyDraw extends JPanel implements ActionListener
{
private JButton rButton, gButton, bButton, rndButton;
private Color color;
private Random rd = new Random();
boolean rnd;
public MyDraw() // constructor: GUI is built here
{
rButton = new JButton( "RED" );
rButton.addActionListener( this );
gButton = new JButton( "GREEN" );
gButton.addActionListener( this );
bButton = new JButton( "BLUE" );
bButton.addActionListener( this );
rndButton = new JButton( "RANDOM" );
rndButton.addActionListener( this );
// create a control panel to build the GUI
JPanel control = new JPanel();
// set the way to arrange buttons added to the control panel
control.setLayout( new GridLayout( 1, 0 ) );
control.add( rButton ); // add the RED button to the panel
control.add( gButton );
control.add( bButton );
control.add( rndButton );
// set the way to arrange components added to JPanel
setLayout( new BorderLayout(5, 5) );
add( "South", control ); // add the control panel to JPanel
}
// this function is called whenever a button is clicked
public void actionPerformed( ActionEvent e )
{
if ( e.getSource() == rButton ) // RED button is clicked
color = new Color( 255, 0, 0 );
else if ( e.getSource() == gButton )
color = new Color( 0, 255, 0 );
else if ( e.getSource() == bButton )
color = new Color( 0, 0, 255 );
else if ( e.getsource() == rndButton )
color = new Color(256, 256, 256 );
repaint(); // this will call function, paintComponent()
}
// drawings are implemented in this function
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // clear drawing area
g.setColor( color ); // set ípení color
int width = getWidth(); // get drawing area width
int height = getHeight(); // get drawing area height
for ( int x = width/6; x < width*5/6; x+=35 )
for ( int y = height/6; y < height*5/6; y+=35 )
g.fillOval( x, y, 30, 30 );
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Creating random color generator, getting error cannot find symbol...

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • View blog
  • Posts: 3,166
  • Joined: 05-April 11

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 08:29 AM

There isn't a color 256 256 256

To get a random color use the Random class:
Random rnd = new Random();

public Color getRandomColor() {
	int random = rnd.nextInt(4); //Get a random number from 0-4 (without 4)
	
	switch(random) {
		case 0:
			return Color.BLACK;
		case 1:
			return Color.BLUE;
		case 2:
			return Color.GREEN;
		case 3:
			return Color.RED;
		default : return Color.WHITE;
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 mcolli03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-April 11

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 08:36 AM

View PostCasiOo, on 19 April 2011 - 08:29 AM, said:

There isn't a color 256 256 256

To get a random color use the Random class:
Random rnd = new Random();

public Color getRandomColor() {
	int random = rnd.nextInt(4); //Get a random number from 0-4 (without 4)
	
	switch(random) {
		case 0:
			return Color.BLACK;
		case 1:
			return Color.BLUE;
		case 2:
			return Color.GREEN;
		case 3:
			return Color.RED;
		default : return Color.WHITE;
	}
}


Okay, so to be able to get any color, can I do an if statement with: rnd.nextInt( 0 > 256 ); ? I am trying to be able to get any color when the random button is chosen. Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • View blog
  • Posts: 3,166
  • Joined: 05-April 11

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 08:52 AM

View Postmcolli03, on 19 April 2011 - 08:36 AM, said:

View PostCasiOo, on 19 April 2011 - 08:29 AM, said:

There isn't a color 256 256 256

To get a random color use the Random class:
Random rnd = new Random();

public Color getRandomColor() {
	int random = rnd.nextInt(4); //Get a random number from 0-4 (without 4)
	
	switch(random) {
		case 0:
			return Color.BLACK;
		case 1:
			return Color.BLUE;
		case 2:
			return Color.GREEN;
		case 3:
			return Color.RED;
		default : return Color.WHITE;
	}
}


Okay, so to be able to get any color, can I do an if statement with: rnd.nextInt( 0 > 256 ); ? I am trying to be able to get any color when the random button is chosen. Thank you for your help.


No you should do it like rnd.nextInt( 256 ), which will give you a number in the range 0-255. You can read about the method here Random class

If you want to do it like new Color( randomNumber, randomNumber, randomNumber ); then I suggest you do it like:
public Color getRandomColor() {
	return new Color( rnd.nextInt( 256 ), rnd.nextInt( 256 ), rnd.nextInt( 256 ) ); 
}


This post has been edited by CasiOo: 19 April 2011 - 08:53 AM

Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,994
  • Joined: 20-September 08

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 11:12 AM

You can simplify that:

    public Color getRandomColor() {
	return new Color((int)(Math.random() * 0xFFFFFF));
    }


Was This Post Helpful? 1
  • +
  • -

#6 mcolli03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-April 11

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 11:42 AM

View Postg00se, on 19 April 2011 - 11:12 AM, said:

You can simplify that:

    public Color getRandomColor() {
	return new Color((int)(Math.random() * 0xFFFFFF));
    }


I finally got it to work, but now I am trying to figure out how to make each individual circle a random color, instead of all the circles/whole page.I am pasting my code again, I have tried several different things, but I am not sure where I should be going with this. Thank you.
import javax.swing.*; // for swing components
import java.awt.*; // for layout manager
import java.awt.event.*; // for the ActionListener interface
import java.util.Random; // for random number generation
public class TestMyDraw
{
public static void main( String[] args )
{
MyDraw panel = new MyDraw(); // create a GUI
JFrame application = new JFrame(); // create a JFrame
application.add( panel ); // add the GUI to JFrame
application.setSize( 600, 600 ); // set JFrame size
application.setVisible( true ); // make JFrame visible
}
}
class MyDraw extends JPanel implements ActionListener
{
private JButton rButton, gButton, bButton, rndButton;
private Color color;
private Random rd = new Random();
boolean rnd = true;
public MyDraw() // constructor: GUI is built here
{
rButton = new JButton( "RED" );
rButton.addActionListener( this );
gButton = new JButton( "GREEN" );
gButton.addActionListener( this );
bButton = new JButton( "BLUE" );
bButton.addActionListener( this );
rndButton = new JButton( "RANDOM" );
rndButton.addActionListener( this );
// create a control panel to build the GUI
JPanel control = new JPanel();
// set the way to arrange buttons added to the control panel
control.setLayout( new GridLayout( 1, 0 ) );
control.add( rButton ); // add the RED button to the panel
control.add( gButton );
control.add( bButton );
control.add( rndButton );
// set the way to arrange components added to JPanel
setLayout( new BorderLayout(5, 5) );
add( "South", control ); // add the control panel to JPanel
}
// this function is called whenever a button is clicked
public void actionPerformed( ActionEvent e )
{
if ( e.getSource() == rButton ) // RED button is clicked
color = new Color( 255, 0, 0 );
else if ( e.getSource() == gButton )
color = new Color( 0, 255, 0 );
else if ( e.getSource() == bButton )
color = new Color( 0, 0, 255 );
else if ( e.getSource() == rndButton )
color = new Color(rd.nextInt(256),rd.nextInt(256),rd.nextInt(256) );
repaint(); // this will call function, paintComponent()
}
// drawings are implemented in this function
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // clear drawing area
g.setColor( color ); // set ípení color
int width = getWidth(); // get drawing area width
int height = getHeight(); // get drawing area height
for ( int x = width/6; x < width*5/6; x+=35 )
for ( int y = height/6; y <height*5/6; y+=35 )
g.fillOval( x, y, 30, 30 );
}
}

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,994
  • Joined: 20-September 08

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 11:50 AM

Use the loop:

    // drawings are implemented in this function
    public void paintComponent(Graphics g) {
        super.paintComponent(g); // clear drawing area
        g.setColor(color); // set ípení color

        int width = getWidth(); // get drawing area width
        int height = getHeight(); // get drawing area height

        for (int x = width / 6; x < ((width * 5) / 6); x += 35) {
            for (int y = height / 6; y < ((height * 5) / 6); y += 35) {
                g.fillOval(x, y, 30, 30);
                g.setColor(getRandomColor());
            }
	}
    }

    public Color getRandomColor() {
        return new Color((int)(Math.random() * 0xFFFFFF));
    }


Was This Post Helpful? 0
  • +
  • -

#8 mcolli03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-April 11

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 12:08 PM

Now the red, green, & blue buttons are doing random colors...
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,994
  • Joined: 20-September 08

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 12:10 PM

Well i don't know what those buttons are meant to do actually
Was This Post Helpful? 0
  • +
  • -

#10 mcolli03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-April 11

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 12:15 PM

View Postg00se, on 19 April 2011 - 12:10 PM, said:

Well i don't know what those buttons are meant to do actually

Red button makes all circles red, green button makes all circles green, blue button makes all the circles blue, and random button is supposed to make them all a different color. The way you showed me did work, but I think I might need to change the boolean(?) to a true/false...not really sure though.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 08:01 PM

View Postmcolli03, on 19 April 2011 - 02:15 PM, said:

View Postg00se, on 19 April 2011 - 12:10 PM, said:

Well i don't know what those buttons are meant to do actually

Red button makes all circles red, green button makes all circles green, blue button makes all the circles blue, and random button is supposed to make them all a different color. The way you showed me did work, but I think I might need to change the boolean(?) to a true/false...not really sure though.

Something like
class MyDraw extends JPanel implements ActionListener
{
	private JButton rButton, gButton, bButton, rndButton;
	private Color color;
	private Random rd = new Random();
	boolean rnd = true;
	boolean useRandomColor = true;      // <----------------

	// this function is called whenever a button is clicked
	public void actionPerformed( ActionEvent e )
	{
		useRandomColor = false;
		if ( e.getSource() == rButton ) // RED button is clicked
			color = Color.RED;
		else if ( e.getSource() == gButton )
			color = Color.GREEN;
		else if ( e.getSource() == bButton )
			color = Color.BLUE;
		else if ( e.getSource() == rndButton )
			useRandomColor = true;
		repaint(); // this will call function, paintComponent()
	}
	// drawings are implemented in this function
	public void paintComponent( Graphics g )
	{
		super.paintComponent( g ); // clear drawing area
		g.setColor( color ); // set ípení color
		int width = getWidth(); // get drawing area width
		int height = getHeight(); // get drawing area height
		for ( int x = width/6; x < width*5/6; x+=35 )
			for ( int y = height/6; y <height*5/6; y+=35 ) {
				if(useRandomColor)
					g.setColor(new Color(rd.nextInt(255), rd.nextInt(255), rd.nextInt(255)));
				g.fillOval( x, y, 30, 30 );
			}
	}
}


Correctly indented free of charge

This post has been edited by pbl: 19 April 2011 - 08:02 PM

Was This Post Helpful? 0
  • +
  • -

#12 mcolli03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-April 11

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 08:17 PM

View Postpbl, on 19 April 2011 - 08:01 PM, said:

View Postmcolli03, on 19 April 2011 - 02:15 PM, said:

View Postg00se, on 19 April 2011 - 12:10 PM, said:

Well i don't know what those buttons are meant to do actually

Red button makes all circles red, green button makes all circles green, blue button makes all the circles blue, and random button is supposed to make them all a different color. The way you showed me did work, but I think I might need to change the boolean(?) to a true/false...not really sure though.

Something like
class MyDraw extends JPanel implements ActionListener
{
	private JButton rButton, gButton, bButton, rndButton;
	private Color color;
	private Random rd = new Random();
	boolean rnd = true;
	boolean useRandomColor = true;      // <----------------

	// this function is called whenever a button is clicked
	public void actionPerformed( ActionEvent e )
	{
		useRandomColor = false;
		if ( e.getSource() == rButton ) // RED button is clicked
			color = Color.RED;
		else if ( e.getSource() == gButton )
			color = Color.GREEN;
		else if ( e.getSource() == bButton )
			color = Color.BLUE;
		else if ( e.getSource() == rndButton )
			useRandomColor = true;
		repaint(); // this will call function, paintComponent()
	}
	// drawings are implemented in this function
	public void paintComponent( Graphics g )
	{
		super.paintComponent( g ); // clear drawing area
		g.setColor( color ); // set ípení color
		int width = getWidth(); // get drawing area width
		int height = getHeight(); // get drawing area height
		for ( int x = width/6; x < width*5/6; x+=35 )
			for ( int y = height/6; y <height*5/6; y+=35 ) {
				if(useRandomColor)
					g.setColor(new Color(rd.nextInt(255), rd.nextInt(255), rd.nextInt(255)));
				g.fillOval( x, y, 30, 30 );
			}
	}
}


Correctly indented free of charge

Exactly like that! Thank you so much! I have been trying to figure it out all day. :)
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Creating random color generator, getting error cannot find symbol...

Posted 19 April 2011 - 08:20 PM

Glad I could help
Happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1