简体   繁体   中英

Draw Shapes That User Selects

I need to create a program to draw shapes(user chooses with radio button), and whether or not the shape is filled(user chooses with check box). This is the code I have so far:

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;

public class SelectShape extends JFrame implements ItemListener{
    private JRadioButton line = new JRadioButton("Line");
    private JRadioButton rect = new JRadioButton("Rectangle");
    private JRadioButton oval = new JRadioButton("Oval");
    private JCheckBox fill = new JCheckBox("Filled");
    private FigurePanel fp;


public static void main(String[] args) {
    SelectShape frame = new SelectShape();
    frame.setTitle("Select Shape");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setSize(400,400);
    frame.setVisible(true);
}

public SelectShape() {
    JPanel p1 = new JPanel();
    p1.add(fp);
    fp.setBackground(Color.WHITE);
    p1.setSize(200,400);

    JPanel p2 = new JPanel();
    p2.setLayout(new FlowLayout());
    p2.add(line);
    p2.add(rect);
    p2.add(oval);
    p2.add(fill);
    add(p2, "South");

    line.addItemListener(this);
    rect.addItemListener(this);
    oval.addItemListener(this);
    fill.addItemListener(this);

}

public void ItemStateChanged(ItemEvent e) {
    if(rect.isSelected()) {
        FigurePanel.dRect();
        repaint();
    }
    else if(oval.isSelected()) {
        FigurePanel.dOval();
        repaint();
    }
    else if(line.isSelected()) {
        FigurePanel.dLine();
        repaint();
    }
    if(fill.isSelected()) {
        FigurePanel.fill();
        repaint();
    }
    else {
        FigurePanel.erase();
        repaint();
    }
}       
}

class FigurePanel extends JPanel {
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    public void dLine(Graphics g) {
        g.drawLine(10,10,160,10);
    }
    public void dRect(Graphics g) {
        g.drawRect(10,10,150,50);
    }
    public void dOval(Graphics g) {
        g.drawOval(10,10,150,50);
    }

    public void fill(Graphics g) {
        g.setColor(Color.GREEN);
        if(rect.isSelected()) {
            g.fillRect(10,10,150,50);
        }
        else if(oval.isSelected()) {
            g.fillOval(10,10,150,50);
        }
    }
    public void erase(Graphics g) {
        g.setColor(Color.WHITE);
        if(rect.isSelected()) {
            g.fillRect(10,10,150,50);
        }
        else if(oval.isSelected()) {
            g.fillOval(10,10,150,50);
        }
    }
}
}

The errors I am getting are illegal start of expression and identifier expected. If I should approach this another way, please tell.

I think you need to go back to basics...

This won't work...

fp.setBackground("white");

Component#setBackground doesn't take a String as a parameter, it takes a Color

All your addItemListener calls arn't going to work, because you've not implement a ItemListener

I'm not sure what it is you hope to achieve by doing this...

@Override
fp.dRect();

But it won't work. @Override is used to indicate that a method was overridden by an ancestor, you are simply calling the method of FigurePanel

Java, like C and C++ is case sensitive;

There is no such class itemEvent ...it's ItemEvent

public void ItemStateChanged(itemEvent e) {

There is no such class graphics , it's Graphics

public void paintComponent(graphics g) {

And I'm not even going to try and guess what it is you were hoping to achieve with the following...

public void paintComponent(graphics g) {
    super.paintComponent(g);
    dLine() {
        g.drawLine(10, 10, 160, 10);
    }
    dRect() {
        g.drawRect(10, 10, 150, 50);
    }
    dOval() {
        g.drawOval(10, 10, 150, 50);
    }
    fill() {
        g.setColor(Color.GREEN);
            if (rect.isSelected()) {
                g.fillRect(10, 10, 150, 50);
            } else if (oval.isSelected()) {
                g.fillOval(10, 10, 150, 50);
            }
        }
    erase() {
        g.setColor(Color.WHITE);
        if (rect.isSelected()) {
            g.fillRect(10, 10, 150, 50);
        } else if (oval.isSelected()) {
            g.fillOval(10, 10, 150, 50);
        }
    }
}

Java doesn't support "inline methods" (or what ever you want to call them) and no, making them methods would also not achieve what you are trying to do...

In fact the one thing you did very well, was to override paintComponent and call super.paintComponent ...well done :D !

Updated

I would encourage you to have a read through...

Updated with possible running example

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DrawShapes {

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

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

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

    public class DrawPane extends JPanel {

        public DrawPane() {

            setLayout(new BorderLayout());

            RenderPane rp = new RenderPane();
            add(new ControlsPane(rp), BorderLayout.NORTH);
            add(rp);

        }

    }

    public class ControlsPane extends JPanel {

        public ControlsPane(RenderPane rp) {

            JRadioButton[] btns = new JRadioButton[4];
            btns[0] = new JRadioButton(new LineAction(rp));
            btns[1] = new JRadioButton(new RectangleAction(rp));
            btns[2] = new JRadioButton(new OvalAction(rp));
            btns[3] = new JRadioButton(new ClearAction(rp));

            ButtonGroup bg = new ButtonGroup();
            for (JRadioButton btn : btns) {
                bg.add(btn);
                add(btn);
            }

        }

    }

    public class RenderPane extends JPanel {

        private Shape shape;

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

        public void setShape(Shape shape) {
            this.shape = shape;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (shape != null) {
                g2d.setColor(Color.RED);
                g2d.draw(shape);
            }
            g2d.dispose();
        }

    }

    public class LineAction extends AbstractRenderAction {

        public LineAction(RenderPane renderPane) {
            super(renderPane);
            putValue(NAME, "Line");
        }

        @Override
        public Shape getShape() {
            return new Line2D.Float(0f, 0f, getRenderPane().getWidth(), getRenderPane().getHeight());
        }

    }

    public class RectangleAction extends AbstractRenderAction {

        public RectangleAction(RenderPane renderPane) {
            super(renderPane);
            putValue(NAME, "Rectangle");
        }

        @Override
        public Shape getShape() {
            return new Rectangle2D.Float(10, 10, getRenderPane().getWidth() - 20, getRenderPane().getHeight() - 20);
        }

    }

    public class OvalAction extends AbstractRenderAction {

        public OvalAction(RenderPane renderPane) {
            super(renderPane);
            putValue(NAME, "Oval");
        }

        @Override
        public Shape getShape() {
            float radius = Math.min(getRenderPane().getWidth() - 20, getRenderPane().getHeight() - 20);
            return new Ellipse2D.Float(10, 10, radius, radius);
        }

    }

    public class ClearAction extends AbstractRenderAction {

        public ClearAction(RenderPane renderPane) {
            super(renderPane);
            putValue(NAME, "Clear");
        }

        @Override
        public Shape getShape() {
            return null;
        }

    }

    public abstract class AbstractRenderAction extends AbstractAction {

        private RenderPane renderPane;

        public AbstractRenderAction(RenderPane renderPane) {
            this.renderPane = renderPane;
        }

        public RenderPane getRenderPane() {
            return renderPane;
        }

        public abstract Shape getShape();

        @Override
        public void actionPerformed(ActionEvent e) {
            getRenderPane().setShape(getShape());
        }

    }

}

Well, the following is definitely not valid Java code:

    dLine() {
        g.drawLine(10,10,160,10);
    }

The same applies to the following dRect, etc.

I'm not sure exactly what you're trying to achieve with that code. If it is to define a method called dLine, you would do the following instead:

public void dLine(Graphics g) {
    g.drawLine(10, 10, 160, 10);
}

I also noticed the following code, which is not currently causing you problems, but it will:

public void ItemStateChanged(itemEvent e) {

This is not capitalized properly, so it will not compile, and you're also not listening to any events, so it will never get called.

There are various other errors in the code, but this should get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM