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 2 of 2

Thread: Square.java and DrawingSquares.java PLEASE HELP IM STUCK

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

    Exclamation Square.java and DrawingSquares.java PLEASE HELP IM STUCK

    The assignment:
    1. Write a class Square that represents a square to be drawn. Store the following information in instance variables:
    �� size (length of any side)
    �� x coord of upper left-hand corner
    �� y coord of upper left-hand corner
    �� color
    Provide the following methods:
    �� A parameterless constructor that generates random values for the size, color, x, and y. Make the size between 100
    and 200, x between 0 and 600, y between 0 and 400. The squares can be any color—note that you can pass a single
    int parameter to the Color constructor, but it will only consider the first 24 bits (8 bits for each of R, G, B
    component).
    �� IMPORTANT: Your random number generator must be declared at the class level (not inside the constructor), and
    must be declared static. So its declaration and initialization should appear with the declarations of size, color, x, and
    y, and should look like this:
    private static Random generator = new Random();
    �� A draw method that draws the square at its x,y coordinate in its color. Note that you need a Graphics object, like the
    page parameter of the paint method, to draw. Your draw method should take a Graphics object as a parameter.
    2. Now write an applet DrawSquares that uses your Square class to create and draw 5 squares. This code should be very
    simple; the paint method will simply create a Square and then draw it, repeated 5 times. Don’t forget to pass the
    Graphics object to draw.

    My Code:
    Square.java (The driver)

    package Mackenzie;
    //*************************************....
    // Square.java
    // Mackenzie
    // 10/28/13
    //*************************************....
    import java.awt.*;
    import javax.swing.*;
    import java.util.Random;
    public class Square
    {
    private int size;
    private int x;
    private int y;
    private Color color;
    private int a1, a2, a3;
    private static Random generator = new Random();
    public Square(){
    size = generator.nextInt(51) + 50;
    x = generator.nextInt(301);
    y = generator.nextInt(301);
    a1 = generator.nextInt(256);
    a2 = generator.nextInt(256);
    a3 = generator.nextInt(256);
    color = new Color(a1, a2, a3);
    }
    public void draw(Graphics pen)
    {
    pen.setColor(color);
    pen.fillRect(x, y, size, size);
    }
    }

    DrawingSquares.java

    package Mackenzie;
    //*************************************....
    // DrawSquares.java
    // Mackenzie
    // 10/28/13
    //*************************************....
    import java.awt.*;
    import javax.swing.*;
    public class DrawSquares extends JApplet {
    public int i;
    Square[] square;
    public void init(){
    square = new Square[5];
    for(int i = 0; i < square.length; i++);
    square[i] = new Square();
    }
    public void paint(Graphics g) {
    super.paint(g);
    for(int i = 0; i < square.length; i++);
    square[i].draw(g);
    }
    }

    It's supposed to draw five squares and every time you move the applet window, it is supposed to continue to draw 5 squares. Can you tell me what I'm doing wrong?
    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,330
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Square.java and DrawingSquares.java PLEASE HELP IM STUCK

    what I'm doing wrong
    What is wrong?
    Can you explain what the code does that you want to change?

    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.

Similar Threads

  1. JAVA MAGIC SQUARE
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 27th, 2012, 11:42 PM
  2. Java error correcting, testing for a square issue i think
    By djl1990 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2011, 03:17 AM
  3. Replies: 2
    Last Post: July 23rd, 2011, 03:29 PM
  4. Java Calculator Square Root Function
    By laser1992 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 3rd, 2011, 08:34 AM
  5. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM

Tags for this Thread