Abstract class. Drawing shapes randomly in JFrame.

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 - 07:50 PM

Hey not sure exactly what I did but I got it to work, here's my main class so you can take a look:

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() {
        // TODO: Create an array of 100 Shape elements.
        // TODO: insert code here

        shapes = new Shape[100];
        for (int i = 0; i < 100; i++) {
            Color color = new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256));
            int typeOfObject = (Math.random() < 0.5) ? 0 : 1;
            int isFilledNum = (Math.random() < 0.5) ? 0 : 1;
            boolean isFilled;
            if (isFilledNum == 0) {
                isFilled = false;
            } else {
                isFilled = true;
            }

            
            if (typeOfObject == 0) {
                shapes[i] = new Rectangle((int) (Math.random() * 800),
                        (int) (Math.random() * 600),
                        (int) (Math.random() * 100),
                        (int) (Math.random() * 100),
                        color,
                        isFilled);
            } else {
                shapes[i] = new Circle((int) (Math.random() * 800),
                        (int) (Math.random() * 600),
                        color,
                        isFilled,
                        (int) (Math.random() * 50));
            }
        }
        setTitle("Abstract Class Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setVisible(true);
      

    } // 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



Make sure you put parenthesis like this on your typecasted randoms: (int) (Math.random() * 800), otherwise it rounds the random down to 0 before you even multiply.
Was This Post Helpful? 0
  • +
  • -

#17 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1407
  • View blog
  • Posts: 3,123
  • Joined: 05-April 11

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 08 October 2012 - 04:58 AM

adonaldson it is pretty hard to help you when you dont post your updated code
Was This Post Helpful? 0
  • +
  • -

#18 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 08 October 2012 - 04:13 PM

View Poststeveo28, on 07 October 2012 - 07:50 PM, said:

Hey not sure exactly what I did but I got it to work, here's my main class so you can take a look:

Make sure you put parenthesis like this on your typecasted randoms: (int) (Math.random() * 800), otherwise it rounds the random down to 0 before you even multiply.


I went in threw in the parathesis and it works. Too bad I didn't read this before I turned it in. Thank you so much though. I knew it had to be trivial.

View PostCasiOo, on 08 October 2012 - 04:58 AM, said:

adonaldson it is pretty hard to help you when you dont post your updated code


Thank you so much for you assistance, you were great. Finially have it working.
Was This Post Helpful? 0
  • +
  • -

#19 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Abstract class. Drawing shapes randomly in JFrame.

Posted 08 October 2012 - 08:08 PM

View Postadonaldson, on 07 October 2012 - 05: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.

Sure !!! If the compilation generates errors how can you expect results from running the program... You completly missed a concept
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2