steveo28's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
New Members
Active Posts:
2 (0 per day)
Joined:
07-October 12
Profile Views:
59
Last Active:
User is offline Oct 13 2012 10:22 AM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   steveo28 has not set their status

Posts I've Made

  1. In Topic: Abstract class. Drawing shapes randomly in JFrame.

    Posted 7 Oct 2012

    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.
  2. In Topic: Abstract class. Drawing shapes randomly in JFrame.

    Posted 7 Oct 2012

    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?

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

steveo28 hasn't added any friends yet.

Comments

steveo28 has no profile comments yet. Why not say hello?