Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: java comp sci help

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default java comp sci help

    [B] the objective is to sort the circles based on color in each row. I cannot get the color pattern to run and then I need to display the colored circles across the applet



    // GraphicsLab06st.java
    // This is the Student Version of the Lab11GRFX06 assignment.

    import java.awt.*;
    import java.applet.*;
    import java.util.*;

    public class GraphicsLab06st extends Applet
    {
    public void paint(Graphics g)
    {
    int circleCount = 40;
    Circles circles = new Circles(g,circleCount);
    }
    }

    class Circles
    {
    private int circleCount;
    private Random rnd;
    private Color randomColor;
    private int colorRow;

    private int redCount, greenCount, blueCount;

    public Circles(Graphics g,int c)
    {
    rnd = new Random(12345);
    circleCount = c;
    redCount = 1;
    greenCount = 1;
    blueCount = 1;
    drawSquares(g);
    for (int k = 1; k <= circleCount; k++)
    drawRandomCircle(g);
    }

    public void drawSquares(Graphics g)
    {
    g.setColor(Color.red);
    g.fillRect(10,100,30,30);
    g.setColor(Color.green);
    g.fillRect(10,250,30,30);
    g.setColor(Color.blue);
    g.fillRect(10,400,30,30);
    }
    public void drawRandomCircle(Graphics g)
    {
    int wxtft = 10;

    getRandomColor();



    g.setColor(randomColor);
    g.fillOval (wxtft,colorRow,30,30);
    }

    public void getRandomColor()
    {



    // red
    int r = rnd.nextInt(256);


    // green
    int g = rnd.nextInt(256);
    // blue
    int b = rnd.nextInt(256);

    randomColor = new Color (r,g,b);
    if (r > g && r > b )
    {
    colorRow = 100 ;
    }
    if (g > b && r < g)
    {
    colorRow = 250 ;
    }

    if (b > r && g < b)
    {
    colorRow = 400;

    }

    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: java comp sci help

    Can you explain what it you mean by "sort the circles based on color" and say what problems you are having doing it?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    dylanoo (April 16th, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java comp sci help

    Quote Originally Posted by Norm View Post
    Can you explain what it you mean by "sort the circles based on color" and say what problems you are having doing it?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    I need the circles sorted based on color in a certain row next to the square of the same color across the screen but I am having issues with the get color method and drawing the circles across the screen.
    I have posted my instruction document in the attachments for futher info. Thank you for your time!
    Attached Files Attached Files

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: java comp sci help

    having issues with the get color method and drawing the circles across the screen.
    Please explain what the issues are.
    Post any doc you want associated with the problem on the thread without requiring a download.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    One problem I see is the creating of an object from inside the paint() method. Objects should be created outside the paint() method. Their methods can be called from inside paint().
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java comp sci help

    ok I am very sorry I attached the wrong word document. I have figured out my problems with the sorting on my own today but I just would to know how to get those circles to display across the screen.

    // GraphicsLab06st.java
    // This is the Student Version of the Lab11GRFX06 assignment.
     
    import java.awt.*;
    import java.applet.*;
    import java.util.*;
     
    public class GraphicsLab06st extends Applet
    { 
    public void paint(Graphics g)
    {
    int circleCount = 40;
    Circles circles = new Circles(g,circleCount);
    }
    }
     
    class Circles
    {
    private int circleCount;
    private Random rnd;
    private Color randomColor;
    private int colorRow;
     
    private int redCount, greenCount, blueCount;
     
    public Circles(Graphics g,int c)
    {
    rnd = new Random(12345);
    circleCount = c;
    redCount = 1;
    greenCount = 1;
    blueCount = 1;
    drawSquares(g);
    for (int k = 1; k <= circleCount; k++)
    drawRandomCircle(g);
    } 
     
    public void drawSquares(Graphics g)
    {
    g.setColor(Color.red);
    g.fillRect(10,100,30,30);
    g.setColor(Color.green);
    g.fillRect(10,250,30,30);
    g.setColor(Color.blue);
    g.fillRect(10,400,30,30);
    }
    public void drawRandomCircle(Graphics g)
    {
    int wxtft = 10;
     
    getRandomColor();
     
     
     
    g.setColor(randomColor);
    g.fillOval (wxtft,colorRow,30,30);
    }
     
    public void getRandomColor()
    {
     
     
     
    // red
    int r = rnd.nextInt(256);
     
     
    // green
    int g = rnd.nextInt(256);
    // blue
    int b = rnd.nextInt(256);
     
    randomColor = new Color (r,g,b);
    if (r > g && r > b ) 
    {
    colorRow = 100 ;
    }
    if (g > b && r > g) 
    {
    colorRow = 250 ;
    }
     
    if (b > r && g > b)
    {
    colorRow = 400;
     
    }
     
    }
    } .
     
     
     
    code=java]
    <YOUR CODE HERE>
    Last edited by Norm; April 17th, 2013 at 08:35 PM. Reason: Added first code tag

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: java comp sci help

    The code tags should go around the code, not at the end of the code. The code should be between the code tags at the place that says: <YOUR CODE HERE>

    One problem I see is the creating of the Circle object from inside the paint() method. Objects should be created outside the paint() method. Their methods can be called from inside paint().

    how to get those circles to display across the screen.
    Change the y values of where the circles are drawn to have them display across the screen.


    The code needs formatting. Nested statements should be indented. All statements should NOT start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Frog jumping code (AP comp science help!)
    By CompScienceStudent1 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 21st, 2011, 06:42 PM
  2. useing the comp's phone jack
    By wolfgar in forum Java Theory & Questions
    Replies: 10
    Last Post: November 7th, 2009, 03:57 PM