Question
Java Write a Java Swing program that displays a regular polygon and uses two buttons named +1 and -1 to increase or decrease the number
Java
Write a Java Swing program that displays a regular polygon and uses two buttons named +1 and -1 to increase or decrease the number of sides of the polygon being displayed, as shown in the following figure. Set the minimum number of sides to 3 as shown. You should also implement a menu bar as shown that will set colors to yellow, red and blue. Also set a menu option to Auto(Start and Stop) which will use the timer class in Swing library (javax.swing.Timer)to randomly change the colors of the polygon every second.
------------------------------------------------------------------------------------------------
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Polygon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities;
//Class public class OurPolygon extends JFrame { //Driver public static void main(String[] args) { //Swing utility SwingUtilities.invokeLater(new Runnable() { //Method run() public void run() { //Create new frame OurPolygon ourFrame = new OurPolygon(); //Set frame visible ourFrame.setVisible(true); } }); }
//Constructor public OurPolygon() { //Title super("PolygonSides"); //Layout setLayout(new BorderLayout()); //Add panel add(OurPolygonPanel()); //Set size setSize(400, 420); //Set location setLocationRelativeTo(null); //Set default close setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } //Method for OurPolygonPanel() private JPanel OurPolygonPanel() { //Create new instance for polygon box JPanel ourPolyBox = new JPanel(new BorderLayout()); //Create new instance for polygon panel ourPanel = new PolygonPanel(); //Create new instance for buttons JPanel ourButtons = new JPanel(new GridLayout(1, 2)); //Increment button incButton = new JButton("+1"); //Action listioner for +1 incButton.addActionListener(new Enlarger()); //Decrement button decButton = new JButton("-1"); //Action listioner for -1 decButton.addActionListener(new Shrinker()); //Add +1 button ourButtons.add(incButton); //Add -1 button ourButtons.add(decButton); //Add panel to box ourPolyBox.add(ourPanel, BorderLayout.CENTER); //Add buttons to box ourPolyBox.add(ourButtons, BorderLayout.SOUTH); //Return return ourPolyBox; }
//Class class Enlarger implements ActionListener { @Override //Enlarge operation public void actionPerformed(ActionEvent e) { //Function call ourPanel.pEnlarge(); } } //Class class Shrinker implements ActionListener { @Override //Shrink operation public void actionPerformed(ActionEvent e) { //Function call ourPanel.pShrink(); } }
//Variables required private JButton incButton; private JButton decButton; private PolygonPanel ourPanel; }
//Class class PolygonPanel extends JPanel { //Variable required private int pSides = 3; //Method pEnlarge() public void pEnlarge() { //Increment pSides++; //Function call repaint(); }
//Method pShrink() public void pShrink() { //Check condition if (pSides > 3) //Decrement pSides--; //Function call repaint(); }
@Override //Method paintComponent() protected void paintComponent(Graphics g) { //Function call super.paintComponent(g); //Variable required int r = (int) (Math.min(getWidth(), getHeight()) * 0.4); int xC = getWidth() / 2; int yC = getHeight() / 2; //Set color g.setColor(Color.YELLOW); //Create instance Polygon po = new Polygon(); //Function call po.addPoint(xC + r, yC); //Function call po.addPoint((int)(xC+r*Math.cos(2*Math.PI/pSides)),(int)(yC-r*Math.sin(2*Math.PI/pSides))); //Loop for (int pp = 2; pp <= pSides; pp++) { //Function call po.addPoint((int) (xC + r * Math.cos(pp * 2 * Math.PI / pSides)), (int) (yC - r * Math.sin(pp * 2 * Math.PI / pSides))); } //Function call g.fillPolygon(po); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started