Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write code that draws a Triangle, a Pentagon, and a Hexagon based on what the us

ID: 3815024 • Letter: W

Question

Write code that draws a Triangle, a Pentagon, and a Hexagon based on what the user has selected in the list. Further, you should implement a Timer (note – this is covered in this week’s second lecture, so you will have a bit more work this time than normal!) to randomly change the color of the shape every three seconds. This requires the code that determines what shape is drawn when to be on a Timer. The GUI size and shape size are up to you. Add a JList to the side of the GUI, with a JScrollBar, letting the user choose the color used. The JList should only let 2 options be seen at a time. Offer five color choices – the exact colors are up to you. Until a color is selected in the JList, the GUI should use the color black. Screenshots are provided, but are likely of limited usefulness due to not being able to show the time transition

http://www.mathopenref.com/coordpolycalc.html may prove useful for getting the coordinates you want for shape drawing.

Random Shapes Red A Blue

Explanation / Answer

Hi,

Please see below the class to draw. Please comment for any queries or fedbacks.

Thanks,

Draw.java

import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Draw extends JPanel {
private static int points;
static Container contentPane ;
static Polygon p ;

//to paint the polygon
public void paintComponent(Graphics g) {
super.paintComponent(g);
p = new Polygon();
for (int i = 0; i < points; i++) p.addPoint((int) (
100 + 50 * Math.cos(i * 2 * Math.PI / points)),(int) (100 + 50 * Math.sin(
i * 2 * Math.PI / points)));
g.drawPolygon(p);
  
}
public static void main(String[] args) {
   Scanner scan = new Scanner(System.in);
   String shape="";
   //Reading the Shape from the user
   System.out.println("Please select 1.Traiangle, 2.Pentagon, 3.Hexagon ");
   shape =scan.nextLine();
   //Setting the vertices
   if("1".equalsIgnoreCase(shape)){
       points = 3;
   }
   else if("2".equalsIgnoreCase(shape)){
       points = 5;
   }
   else if("3".equalsIgnoreCase(shape)){
       points = 6;
   }
JFrame frame = new JFrame();
frame.setTitle("Polygon");
frame.setSize(550, 250);
  
System.out.println("");
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
  
DefaultListModel colors = new DefaultListModel();

colors.addElement("Red");
colors.addElement("Blue");
colors.addElement("Yellow");
colors.addElement("Cyan");
colors.addElement("Green");
  
final JList colorList = new JList(colors);
colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
colorList.setSelectedIndex(0);
colorList.setVisibleRowCount(2); //Setting the visible rowcount as 2
  
JScrollPane colorListScrollPane = new JScrollPane(colorList);
controlPanel.add(colorListScrollPane);
  
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
contentPane = frame.getContentPane();
contentPane.add(new Draw());
frame.add(controlPanel,BorderLayout.NORTH);
frame.setVisible(true);
  
//ActinListener for JList
colorList.addListSelectionListener(new ListSelectionListener()
      
       {
public void valueChanged(ListSelectionEvent evt) {
   if (!evt.getValueIsAdjusting()) {
      
       //Setting teh color based on the color selected
if("Red".equalsIgnoreCase(colorList.getSelectedValue().toString())){
   Graphics g =contentPane.getGraphics();
   g.setColor(Color.RED);
   g.fillPolygon(p);
}
else if("Blue".equalsIgnoreCase(colorList.getSelectedValue().toString())){
   Graphics g =contentPane.getGraphics();
   g.setColor(Color.BLUE);
   g.fillPolygon(p);
  
} if("Yellow".equalsIgnoreCase(colorList.getSelectedValue().toString())){
   Graphics g =contentPane.getGraphics();
   g.setColor(Color.YELLOW);
   g.fillPolygon(p);
} if("Cyan".equalsIgnoreCase(colorList.getSelectedValue().toString())){
   Graphics g =contentPane.getGraphics();
   g.setColor(Color.CYAN);
   g.fillPolygon(p);
} if("Green".equalsIgnoreCase(colorList.getSelectedValue().toString())){
   Graphics g =contentPane.getGraphics();
   g.setColor(Color.GREEN);
   g.fillPolygon(p);
     
}
}
}
});
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote