1

I need to create a program that allows the user to select a color from a list of checkboxes, red and blue, and then a shape from a list of radio buttons, square or circle. When the “Draw” button is pressed the selected shape and color are drawn. If both red and blue are chosen, the shape is drawn in purple.

should look like the following picture:

enter image description here

This is about as far i've gotten, stumped as to how to create the circle and print it when that option is chosen. Also how do I reorganize the labels and buttons?

any help is appreciated

import java.awt.GridBagLayout;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Shapes
{
    public static JFrame window = new JFrame("Shapes");
    public static JPanel panel = new JPanel(new GridBagLayout());

    public static void main(String[] args) 
    {

        window.setBounds(0, 0,300, 300);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.add(panel);
        MApp m = new MApp();
        m.setBounds(100,100,100,100);
        window.add(m);

        Draw d = new Draw(panel) ;
        d.setBounds(0, 0, window.getWidth(), 90);
        window.add(d);

        window.setVisible(true);
    }

}
import java.awt.Color; 
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;

import javax.swing.JPanel;

public class MApp extends JPanel implements MouseListener 
{
    private boolean clicked; 
    private Rectangle r; 
    public MApp()
    {
        clicked = false;
        r = new Rectangle(15, 15, 50, 50); 
        addMouseListener(this);
    }
    public void paintComponent(Graphics g)
    {
        if(clicked)
        {
            g.setColor(Color.BLUE);
        }
        else
        {
            g.setColor(Color.RED);
        } 
        g.fillRect((int)r.getX(), (int)r.getY(),
        (int)r.getWidth(), (int)r.getHeight()); 
    }
    public void mouseClicked (MouseEvent e) 
    {
        Point p = new Point(e.getX(),e.getY()); 
        if(r.contains(p))
        {
            clicked = !clicked; 
        }
        repaint(); 
    }
    public void Circle()
    {
         g.fillOval(0, 0, s, s);
    }
    public void mousePressed (MouseEvent evnt) {}
    public void mouseReleased (MouseEvent evnt) {}
    public void mouseEntered (MouseEvent evnt) {}
    public void mouseExited (MouseEvent evnt) {} 
}

    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.ButtonGroup;
    import javax.swing.GroupLayout;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;

    public class Draw extends JPanel implements ActionListener
    {
        JTextField tfInfo;
        JLabel lblColor, lblShapes;
        JCheckBox cbRed, cbBlue;
        ButtonGroup shapes;
        JRadioButton rbCircle, rbSquare;
        JButton btnSubmit; 
        public Draw(JPanel panel) 
        {
            GridBagConstraints c = new GridBagConstraints();
            tfInfo = new JTextField("Color", 15);
            tfInfo = new JTextField("Shapes", 50);
            lblColor = new JLabel("Colors:");
            cbRed = new JCheckBox("Red");
            cbBlue = new JCheckBox("Blue");
            lblShapes = new JLabel("Shapes:");
            shapes = new ButtonGroup();
            rbCircle = new JRadioButton("Circle");
            rbSquare = new JRadioButton("Square");
            btnSubmit = new JButton("Draw"); 
            btnSubmit.addActionListener(this);
            this.setBackground(Color.WHITE);

            add(lblColor);
            add(cbRed); 
            add(cbBlue); 
            add(lblShapes);
            add(rbCircle);
            add(rbSquare);
            add(btnSubmit);
            shapes.add(rbCircle);
            shapes.add(rbSquare); 
        }
        public void actionPerformed(ActionEvent a)
        {
            if(a.getSource() == btnSubmit)
            { 
                if(cbRed.isSelected()&&cbBlue.isSelected())
                {
                    if(rbCircle.isSelected())
                    {

                    }
                    else if(rbSquare.isSelected())
                    {

                    } 
                }
                else if(cbRed.isSelected())
                {   
                    if(rbCircle.isSelected())
                    { 

                    }
                    else if(rbSquare.isSelected())
                    {

                    }    
                }
                else if(cbBlue.isSelected())
                {  
                    if(rbCircle.isSelected())
                    {

                    } 
                }
                else if(rbSquare.isSelected())
                {

                } 
            }
            repaint();
        }
    }
Mike M.
  • 38,532
  • 8
  • 99
  • 95
user3185727
  • 159
  • 1
  • 3
  • 12

1 Answers1

4

Start by separating your "management" code from you "painting" code

You should have a single class that only handles the painting of the shape, nothing else, it just does what it's told.

You should then have a second class which takes input from the user and when they press the Draw button, it tells the "paint" class what it should be paint, for example...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DrawStuff extends JFrame {

    public static void main(String[] args) {
        new DrawStuff();
    }

    public DrawStuff() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ControlPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ControlPane extends JPanel {

        private JRadioButton circle;
        private JRadioButton square;

        private DrawPane drawPane;

        public ControlPane() {
            setLayout(new GridBagLayout());

            ButtonGroup bg = new ButtonGroup();
            circle = new JRadioButton("Circle");
            square = new JRadioButton("Square");

            bg.add(circle);
            bg.add(square);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;

            JPanel shape = new JPanel();
            shape.add(circle);
            shape.add(square);
            add(shape, gbc);

            JButton draw = new JButton("Draw");
            draw.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (circle.isSelected()) {
                        drawPane.setDrawableShape(DrawableShape.CIRCLE);
                    } else if (square.isSelected()) {
                        drawPane.setDrawableShape(DrawableShape.SQUARE);
                    }
                }
            });

            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(draw, gbc);

            drawPane = new DrawPane();

            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = gbc.BOTH;
            add(drawPane, gbc);
        }

    }

    public enum DrawableShape {
        CIRCLE,
        SQUARE
    }

    public class DrawPane extends JPanel {

        private DrawableShape drawableShape;

        public DrawPane() {
        }

        public void setDrawableShape(DrawableShape drawableShape) {
            this.drawableShape = drawableShape;
            repaint();
        }

        public DrawableShape getDrawableShape() {
            return drawableShape;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            DrawableShape shape = getDrawableShape();
            if (shape != null) {
                int width = getWidth() - 20;
                int height = getHeight() - 20;
                int size = Math.min(width, height);

                int x = (getWidth() - size) / 2;
                int y = (getHeight() - size) / 2;
                if (shape == DrawableShape.CIRCLE) {
                    g2d.fillOval(x, y, size, size);
                } else if (shape == DrawableShape.SQUARE) {
                    g2d.fillRect(x, y, size, size);
                }
            }
            g2d.dispose();
        }

    }
}

I'll leave you to add in the color management.

Have a closer look at:

for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366