Results 1 to 15 of 15
Thread: Colors and shapes.
- 10-10-2008, 02:17 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
Colors and shapes.
How can I make the dots have random colors?
The program should draw 5 dots with with a line beetween them and different colors..
The code starts here:
import java.awt.*;
import javax.swing.*;
import java.util.Random;
class Vindu extends JFrame {
public Vindu(String tittel) {
setTitle(tittel);
setSize(420, 420); // bredde, høyde
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Tegning tegningen = new Tegning();
add(tegningen);
}
}
class Tegning extends JPanel {
private static final int diameter = 10;
private static final int sentr = diameter / 2;
private static final int antStreker = 4;
private Random tallGen = new Random();
private int maksXverdi;
private int maksYverdi;
/*generer tilfeldig farge
Color color = new Color( randomNumbers.nextInt( 256 ),
randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) );
*/
public void paintComponent(Graphics tegneflate) {
super.paintComponent(tegneflate);
maksXverdi = getWidth() - 1;
maksYverdi = getHeight() - 1;
int x = tallGen.nextInt(maksXverdi - diameter);
int y = tallGen.nextInt(maksYverdi - diameter);
tegneflate.fillOval(x, y, diameter, diameter);
int teller = 0;
while (teller < antStreker) {
int forrigeX = x;
int forrigeY = y;
x = tallGen.nextInt(maksXverdi - diameter);
y = tallGen.nextInt(maksYverdi - diameter);
tegneflate.drawLine(forrigeX + sentr, forrigeY + sentr, x + sentr, y + sentr);
tegneflate.fillOval(x, y, diameter, diameter);
teller++;
}
}
}
class TegnOving7 {
public static void main(String[] args) {
Vindu etVindu = new Vindu("Tilfeldige former og flekker i vindu");
etVindu.setVisible(true);
}
}
- 10-10-2008, 03:12 PM #2
Do you get errors? Copy and past them here.
Does the program execute?
Is the output NOT what you want? Please explain.
I don't see where you set the color.
To have a new color for each dot, You need to create a new object color before you draw it.
- 10-10-2008, 11:07 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
The problem is that I dont know how to use, the random function on the dots.. For the color.
- 10-10-2008, 11:20 PM #4
Have you read the API doc for the Random class? Your code uses the nextInt method. That should work.
What was your problem?
Copy and paste the error messages here.
The code would be something like this:
create a new color using Random method.
set the color
fill the shape
- 10-11-2008, 10:44 AM #5
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
Problem is that I dont know where to put the color lines, to affect the dots..
-
If you haven't read the Sun tutorials on drawing in components and on 2D graphics, you may wish to take a look as much will be explained there.
- 10-11-2008, 02:56 PM #7
What does that mean? Are the lines you refer to being drawn by the drawLine method?where to put the color lines, to affect the dots..
You set the color with a method.
You draw a line with a method. Its location ("where") is specified in the method's args.
If you set the color before drawing the line, the line will have that color.
- 10-11-2008, 10:07 PM #8
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
What I mean is that I want the dots to be coloured instead of black, and random colors..
- 10-11-2008, 10:24 PM #9
Did you try setting the color before drawing the dots?
- 10-12-2008, 12:44 AM #10
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
This is what I get and all I seem to get.. No clue whatso ever...
C:\Java\Oving 7\TegnOving7.java:27: cannot find symbol
symbol : variable randomNumbers
location: class Tegning
Color color = new Color( randomNumbers.nextInt( 256 ),
^
C:\Java\Oving 7\TegnOving7.java:28: cannot find symbol
symbol : variable randomNumbers
location: class Tegning
randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) );
^
C:\Java\Oving 7\TegnOving7.java:28: cannot find symbol
symbol : variable randomNumbers
location: class Tegning
randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) );
^
3 errors
Tool completed with exit code 1
CODE:
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.Graphics;
import java.util.Random;
class Vindu extends JFrame {
public Vindu(String tittel) {
setTitle(tittel);
setSize(420, 420); // bredde, høyde
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Tegning tegningen = new Tegning();
add(tegningen);
}
}
class Tegning extends JPanel {
Color color = new Color( randomNumbers.nextInt( 256 ),
randomNumbers.nextInt( 256 ), randomNumbers.nextInt( 256 ) );
private static final int diameter = 10;
private static final int sentrum = diameter / 2;
private static final int antStreker = 4;
private Random tallGen = new Random();
private int maksXverdi;
private int maksYverdi;
public void paintComponent(Graphics tegneflate) {
super.paintComponent(tegneflate);
maksXverdi = getWidth() - 1;
maksYverdi = getHeight() - 1;
int x = tallGen.nextInt(maksXverdi - diameter);
int y = tallGen.nextInt(maksYverdi - diameter);
tegneflate.fillOval(x, y, diameter, diameter);
tegneflate.fillOval(x, y, diameter, diameter);
int teller = 0;
while (teller < antStreker) {
int forrigeX = x;
int forrigeY = y;
x = tallGen.nextInt(maksXverdi - diameter);
y = tallGen.nextInt(maksYverdi - diameter);
tegneflate.drawLine(forrigeX + sentrum, forrigeY + sentrum, x + sentrum, y + sentrum);
tegneflate.fillOval(x, y, diameter, diameter);
teller++;
}
}
}
class TegnOving7 {
public static void main(String[] args) {
Vindu etVindu = new Vindu("Tilfeldige former og flekker i vindu");
etVindu.setVisible(true);
}
}
-
the error messages are telling you what's wrong: you're using a method that either doesn't exist or if it does exist isn't visible from where it is being called. You may wish to initialize the Colors in your constructor and take care to use only methods or objects that are present and within scope of the constructor.
- 10-12-2008, 03:31 AM #12
Here you've defined an instance of Random and you use it in several places.Random tallGen = new Random();
Where do you define: randomNumbers ?
- 10-12-2008, 11:42 AM #13
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
Thanks for them answers.
Randomnumber should be tallGen so I change it to that. But where should I put the colorrandom generator so It can change the colors of the dots?
I tried to put it over the filloval put it had no effect..
class Tegning extends JPanel {
// Konstruktør
private static final int diameter = 10;
private static final int sentrum = diameter / 2;
private static final int antStreker = 14;
private Random tallGen = new Random();
private int maksXverdi;
private int maksYverdi;
public void paintComponent(Graphics tegneflate) {
super.paintComponent(tegneflate);
maksXverdi = getWidth() - 1;
maksYverdi = getHeight() - 1;
int x = tallGen.nextInt(maksXverdi - diameter);
int y = tallGen.nextInt(maksYverdi - diameter);
tegneflate.fillOval(x, y, diameter, diameter);
tegneflate.fillOval(x, y, diameter, diameter);
int teller = 0;
while (teller < antStreker) {
int forrigeX = x;
int forrigeY = y;
x = tallGen.nextInt(maksXverdi - diameter);
y = tallGen.nextInt(maksYverdi - diameter);
Color color = new Color( tallGen.nextInt( 256 ), tallGen.nextInt( 256 ), tallGen.nextInt( 256 ) );
tegneflate.fillOval(x, y, diameter, diameter);
teller++;
}
}
}
- 10-12-2008, 02:37 PM #14
Where do you use the color you've created? You have to tell the system what you want it to use it? Look at the API doc for Graphics to see what method to use to set the color to be used.
- 10-13-2008, 05:25 PM #15
Member
- Join Date
- Oct 2008
- Posts
- 33
- Rep Power
- 0
Thnaks for them answers, this is the final outcome.
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.Graphics;
import java.util.Random;
import java.awt.Color;
class Vindu extends JFrame {
public Vindu(String tittel) {
setTitle(tittel);
setSize(420, 420); // bredde, høyde
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Tegning tegningen = new Tegning();
add(tegningen);
}
}
class Tegning extends JPanel {
private static final int diameter = 10;
private static final int sentrum = diameter / 2;
private static final int antStreker = 14;
private Random tallGen = new Random();
private int maksXverdi;
private int maksYverdi;
public void paintComponent(Graphics tegneflate) {
super.paintComponent(tegneflate);
maksXverdi = getWidth() - 1;
maksYverdi = getHeight() - 1;
int x = tallGen.nextInt(maksXverdi - diameter);
int y = tallGen.nextInt(maksYverdi - diameter);
tegneflate.fillOval(x, y, diameter, diameter);
tegneflate.fillOval(x, y, diameter, diameter);
int teller = 0;
while (teller < antStreker) {
int forrigeX = x;
int forrigeY = y;
x = tallGen.nextInt(maksXverdi - diameter);
y = tallGen.nextInt(maksYverdi - diameter);
int fargevalg = tallGen.nextInt(3);
if(fargevalg == 0) tegneflate.setColor(Color.red);
if(fargevalg == 1) tegneflate.setColor(Color.pink);
if(fargevalg == 2) tegneflate.setColor(Color.yellow);
int formvalg = tallGen.nextInt(3);
if(formvalg == 0)tegneflate.fillOval (x, y, diameter, diameter);
if(formvalg == 1)tegneflate.drawOval(x, y, diameter, diameter);
if(formvalg == 2)tegneflate.fillRect (x, y, 100, 100);
teller++;
}
}
}
class TegnOving7 {
public static void main(String[] args) {
Vindu etVindu = new Vindu("Tilfeldige flekker i vindu med farger");
etVindu.setVisible(true);
}
}
Similar Threads
-
How do I use multiple background colors?
By trbox in forum New To JavaReplies: 1Last Post: 09-02-2008, 01:00 PM -
change colors
By calblue in forum New To JavaReplies: 1Last Post: 12-02-2007, 11:52 PM -
how to set multiple colors in a JtextArea
By valery in forum AWT / SwingReplies: 2Last Post: 08-01-2007, 05:12 PM -
Help with basic shapes in java
By carl in forum Java 2DReplies: 1Last Post: 07-31-2007, 11:40 PM -
Help, basic shapes using java
By coco in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 08:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks