Draw Shapes That User Selects

后端 未结 2 1629
走了就别回头了
走了就别回头了 2021-01-27 11:21

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:

相关标签:
2条回答
  • 2021-01-27 11:39

    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...

    • The Java Trails, especially those covering the basics, especially those covering inheritance
    • Custom Painting
    • 2D Graphics

    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());
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2021-01-27 11:49

    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.

    0 讨论(0)
提交回复
热议问题