mardi 21 avril 2015

Random Rects, Ovals, and Lines

Doing an assignment its due tonight (Procrastination D:) I am almost done I think but the Ovals are messing up and I cant find out what is wrong I know it is just the oval because If i delete the oval or unlink it the applet will run perfectly fine with just the lines and rectangles and whatnot. If you can provide a solution and also an explanation that would be very awesome!

Here is my:

DrawPanel

    // DrawPanel.java.
// Author: Mike Qualls

package lab4;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class DrawPanel extends JPanel {
    // declare instance variables
        private Random randomNumbers = new Random ();
        private MyLine lines [];
        private MyRectangle rects [];
        private MyOval ovals [];
        static final long serialVersionUID = 0;
        // the constructor
        public DrawPanel () {
            // declare local variables
            final int NUMBER_COLORS = 256;  // (0-255 color values)
            final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 300;
            int x1, y1, x2, y2;
            Color color;
            boolean fill;


            // set background color
            setBackground (Color.WHITE);

            // allocate the arrays  (5 to 9 references)
            lines = new MyLine [5 + randomNumbers.nextInt (5)];
            rects = new MyRectangle [5 + randomNumbers.nextInt (5)];

            // create the MyLine objects
            for (int count = 0; count < lines.length; count++) {
                // generate random coordinates (0 to 299)
                x1 = randomNumbers.nextInt (WINDOW_WIDTH);
                y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
                x2 = randomNumbers.nextInt (WINDOW_WIDTH);
                y2 = randomNumbers.nextInt (WINDOW_HEIGHT);

                // generate a random color
                color = new Color (randomNumbers.nextInt (NUMBER_COLORS),
                                            randomNumbers.nextInt (NUMBER_COLORS),
                                                randomNumbers.nextInt (NUMBER_COLORS));

                // construct a MyLine object
                lines [count] = new MyLine (x1, y1, x2, y2, color);
            }  // end for loop to create MyLine objects
            // create the MyRectangle objects
            for (int count = 0; count < rects.length; count++) {
                // generate random coordinates (0 to 299)
                x1 = randomNumbers.nextInt (WINDOW_WIDTH);
                y1 = randomNumbers.nextInt (WINDOW_HEIGHT);
                x2 = randomNumbers.nextInt (WINDOW_WIDTH);
                y2 = randomNumbers.nextInt (WINDOW_HEIGHT);

                // generate a random color
                color = new Color (randomNumbers.nextInt (NUMBER_COLORS),
                                            randomNumbers.nextInt (NUMBER_COLORS),
                                                randomNumbers.nextInt (NUMBER_COLORS));
                fill = randomNumbers.nextBoolean ();

                // construct a MyRectangle & MyOval object
                rects [count] = new MyRectangle (x1, y1, x2, y2, color, fill);
                ovals [count] = new MyOval (x1, y1, x2, y2, color, fill);
            }  // end for loop to create MyRectangle objects
        }  // end drawPanel constructor


        // draw the lines, rectangles, and ovals
        public void paintComponent (Graphics g) {
            // paint the parent first
            super.paintComponent (g);

            // draw the lines, rectangles, and ovals
            for (MyLine line : lines) 
                line.draw (g);
            for (MyRectangle rect : rects)
                rect.draw (g);
            for (MyOval oval : ovals)
                oval.draw (g);
        }  // end method paintComponent
    }  // end class DrawPanel

MyLines

    // MyLine.java.
// Author: Mike Qualls

package lab4;

import java.awt.Graphics;
import java.awt.Color;

public class MyLine {
    private int x1, y1, x2, y2;
    private Color myColor;

    // the constructors
    public MyLine () {
        // the default
        this.x1 = 0;
        this.y1 = 0;
        this.x2 = 0;
        this.y2 = 0;
        this.myColor = Color.BLACK; 
    }  // end default constructor
    public MyLine (int x1, int y1, int x2, int y2, Color color) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.myColor = color;
    }  // end constructor

    // sets/gets
    public void setX1 (int x1) {
        this.x1 = x1;
    }
    public void setY1 (int y1) {
        this.y1 = y1;
    }
    public void setX2 (int x2) {
        this.x2 = x2;
    }
    public void setY2 (int y2) {
        this.y2 = y2;
    }
    public void setColor (Color color) {
        this.myColor = color;
    }
    public int getX1 () {
        return x1;
    }
    public int getY1 () {
        return y1;
    }
    public int getX2 () {
        return x2;
    }
    public int getY2 () {
        return y2;
    }
    public Color getColor () {
        return myColor;
    }
    // draw the shape
    public void draw (Graphics g) {
        g.setColor (myColor);
        g.drawLine (x1, y1, x2, y2);
    }  // end method draw

}  // end class MyLine

MyRectangle

   package lab4;

import java.awt.Color;
import java.awt.Graphics;


public class MyRectangle  {
    // declare instance variables
    private int x1, y1, x2, y2;
    private Color myColor;
    private boolean fill;       // to fill or not

    // the constructors
    public MyRectangle () {
        // the default
        this.x1 = 0;
        this.y1 = 0;
        this.x2 = 0;
        this.y2 = 0;
        this.myColor = Color.BLACK;
        this.fill = false;
    }  // end default constructor

    public MyRectangle (int x1, int y1, int x2, int y2, Color color, boolean fill) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.myColor = color;
        this.fill = fill;
    }  // end constructor

    // sets/gets
    public void setX1 (int x1) {
        this.x1 = x1;
    }
    public void setY1 (int y1) {
        this.y1 = y1;
    }
    public void setX2 (int x2) {
        this.x2 = x2;
    }
    public void setY2 (int y2) {
        this.y2 = y2;
    }
    public void setColor (Color color) {
        this.myColor = color;
    }
    public void setFill (boolean fill) {
        this.fill = fill;
    }
    public int getX1 () {
        return x1;
    }
    public int getY1 () {
        return y1;
    }
    public int getX2 () {
        return x2;
    }
    public int getY2 () {
        return y2;
    }
    public Color getColor () {
        return myColor;
    }
    public boolean getFill () {
        return fill;
    }

    // now the gets for calculated values
    public int getUpperLeftX () {
        // defined as the minimum of x1 and x2
        return Math.min (x1, x2);
    }
    public int getUpperLeftY () {
        // defined as the minimum of y1 and y2
        return Math.min (y1, y2);
    }
    public int getWidth () {
        return Math.abs (x1 - x2);
    }
    public int getHeight () {
        return Math.abs (y1 - y2);
    }
    // draw the shape using calculated values
    public void draw (Graphics g) {
        g.setColor (myColor);

        // draw the right type of shape
        if (fill)
            g.fillRect(getUpperLeftX (), getUpperLeftY (),
                                                getWidth (), getHeight ());
        else
            g.drawRect (getUpperLeftX (), getUpperLeftY (),
                                                getWidth (), getHeight ());
    }  // end method draw

}  // end class MyRectangle

TestDriver

// TestDraw.java.
// Author: Mike Qualls


package lab4;

import javax.swing.*;

public class TestDraw {

    public static void main(String[] args) {
        // declare local variables/objects, using constants for the window dimensions
        final int WINDOW_WIDTH = 600, WINDOW_HEIGHT = 250;
        DrawPanel panel = new DrawPanel (); // call constructor creating MyLine objects

        JFrame application = new JFrame (); // the window and its components

        application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        application.add (panel);
        application.setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
        application.setVisible (true);      // show the window

    }  // end method main

}  // end class TestDraw




Aucun commentaire:

Enregistrer un commentaire