代码之家  ›  专栏  ›  技术社区  ›  Acitropy

绘制用户选择的形状

  •  0
  • Acitropy  · 技术社区  · 12 年前

    我需要创建一个程序来绘制形状(用户用单选按钮选择),以及形状是否填充(用户用复选框选择)。这是我目前掌握的代码:

     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);
            }
        }
    }
    }
    

    我得到的错误是表达式和标识符的非法开头。如果我应该换一种方式来处理这个问题,请告诉我。

    2 回复  |  直到 12 年前
        1
  •  4
  •   MadProgrammer    12 年前

    我认为你需要回归基本。。。

    这行不通。。。

    fp.setBackground("white");
    

    Component#setBackground 不需要 String 作为一个参数,它需要 Color

    你所有的 addItemListener 调用arn不起作用,因为您尚未实现 ItemListener

    我不确定你希望通过这样做来实现什么。。。

    @Override
    fp.dRect();
    

    但这行不通@Override用于指示一个方法被祖先覆盖,您只需调用的方法 FigurePanel

    Java与C和C++一样,是区分大小写的;

    没有这样的阶级 itemEvent …是的 ItemEvent

    public void ItemStateChanged(itemEvent e) {
    

    没有这样的阶级 graphics ,是的 Graphics

    public void paintComponent(graphics g) {
    

    我甚至不会试图猜测你希望通过以下方式实现什么。。。

    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不支持“内联方法”(或者你想怎么称呼它们),不,让它们成为方法也无法实现你想要做的事情。。。

    事实上,你做得很好的一件事就是 paintComponent 然后打电话 super.paintComponent …干得好:D!

    已更新

    我鼓励你通读。。。

    更新了可能的运行示例

    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());
            }
    
        }
    
    }
    
        2
  •  1
  •   Eric Galluzzo Moorthy GK    12 年前

    好吧,下面的代码肯定不是有效的Java代码:

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

    这同样适用于以下dRect等。

    我不确定您使用该代码到底想要实现什么。如果要定义一个名为dLine的方法,您应该执行以下操作:

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

    我还注意到以下代码,它目前不会给您带来问题,但会:

    public void ItemStateChanged(itemEvent e) {
    

    这没有正确的大写,所以它不会编译,而且你也没有听到任何事件,所以它永远不会被调用。

    代码中还有其他各种错误,但这应该会让您开始。