Results 1 to 13 of 13
  1. #1
    gubata is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Question Java - Randomshape

    Hello
    A task must be completed, but I dont have any idea how to start any help please.

    Write a program that draws a random picture on the screen that consists of different geometric shapes
    with different colours. The number, position, types and colors of the shapes should be random. It is up
    to you what randomness you choose, whether you make the random choices dependent on each other, etc.
    Minimal requirements are:
     a variation of at least 4 different shapes;
     at least 10 shapes in each picture, with variation up to at least 20 (i.e., if 10 is the minimum number
    of shapes in your pictures, there should also be a picture possible with at least 30 shapes);
     variation of at least 20 different colours;
     variation of positions.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java - Randomshape

    What have you tried? Be sure to wrap any code you post in code tags to preserve its formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gubata is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Default Re: Java - Randomshape

    Well it should looke something like this, but 2 more shapes should be added.

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Rectangle2D;
    import java.util.ArrayList;
    import java.util.Random;
    import javax.swing.*;
    
    class ColoredShapes {
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    JPanel gui = new JPanel(new BorderLayout(5,5));
    
                    final ArrayList<Drawable> drawables = new ArrayList<Drawable>();
                    final PaintPanel paintPanel = new PaintPanel(drawables);
                    gui.add(paintPanel, BorderLayout.CENTER);
    
                    final Random r = new Random();
    
                    JToolBar tools = new JToolBar();
                    gui.add(tools, BorderLayout.PAGE_START);
    
                    JButton addCircle = new JButton("Add Circle");
                    ActionListener addCircleListener = new ActionListener() {
    
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            Point p = new Point(r.nextInt(400), r.nextInt(400));
                            int s = r.nextInt(50) + 50;
                            Color c = new Color(
                                    r.nextInt(255), r.nextInt(255), r.nextInt(255));
                            drawables.add(new DrawableCircle(p, s, c));
                            paintPanel.repaint();
                        }
                    };
                    tools.add(addCircle);
                    addCircle.addActionListener(addCircleListener);
    
                    JButton addSquare = new JButton("Add Square");
                    ActionListener addSquareListener = new ActionListener() {
    
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            Point p = new Point(r.nextInt(400), r.nextInt(400));
                            int s = r.nextInt(50) + 50;
                            Color c = new Color(
                                    r.nextInt(255), r.nextInt(255), r.nextInt(255));
                            drawables.add(new DrawableSquare(p, s, c));
                            paintPanel.repaint();
                        }
                    };
                    tools.add(addSquare);
                    addSquare.addActionListener(addSquareListener);
    
                    JOptionPane.showMessageDialog(null, gui);
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // [url=http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html]Initial Threads (The Java™ Tutorials > Creating a GUI With JFC/Swing > Concurrency in Swing)[/url]
            SwingUtilities.invokeLater(r);
        }
    }
    
    interface Drawable {
    
        public abstract void draw(Graphics2D g);
    
        public abstract Color getColor();
    }
    
    class DrawableCircle implements Drawable {
    
        private Color color;
        private Shape shape;
    
        DrawableCircle(Point topLeft, int size, Color color) {
            shape = new Ellipse2D.Double(topLeft.x, topLeft.y, size, size);
            this.color = color;
        }
    
        public void draw(Graphics2D g) {
            g.setColor(getColor());
            g.fill(shape);
        }
    
        public Color getColor() {
            return color;
        }
    }
    
    class DrawableSquare implements Drawable {
    
        private Color color;
        private Shape shape;
    
        DrawableSquare(Point topLeft, int size, Color color) {
            shape = new Rectangle2D.Double(topLeft.x, topLeft.y, size, size);
            this.color = color;
        }
    
        public void draw(Graphics2D g) {
            g.setColor(getColor());
            g.fill(shape);
        }
    
        public Color getColor() {
            return color;
        }
    }
    
    class PaintPanel extends JPanel {
    
        Dimension preferredSize = new Dimension(400,400);
        ArrayList<Drawable> drawables;
    
        PaintPanel(ArrayList<Drawable> drawables) {
            this.drawables = drawables;
        }
    
        public Dimension getPreferredSize() {
            return preferredSize;
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING, 
                    RenderingHints.VALUE_ANTIALIAS_ON);
            for (Drawable drawable : drawables) {
                drawable.draw(g2);
            }
        }
    }
    Last edited by gubata; 10-04-2015 at 07:37 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java - Randomshape

    Please edit your post and wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    gubata is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Default Re: Java - Randomshape

    Done :)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java - Randomshape

    I don't see any comments in the code that says where the shapes are being added.
    2 more shapes should be added.
    I would expect there to be a loop that added the shapes. If you want more shapes, go around the loop a few more times for the extra shapes.

    Can you describe what problems you are having now?
    Last edited by Norm; 10-04-2015 at 08:23 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    gubata is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Default Re: Java - Randomshape

    Well that is an example I downloaded if you can run the program in java you will see there are 2 buttons Add circle and add square and when uou click on one of them they appear on a random place on the background, the thing I want to be made is to put two more buttons for example add triangle and add diamond, which have the same function as the other 2

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java - Randomshape

    I want to be made is to put two more buttons for example add triangle and add diamond
    Ok, you should be able to follow the code for the current buttons and classes to make more buttons and classes that follow that same scheme for the 2 new shapes you want to add.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Java - Randomshape

    you have to add anywhere from 10 to 30 shapes to your PaintPanel (number should be random). These shapes can come from 4 possible shapes (classes, of which you have 2 already: square and circle), which have to be selected randomly. These 10 to 30 shapes have random attributes (location, color).

    So you have to pick a random number between 10 and 30 ("X") and then loop trough x times and each loop creates a randomly selected instance of your 4 classes with random attributes.

    I am not sure whether Buttons are considered geometric shapes here.

    just my 2 cents, 'hope this isn't spoon feeding.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Java - Randomshape

    Quote Originally Posted by benji2505 View Post
    I am not sure whether Buttons are considered geometric shapes here.
    I believe the buttons are used to add a shape. Two buttons, one for adding a circle and one for adding a square.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Java - Randomshape

    Quote Originally Posted by jim829 View Post
    I believe the buttons are used to add a shape. Two buttons, one for adding a circle and one for adding a square.

    Regards,
    Jim
    Fair enough, but the shape selection should be random.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java - Randomshape

    the shape selection should be random
    Would that be started by a user pressing a button: "Create Random shape"?
    The randomness could be effected by using the Random class. If there are 4 shapes to chose from, have the Random class's method generate a number in the range 0 to 3 and associate those numbers with a shape.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Java - Randomshape

    I have a feeling that the Prof that gave out this homework is already happy if there some sort of random selection of shapes. She/He probably does not care what calls the method.

    As the OP said, the code above is only something he/she downloaded, so something that the OP thinks might fit somehow.
    Last edited by benji2505; 10-07-2015 at 02:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •