Abstract class. Drawing shapes randomly in JFrame.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2531 Views - Last Post: 08 October 2012 - 08:08 PM Rate Topic: -----

#1 adonaldson  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 07-October 12

Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 01:58 PM

I need to draw a random selection of shapes in a JFrame. Either a Circle or a Rectangle. Each field is randomly generated for each. I can not figure out why it is not running. Can someone help me. This is my program.
Main:
package p04abstractclasses;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;

public class Main extends JFrame {
    private Shape[] shapes;         // the array of shapes to be randomly filled
    
    public static void main(String[] args) {
        new Main();
    } // end main()
    
    public Main() {
        setTitle("Abstract Class Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setVisible(true);
        
        // TODO: Create an array of 100 Shape elements.
        // TODO: insert code here
       
        /* Randomly generate each of the geometric parameters.
         * Following these guidelines for each:
         * 
         * x, y origin:
         * 0 <= x < 800
         * 0 <= y < 600
         * 
         * bounding box's width and height:
         * 0 <= width < 100
         * 0 <= height < 100
         * 
         * Circle's radius:
         * 0 <= r < 50
         * 
         * Object's color:
         * 0 <= red < 1.0
         * 0 <= green < 1.0
         * 0 <= blue < 1.0
         * 
         * Object's filled state:
         * 0 <= filled <= 1
         * 
         * Type of object:
         * 0: Rectangle
         * 1 : Circle
         */
        // TODO: insert code here.
         for(int i= 0; i<100; i++){
         int r = (int)Math.random() * (50);
            //Random Object color
            float red = (float) (Math.random() * (1.0));
            float green = (float) (Math.random() * (1.0));
            float blue = (float) (Math.random() * (1.0));
            Color color = new Color(red, green, blue);
            //Random fill
            int fill = (int)Math.random() * (2);
            boolean filled = false;
            if(fill==0){
                 filled = false;
            }else if(fill==1){
                 filled = true;
            }//end if-else-if
            //Random type of Object
            int type = (int)Math.random() * (2);
            
            if(type==0){    
                shapes[i] = new Rectangle( ((int)Math.random() * 800),
                                           ((int)Math.random() * 600),
                                           ((int)Math.random() * 100),
                                           ((int)Math.random() * 100),
                                           (color),
                                           (filled));
            }else if(type==1){
                shapes[i] = new Circle( ((int)Math.random() * 800),
                                        ((int)Math.random() * 600),
                                        (color),
                                        (filled),
                                        ((int)Math.random() * 50));
            }//end if-else-if            
        }//end for
    } // end Main()
    
    @Override
    public void paint(Graphics g) {
        // Uses a fast enumeration to draw out all the shapes.
        for (Object s : shapes) {
            ((Shape)s).draw(g);
        }
    } // end paint()
} // end Main



Shape class, abstract super class:
package p04abstractclasses;

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


public abstract class Shape implements Cloneable{
    private int x;
    private int y;
    private Color color;
    private boolean filled;
    
    protected Shape(int x, int y, Color color, boolean filled){
        set(x, y, color, filled);
    }//end Shape(i,i,C,B)/>, designated constructor
    
    private void set(int x, int y, Color color, boolean filled){
        this.x = x;
        this.y = y;
        this.color = color;
        this.filled = filled;
    }//end designated setter

    //Getters & Setters
    public int getX() {
        return x;
    }//end getX()

    public void setX(int x) {
        set(x, getY(), getColor(),isFilled());
    }//end setX(i)

    public int getY() {
        return y;
    }//end getY()

    public void setY(int y) {
        set(getX(), y, getColor(), isFilled());
    }//end setY(i)

    public Color getColor() {
        return color;
    }//end getColor()

    public void setColor(Color color) {
        set(getX(), getY(), color, isFilled());
    }//end setColor(C)

    public boolean isFilled() {
        return filled;
    }//end isFilled()

    public void setFilled(boolean filled) {
        set(getX(), getY(), getColor(), filled);
    }//end setFilled(B)/>
    
    public abstract void draw(Graphics g);
    //end draw(G)   
}//end Shape class



Circle class:
package p04abstractclasses;

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

public class Circle extends Shape {
    public int radius;

    public Circle(int x, int y, Color color, boolean filled, int radius) {
        super(x, y, color, filled);
        set(radius);
    }//end designated constructor
    
    private void set(int radius){
        this.radius = radius;
    }//end designated setter

    //Getter & Setter
    public int getRadius() {
        return radius;
    }//end getRadius()

    public void setRadius(int radius) {
        set(radius);
    }//end setRadius(i)

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }//end clone()
    
    @Override
    public void draw(Graphics g){
        if(super.isFilled()==true){
            g.fillOval(getX(), getY(), radius*2, radius*2);
        } else if(super.isFilled()==false){
            g.drawOval(getX(), getY(), radius*2, radius*2);
        }//end if-else-if
    }//end draw(G)  
}//end Circle class



Rectangle class:
package p04abstractclasses;

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

public class Rectangle extends Shape{
    private int width;
    private int height;

    public Rectangle(int x, int y, int width, int height, Color color, boolean filled) {
        super(x, y, color, filled);
        set(width, height);
    }//end designated constructor
    
    private void set(int width, int height){
        this.width = width;
        this.height = height;
    }//end designated setter

    //Getters & Setters
    public int getWidth() {
        return width;
    }//end getWidth()

    public void setWidth(int width) {
        set(width, getHeight());
    }//end setWidth(i)

    public int getHeight() {
        return height;
    }//end getHeight()

    public void setHeight(int height) {
        set(getWidth(), height);
    }//end setHeight(i)

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }//end clone()
    
    @Override
    public void draw(Graphics g){        
        if(super.isFilled()==true){
            g.fillRect(getX(), getY(), width, height);
        }else if(super.isFilled()==false){
            g.drawRect(getX(), getY(), width, height);
        }//end if-else-if
    }//end draw(G)  
}//end Rectangle class



I'm pretty sure I'm not doing something right in Main. Please help! This abstract class stuff is confusing me.

Is This A Good Question/Topic? 0
  • +

Replies To: Abstract class. Drawing shapes randomly in JFrame.

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 02:07 PM

What is the error/problem you are having?
Was This Post Helpful? 0
  • +
  • -

#3 adonaldson  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 07-October 12

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 02:28 PM

It is not compiling at all. It's throwing lots of errors. and the JFrame comes up blank. I did everything that was instructed but obviously have missed something.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 02:35 PM

View Postadonaldson, on 07 October 2012 - 09:28 PM, said:

It is not compiling at all. It's throwing lots of errors. and the JFrame comes up blank. I did everything that was instructed but obviously have missed something.


So post the errors in code tags :)
Was This Post Helpful? 0
  • +
  • -

#5 adonaldson  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 07-October 12

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 02:38 PM

Sorry here they are.
run:
Exception in thread "main" java.lang.NullPointerException
	at p04abstractclasses.Main.<init>(Main.java:75)
	at p04abstractclasses.Main.main(Main.java:16)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at p04abstractclasses.Main.paint(Main.java:94)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
	at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:711)
	at java.awt.EventQueue.access$000(EventQueue.java:104)
	at java.awt.EventQueue$3.run(EventQueue.java:672)
	at java.awt.EventQueue$3.run(EventQueue.java:670)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:681)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)


Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 02:40 PM

You forgot to initialize your array
Make room for 100 shapes in your array :)

shapes = new Shape[100];


Was This Post Helpful? 0
  • +
  • -

#7 adonaldson  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 07-October 12

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 02:45 PM

Well that's part of the code that the professor already wrote, so I thought I should of left it alone. These are the new errors if I do this.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at p04abstractclasses.Main.paint(Main.java:95)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
	at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:711)
	at java.awt.EventQueue.access$000(EventQueue.java:104)
	at java.awt.EventQueue$3.run(EventQueue.java:672)
	at java.awt.EventQueue$3.run(EventQueue.java:670)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:681)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)


Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 03:08 PM

Is it this line you are getting the nullpointer?
((Shape)s).draw(g);



If it is, then you didn't initialize every index of your shapes array.

I think you could make your code cleaner if you used the Random class to generate random numbers :)
Was This Post Helpful? 0
  • +
  • -

#9 adonaldson  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 07-October 12

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 03:16 PM

Yes that's the one. I'll check but I thought that the for loop would get all of them. We are given certain ranges of numbers to generate a random number from, and that's the only way I could think of to do it.
Was This Post Helpful? 0
  • +
  • -

#10 adonaldson  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 07-October 12

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 03:49 PM

Ok so I changed the for loop to a while loop going the length of the array to be sure that I got every index of the array. And I am still getting the same errors. :angry: so frustrating!
Was This Post Helpful? 0
  • +
  • -

#11 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 04:02 PM

int type = (int)Math.random() * (2); 


It will always give you 0 since Math.random() returns a value between 0.0 (inclusive) - 1 (exclusive), so when you cast it to an int it will always be 0.
0 * 2 = 0 :)
Was This Post Helpful? 0
  • +
  • -

#12 adonaldson  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 07-October 12

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 04:16 PM

ok I changed that to:
int type = (Math.random()<0.5)?0:1;



but am still getting the errors. Plus even it it did give me a 0 each time that should of just gave me an array of Rectangles only. Is there any other way to have it paint from the paint method in main. That seems to be where it is erroring at. The teacher wrote that but If I can get it to work by changing his code it's worth a try.
Was This Post Helpful? 0
  • +
  • -

#13 adonaldson  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 07-October 12

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 04:41 PM

This file has a picture of what the result are suppose to look like, and the uml. If it helps at all.
Was This Post Helpful? 0
  • +
  • -

#14 steveo28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-October 12

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 06:11 PM

Dude I think I'm in your class and I'm getting the exact same problem you are, our code looks pretty similar, you figure it out?
Was This Post Helpful? 0
  • +
  • -

#15 adonaldson  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 07-October 12

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 07 October 2012 - 07:05 PM

Nope. I made a few minor syntax changes. But it still isn't working. It says that Socratis's code is messing up in the paint method in Main. I just don't know. I can't see anything wrong with my logic.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2