Question

Design a Program “Shape Factory” that has Buttons to produce: a. Random Rectangles with random colors....

Design a Program “Shape Factory” that has Buttons to produce: a. Random Rectangles with random colors. b. Random Ellipses with random colors. c. Random Lines with random colors. Whenever user clicks on a button, the corresponding shape will be added to the already drawn shapes in the Canvas

(java)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.awt.event.*;
import javax.swing.*;

public class ShapeFactory extends JFrame implements ActionListener{

public static void main(String[] args) {
new ShapeFactory();
}
public int numberOfRects;
public int numberOfEllis;
public int numberOfLines;
JButton ranRects = new JButton("Random Rectangles");
JButton ranElli = new JButton("Random Ellipses");
JButton ranLines = new JButton("Random Lines");
Graphics g;
Color c;
ArrayList shapeList = new ArrayList( );

public ShapeFactory(){
super("Shape Factory");
setLayout(new FlowLayout());
setSize(600,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

add(ranRects);
add(ranElli);
add(ranLines);

JPanel nPanel = new JPanel (new GridLayout(1,3));
nPanel.add(ranRects);
nPanel.add(ranElli);
nPanel.add(ranLines);

add(nPanel);


ranRects.addActionListener(this);

ranElli.addActionListener(this);

ranLines.addActionListener(this);


setVisible(true);
}

public void paint (Graphics g) {

super.paint(g);

super.paint(g);
for (int i = 0; i < this.numberOfRects; i++)
paintRect(g);
for (int i = 0; i < this.numberOfEllis; i++)
paintOval(g);
for (int i = 0; i < this.numberOfLines; i++)
this.paintLine(g);
}

public void paintRect(Graphics g) {

int rectX1 = (int)(Math.random() * getWidth() / 4.0);
int rectY1 = (int)(Math.random() * getHeight()/ 4.0);
int rectX2 = (int)(Math.random() * getWidth());
int rectY2 = (int)(Math.random() * getHeight());
c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
g.setColor(c);
g.fillRect(rectX1,rectY2,rectX2,rectY2);
}
public void paintOval(Graphics g) {

int ciX1 = (int)(Math.random() * getWidth() / 4.0);
int ciY1 = (int)(Math.random() * getHeight()/ 4.0);
int ciX2 = (int)(Math.random() * getWidth());
int ciY2 = (int)(Math.random() * getHeight());
c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
g.setColor(c);
g.fillOval(ciX1,ciY1,ciX2,ciY2);
}
public void paintLine(Graphics g) {

int lineX1 = (int)(Math.random() * getWidth() / 4.0);
int lineY1 = (int)(Math.random() * getHeight()/ 4.0);
int lineX2 = (int)(Math.random() * getWidth());
int lineY2 = (int)(Math.random() * getHeight());
c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
g.setColor(c);
g.drawLine(lineX1,lineY1,lineX2,lineY2);
}

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == ranRects) {
this.numberOfRects++;
} else if (source == ranElli) {
this.numberOfEllis++;
} else if (source == ranLines) {
this.numberOfLines++;
}
repaint();
}

}

Add a comment
Know the answer?
Add Answer to:
Design a Program “Shape Factory” that has Buttons to produce: a. Random Rectangles with random colors....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • GUI bouncing shapes program use this interface the out put of the program should be a jFr...

    GUI bouncing shapes program use this interface the out put of the program should be a jFrame with three buttons: “add triangle”, “add square” and “add circle” and a blank screen. when the user clicks any of these buttons the corresponding shape appears and starts bouncing around the screen. the shape of these shaoes and randomized and anytime the user clicks the button you should be able to keeping adding shapes on the screen while the previosus shape is bouncing...

  • Attached is an incomplete Python template file. button_get4_Q.py Download this file and complete it to solve...

    Attached is an incomplete Python template file. button_get4_Q.py Download this file and complete it to solve the following problem. Consider the GUI below (1 Text area, 5 number Buttons and a "RESET" Button) which has been produced with a Python program using the Tkinter library. Get 4 - 0 X Il 0 X Get 4 - 3542 RESET RESET Initial GUI GUI after interaction You are required to write a Python program to produce this interactive GUI. Your GUI must...

  • Hello I just need the code for this GUI thanks Also computer screenshot of the code...

    Hello I just need the code for this GUI thanks Also computer screenshot of the code will be helpful Attached is an incomplete Python template file. button_multi_colour_changer_Q.py Download this file and complete it to solve the following problem. Consider the GUI below (4 Labels, 4 colour Buttons and a "RESET" Button) which has been produced with a Python program using the Tkinter library. MULTI Button Colour - a X MULTI Button Colour - O X Violet Purple Blue | Green...

  • ##JAVA## about a GUI program (see screenshot below) that permits the user to create, save, and load polygons that are displayed (both hollow and filled) in a GUI. The features and operations required...

    ##JAVA## about a GUI program (see screenshot below) that permits the user to create, save, and load polygons that are displayed (both hollow and filled) in a GUI. The features and operations required by the GUI program include: 1. The program starts with an empty (no vertices) polygon set with a randomly generated color. The drawing area is set to be initially square and should have a black background. ------------------------------------------------------------------------------------------------ 2. The left mouse button lets you begin building a...

  • JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to...

    JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...

  • Task: Write a Java program to implement a simple graph editor that can be used to...

    Task: Write a Java program to implement a simple graph editor that can be used to draw a circumcircle. The editor has a pull-down menu on top of the screen with 2 buttons: "Circumcircle" and "Quit". 1) When the user selects "Circumcircle", he/she can draw a circumcircle on the screen as described in Exercise 2.6 on page 59 of the textbook. 2) The editor terminates/quits execution if the user selects "Quit" from the pull- down menu. 3) The editor has...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow...

    import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...

  • import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow...

    import javax.swing.*; import java.awt.*; import java.util.List; import java.util.*; /** * Canvas is a class to allow for simple graphical drawing on a canvas. * This is a modification of the general purpose Canvas, specially made for * the BlueJ "shapes" example. * * @author: Bruce Quig * @author: Michael Kolling (mik) * Minor changes to canvas dimensions by William Smith 6/4/2012 * * @version: 1.6 (shapes) */ public class Canvas { // Note: The implementation of this class (specifically the...

  • Design and implement a C version of the color match program. As a starting point, use...

    Design and implement a C version of the color match program. As a starting point, use the file HW2-1-shell.c. The program should employ a reasonable algorithm that compares all pairings of colors in the palette exactly once. A color should not be compared to itself. Nor should two colors be compared to each other more than once. Your C program should print out the total component color difference for the closest pair using the print string provided in the shell...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT