The Java program should have 3 buttons that say Triangle, Rectangle, and Circle
ID: 3872588 • Letter: T
Question
The Java program should have 3 buttons that say Triangle, Rectangle, and Circle on them, respectively. The buttons are used to select the shape that will be drawn, and after one of the buttons has been clicked, a label should display which shape has been selected. One shape should be selected by default.
There should be three labels, Red, Green, and Blue, each followed by a textfield, which has 0 as default value. There should also be another button named "Draw", which when clicked will draw the selected shape in the selected color. If an invalid value (anything other than an integer between 0 and 255) is entered in any of the textfields, a black shape is drawn instead. There should be a final button named "Change color", which changes the color of the shape to a random color and the text in the text fields to display the RGB values of that color.
Explanation / Answer
import java.awt.*;
import javax.print.attribute.TextSyntax;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class ShapesColors
{
JFrame fram2;
String shapeClicked="";
JFrame frame=new JFrame("");
//Constructing the Frame
JPanel panel=new JPanel();
JPanel panel2=new JPanel();
JPanel panel4=new JPanel();
//Constructing the Panels
JLabel label1=new JLabel("Red");
JLabel label2=new JLabel("Green");
JLabel label3=new JLabel("Blue");
JLabel label4=new JLabel("");
JLabel label5=new JLabel("");
//Constructing the Labels
Graphics2D g;
JButton button1=new JButton("Rectangle");
JButton button2=new JButton("Triangle");
JButton button3=new JButton("Circle");
JButton button4=new JButton("Draw");
JButton button5=new JButton("Change Color");
//Constructing the Buttons
JTextField textField1=new JTextField();
JTextField textField2=new JTextField();
JTextField textField3=new JTextField();
//Constructing TextField
public void Gui()
{
frame.setVisible(true);
frame.setSize(800,800);
textField1.setColumns(10);
textField2.setColumns(10);
textField3.setColumns(10);
button1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
label4.setText("You clicked Rectangle");
shapeClicked="RECTANGLE";
}
});
button2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
label4.setText("You clicked Trigle");
shapeClicked="TRIANGLE";
}
});
button3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
label4.setText("You clicked Circle");
shapeClicked="CIRCLE";
}
});
button4.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
if(fram2!=null)
fram2.dispose();
int red;
int green;
int blue;
try
{
red=Integer.parseInt(textField1.getText());
green=Integer.parseInt(textField2.getText());
blue=Integer.parseInt(textField3.getText());
}
catch(Exception er)
{
red=0;
green=0;
blue=0;
}
drawShape(red,green,blue);
}
});
button5.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
if(fram2!=null)
fram2.dispose();
Random rand=new Random();
int red=rand.nextInt(255);
int green=rand.nextInt(255);
int blue=rand.nextInt(255);
drawShape(red, green, blue);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.CENTER);
frame.add(panel4,BorderLayout.SOUTH);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel2.add(label1);
panel2.add(textField1);
panel2.add(label2);
panel2.add(textField2);
panel2.add(label3);
panel2.add(textField3);
panel2.add(label4);
panel4.add(label5);
}
public static void main(String[]args){
ShapesColors SC=new ShapesColors();
//Constructs the ShapesColors Class
}
public ShapesColors(){
Gui();
//Calls the Gui.
}
public void drawShape(int red,int green,int blue)
{
if(shapeClicked==null || shapeClicked=="")
{
label5.setText("click some button for shape");
return;
}
Shapes shape=new Shapes(shapeClicked,red,blue,green);
fram2=new JFrame();
fram2.add(shape);
fram2.setSize(150, 150);
fram2.setVisible(true);
}
}
New class Shape:
package windowShape;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Shapes extends Canvas
{
String shapeName="";
Color color=Color.RED;
public Shapes() {
}
public Shapes(String shapeName,int red,int blue,int green)
{
this.shapeName =shapeName;
if((red>255 || red<0) && (blue>255 || blue<0) && (green>255 || green<0))
{
color=new Color(0, 0, 0);
}
else
{
color=new Color(red, green, blue);
}
}
public void paint(Graphics g)
{
if(shapeName.equals("CIRCLE"))
{
setForeground(color);
g.fillOval(0,0,100, 100);
}
else if(shapeName.equals("RECTANGLE"))
{
setForeground(color);
g.fillRect(0, 0, 150, 100);
}
else if(shapeName.equals("TRIANGLE"))
{
setForeground(color);
int []nXpts = {75,35,110};
int []nYpts = {130,20,20};
g.fillPolygon(nXpts,nYpts,3);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.