Question: PLEASE READ CAREFULLY Write a Java GUI program that will display three buttons which will draw three shapes in a random color when you click

PLEASE READ CAREFULLY

Write a Java GUI program that will display three buttons which will draw three shapes in a random color when you click these buttons.

The program templates are provide as the following. This template does not have MouseListener implemented yet. Thus it will not draw anything. To test it out, you can assign a value 1-3 to the shapeCode in line 14, you should be able to see one drawing. Your job is to fill in all the missing spots in the code.

I NEED THE PARTS THATS COMMENTED ON MY PROGRAM BELOW TO BE IMPLEMENTED NOT SURE HOW TO DO IT

HERES THE CODE:

/*

*/

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

public class TestButtonClicked {

public static void main (String[] args) { JFrame frame = new JFrame ("Draw Triangle"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new ButtonClickedPanel()); frame.pack(); frame.setVisible(true); } }

AND THE SECOND PART THAT NEEDS CODE IMPLEMENTATION:

//This panel has a button and when you clicked on //the button, it will draw a triangle on the panel //below it

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

public class ButtonClickedPanelTemplate extends JPanel { private boolean clicked = false; private JButton b1, b2, b3; private JPanel p1; private ShapPanel p2; private int shapeCode; private final int TRIANGLE = 1; private final int CIRCLE = 2; private final int RECTANGLE = 3; private final int SCREEN_X = 350; private final int SCREEN_Y = 375; public ButtonClickedPanelTemplate() { b1 = new JButton("Triangle"); b2 = new JButton("Circle"); b3 = new JButton("Rectangle"); DrawShape ds = new DrawShape(); /* add mouse listener to each button here */ /* ......................................*/ p1 = new JPanel(); p2 = new ShapPanel(); this.add(p1); this.add(p2); p1.add(b1); p1.add(b2); p1.add(b3); setBackground (Color.yellow); setPreferredSize (new Dimension(SCREEN_X, SCREEN_Y)); } class DrawShape implements ActionListener { public void actionPerformed (ActionEvent event) { /* implement actionPerform method here */ /* ...................................*/ } } class ShapPanel extends JPanel { int[] xPtArray = {75, SCREEN_X/2, SCREEN_X-75, 75}; int[] yPtArray = {SCREEN_Y-150, 50, SCREEN_Y-150, SCREEN_Y-150}; private Random rd = new Random(); private int r, g, b; public ShapPanel() { setBackground (Color.white); setPreferredSize(new Dimension(350, 300)); } protected void paintComponenet(Graphics page) { super.paintComponent (page); switch(shapeCode) { case TRIANGLE: { page.setColor(Color.RED); /*Change the above line using random color with the alpha value equla to 150 */ /*..........................................................................*/ page.fillPolygon(xPtArray, yPtArray, xPtArray.length); break; } case CIRCLE: { page.setColor(Color.BLUE); /*Change the above line using random color with the alpha value equla to 150 */ /*..........................................................................*/ page.fillOval(75,50, 200, 200); break; } case RECTANGLE: { page.setColor(Color.ORANGE); /*Change the above line using random color with the alpha value equla to 150 */ /*..........................................................................*/ page.fillRect(75,50, 200, 200); break; } default; { break; } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!