I want to make new JComboBox to allow the people to choose colors the shape to be drawn in
How can i apply the colors in "public void actionPerformed(ActionEvent e) {" to make my shape change colors? Thank you
How can i apply the colors in "public void actionPerformed(ActionEvent e) {" to make my shape change colors? Thank you
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Class definition
public class Shapes extends JPanel implements ActionListener {
private JButton moveLeft, moveRight, makeBigger, makeSmaller;
private JComboBox shapeChoice, colorChoice;
private Point firstPoint;
private Circle firstCircle;
private Square firstSquare;
private Doughnut firstDoughnut;
private Triangle firstTriangle;
private Rectangle firstRectangle;
private Point aShape;
private Color col;
private Graphics g;
private JSlider slider;
private int value = 1;
// Constructor method declaration
moveLeft = new JButton("Move Left");
add(moveLeft);
moveLeft.addActionListener(this);
moveRight = new JButton("Move Right");
add(moveRight);
moveRight.addActionListener(this);
// makeBigger = new JButton("Make Bigger");
//add(makeBigger);
// makeBigger.addActionListener(this);
// makeSmaller = new JButton("Make Smaller");
//add(makeSmaller);
// makeSmaller.addActionListener(this);
shapeChoice = new JComboBox();
shapeChoice.addItem("Point");
shapeChoice.addItem("Square");
shapeChoice.addItem("Circle");
shapeChoice.addItem("Doughnut");
shapeChoice.addItem("Triangle");
shapeChoice.addItem("Rectangle");
add(shapeChoice);
shapeChoice.addActionListener(this);
colorChoice = new JComboBox();
colorChoice.addItem("Blue");
colorChoice.addItem("Green");
colorChoice.addItem("Red");
colorChoice.addItem("Yellow");
colorChoice.addItem("Orange");
colorChoice.addItem("Purple");
colorChoice.addItem("Black");
add(colorChoice);
colorChoice.addActionListener(this);
// Instantiate shape classes
firstPoint = new Point();
firstCircle = new Circle();
firstSquare = new Square();
firstDoughnut = new Doughnut();
firstRectangle = new Rectangle();
firstTriangle = new Triangle();
// Initially assign aShape a Point object
aShape = firstPoint;
slider = new JSlider(JSlider.HORIZONTAL, minimumValue, maximumValue, initValue);
add(slider);
// Create a new window using the Swing class JFrame and add this panel
makeFrame();
}
public void actionPerformed(ActionEvent e) {
// Manipulate respective object when buttons pressed
if (e.getSource() == moveLeft) {
aShape.moveXY(-20, 0);
}
else if (e.getSource() == moveRight) {
aShape.moveXY(20, 0);
}
else if (e.getSource() == makeBigger) {
aShape.reSize(20);
}
else if (e.getSource() == makeSmaller) {
aShape.reSize(-20);
}
// checkbox assigns object to aShape
else if (e.getSource() == shapeChoice) {
if (shapeChoice.getSelectedItem().equals("Circle"))
aShape = firstCircle;
else if (shapeChoice.getSelectedItem().equals("Square"))
aShape = firstSquare;
else if (shapeChoice.getSelectedItem().equals("Point"))
aShape = firstPoint;
else if (shapeChoice.getSelectedItem().equals("Doughnut"))
aShape = firstDoughnut;
else if (shapeChoice.getSelectedItem().equals("Triangle"))
aShape = firstTriangle;
else if (shapeChoice.getSelectedItem().equals("Rectangle"))
aShape = firstRectangle;
}
else if (e.getSource() == value) {
}
{ if (slider.getValue() >= 20) aShape.reSize(20);
if (slider.getValue() >= 30) aShape.reSize(20);
aShape.reSize(20);
if (slider.getValue() >= 40) aShape.reSize(20);
aShape.reSize(20);
if (slider.getValue() >= 50) aShape.reSize(20);
aShape.reSize(20);
if (slider.getValue() < 60)
aShape.reSize(20);
}
{
if (e.getSource() == colorChoice)
{
// Ask Java to repaint the window to reflect the updates made
repaint();
}
}
}
// Provide this method to instruct Java what to do when repainting the window
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Call the respective methods from respective class to print the new co-ordinate values
aShape.outputXYCoords(g);
g.setColor(Color.blue);
aShape.display(g);
add(slider, BorderLayout.SOUTH);
}
// Call the respective methods from respective class to redraw the shape
// Create a window frame
public void makeFrame() {
// Instantiate a window frame using Swing class JFrame
JFrame frame = new JFrame("Shapes");
// When the window is closed terminate the application
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// set initial size of window
frame.setSize(800, 600);
// add the current object to the centre of the frame and make visible
frame.getContentPane().add(this, BorderLayout.CENTER);
frame.setVisible(true);
}
}