Results 1 to 15 of 15
  1. #1
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default 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);
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    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.

  3. #3
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    The problem is that I dont know how to use, the random function on the dots.. For the color.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    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

  5. #5
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    Problem is that I dont know where to put the color lines, to affect the dots..

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    where to put the color lines, to affect the dots..
    What does that mean? Are the lines you refer to being drawn by the drawLine method?
    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.

  8. #8
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    What I mean is that I want the dots to be coloured instead of black, and random colors..

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Did you try setting the color before drawing the dots?

  10. #10
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    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);
    }
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Random tallGen = new Random();
    Here you've defined an instance of Random and you use it in several places.
    Where do you define: randomNumbers ?

  13. #13
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    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++;
    }
    }
    }

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    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.

  15. #15
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    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

  1. How do I use multiple background colors?
    By trbox in forum New To Java
    Replies: 1
    Last Post: 09-02-2008, 01:00 PM
  2. change colors
    By calblue in forum New To Java
    Replies: 1
    Last Post: 12-02-2007, 11:52 PM
  3. how to set multiple colors in a JtextArea
    By valery in forum AWT / Swing
    Replies: 2
    Last Post: 08-01-2007, 05:12 PM
  4. Help with basic shapes in java
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 11:40 PM
  5. Help, basic shapes using java
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •