0

我有一个有 2 的程序Frames。1Frame拥有一个简单的ButtonGUIJComboBox界面,而另一个Frame应该根据界面上选择的选项来绘制和操作形状。

我已经设置了界面,但我不知道如何实现多边形绘图。我知道如何绘制多边形,但我在网上搜索的所有教程和帮助并没有真正为我提供绘图+多种方法的组合。此外,我的代码可能与我构建程序的所有尝试都相当复杂,所以我想知道这是否会增加我的问题。

我将在下面发布我的代码,并在我需要关注的地方写下“此处需要帮助 1”和“此处需要帮助 2”。这是我的程序大致样子的图像。

提前感谢任何试图解决我的问题的人。

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.Polygon;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class Final {

// Declare variables for everything used in the program
private JFrame f;

private JPanel p, p2;

private JButton b1, b2, b3, b4, b5, b6, b7, b8;

private JComboBox combo, combo2, combo3;

private JTextField txt, txt2, txt3;

private Graphics g;

private Shape selectedShape;

public Final() {
    gui();
    draw();
}

public void gui() {

    f = new JFrame("Interface");
    f.setVisible(true);
    f.setSize(800, 600);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Closes the program
                                                        // when you click
                                                        // the X

    p = new JPanel(new GridBagLayout()); // create panel and decide the
                                            // details
    p.setBackground(Color.WHITE);
    GridBagConstraints c = new GridBagConstraints(); // Define way to call
                                                        // constraints for
                                                        // grid layout
    GridBagConstraints c2 = new GridBagConstraints();
    GridBagConstraints c3 = new GridBagConstraints();
    c.insets = new Insets(0, 0, 0, 0); // add spacing between objects on the
                                        // grid. 10 pixels from bottom top,
                                        // left and right
    c2.insets = new Insets(5, 5, 5, 5);
    c3.insets = new Insets(0, 0, 0, 0);

    // Combo Box stuff below this
    String course2[] = { "CHOOSE", "YELLOW", "BLUE", "RED" };
    String course3[] = { "CHOOSE", "YELLOW", "BLUE", "RED" };

    combo = new JComboBox(Shape.values());
    combo.setBackground(Color.white);
    combo.setForeground(Color.black);
    txt = new JTextField(20);
    combo.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ie) {
            if(ie.getStateChange() == ItemEvent.DESELECTED) {
                return;
            }
            Shape selectedShape = (Shape) combo.getSelectedItem();
            setSelectedShape(selectedShape);
            String str = selectedShape.getLabel();
            txt.setText(str);
            switch (selectedShape) {
            case NONE:                  
                break;
            case PARALLELOGRAM:
                drawParallelogram();
                break;
            case QUADRILATERAL:
                drawQuadrilateral();
                break;
            case RECTANGLE:
                drawRectangle();
                break;
            case RHOMBUS:
                drawRhombus();
                break;
            case SQUARE:
                drawSquare();
                break;
            case TRAPEZOID:
                drawTrapezoid();
                break;
            case TRIANGLE:
                drawTriangle();
                break;
            default:
                break;
            }
        }

        private Polygon drawTriangle() {
            // Need Help Here 1
            Polygon triangle = new Polygon();

            triangle.addPoint(100,100);
            triangle.addPoint(150,150);
            triangle.addPoint(50,150);
            return triangle;
        }

        private void drawTrapezoid() {
            // TODO Auto-generated method stub          
            System.out.println("This is the code to draw a trapezoid");
        }

        private void drawSquare() {
            // TODO Auto-generated method stub      
            g.drawRect(0, 0, 50, 50);
        }

        private void drawRhombus() {
            // TODO Auto-generated method stub      
        }

        private void drawRectangle() {
            // TODO Auto-generated method stub          
            System.out.println("This is the code to draw a rectangle");
        }

        private void drawQuadrilateral() {
            // TODO Auto-generated method stub      
            System.out.println("This is the code to draw a quadrilateral");
        }

        private void drawParallelogram() {
            // TODO Auto-generated method stub          
            System.out.println("This is the code to draw a parallelogram");
        }
    });

    combo2 = new JComboBox(course2);
    combo2.setBackground(Color.white);
    combo2.setForeground(Color.black);
    txt2 = new JTextField(20);
    combo2.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ie) {
            String str2 = (String) combo2.getSelectedItem();
            txt2.setText(str2 + " BORDER");
        }
    });

    combo3 = new JComboBox(course3);
    combo3.setBackground(Color.white);
    combo3.setForeground(Color.black);
    txt3 = new JTextField(20);
    combo3.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ie) {
            String str3 = (String) combo3.getSelectedItem();
            txt3.setText(str3 + " INSIDE");
        }
    });

    // X and Y movement buttons
    b1 = new JButton("X+");
    b2 = new JButton("X-");
    b3 = new JButton("Y+");
    b4 = new JButton("Y-");
    b5 = new JButton("Clockwise");
    b6 = new JButton(" Counter ");
    b7 = new JButton("Enlarge");
    b8 = new JButton("Shrink ");

    // Object adding to panel and grid layout here
    c.gridx = 0;
    c.gridy = 0;
    p.add(combo, c);

    c.gridx = 1;
    c.gridy = 0;
    p.add(txt, c);

    c2.gridx = 0; // x and y for first object
    c2.gridy = 1;
    p.add(b1, c2);

    c2.gridx = 1;
    c2.gridy = 1;
    p.add(b2, c2);

    c2.gridx = 0;
    c2.gridy = 2;
    p.add(b3, c2);

    c2.gridx = 1;
    c2.gridy = 2;
    p.add(b4, c2);

    c3.gridx = 0;
    c3.gridy = 4;
    p.add(combo2, c3);

    c3.gridx = 1;
    c3.gridy = 4;
    p.add(txt2, c3);

    c3.gridx = 0;
    c3.gridy = 5;
    p.add(combo3, c3);

    c3.gridx = 1;
    c3.gridy = 5;
    p.add(txt3, c3);

    c2.gridx = 0;
    c2.gridy = 6;
    p.add(b5, c2);

    c2.gridx = 1;
    c2.gridy = 6;
    p.add(b6, c2);

    c2.gridx = 0;
    c2.gridy = 7;
    p.add(b7, c2);

    c2.gridx = 1;
    c2.gridy = 7;
    p.add(b8, c2);

    f.add(p, BorderLayout.NORTH); // add the entire panel to the frame
    f.show();
}

public void draw() {
    // Need help here 2
    // Create a frame
    JFrame frame = new JFrame();

    // Add a component with a custom paint method
    //frame.getContentPane().add(new MyComponent());

    Container contentPane = frame.getContentPane();
    contentPane.add(drawTriangle());

    // Display the frame
    int frameWidth = 600;
    int frameHeight = 600;
    frame.setSize(frameWidth, frameHeight);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

  /* class MyComponent extends JComponent {
    // This method is called whenever the contents needs to be painted
    public void paint(Graphics g) {
        // Retrieve the graphics context; this object is used to paint
        // shapes
        Graphics2D g2d = (Graphics2D) g;
    }
}*/

public static void main(String[] args) {

    new Final();

}

public Shape getSelectedShape() {
    return selectedShape;
}

public void setSelectedShape(Shape selectedShape) {
    this.selectedShape = selectedShape;
}

public enum Shape {
    NONE, TRIANGLE, QUADRILATERAL, PARALLELOGRAM, TRAPEZOID, RHOMBUS, RECTANGLE, SQUARE;

    static {
        NONE.label = "Choose one...";
        TRIANGLE.label = "Triangle";
        QUADRILATERAL.label = "Quadrillateral";
        PARALLELOGRAM.label = "Parallelogram";
        TRAPEZOID.label = "Trapezoid";
        RHOMBUS.label = "Rhombus";
        RECTANGLE.label = "Rectangle";
        SQUARE.label = "Square";
    }

    private String label;

    public String getLabel() {
        return label;
    }
}
}

对不起,这JComboBoxes似乎是我存在的祸根,我想知道我是否应该只使用从头开始重建程序JButtons

4

1 回答 1

3

我将为形状创建自己的类,而不是形状的枚举(在您的情况下会导致非 OO 开关情况)。然后您可以使用子类(或 java.awt.Shape 类型的成员)来实现绘制不同的形状(该示例假设使用 java.awt.Shape 来描述您传递给构造函数的要绘制的元素):

public class MyShape {

    private String label;
    private Shape awtShape;

    public MyShape(String label, Shape awtShape) {
        this.label = label;
        this.awtShape = awtShape;
    }

    public String toString() {
        return label;
    }

    public int hashCode() {
        return label.hashCode();
    }

    public boolean equals(Object obj) {
        return obj instanceof MyShape && ((MyShape) obj).label.equals(label);
    }

    // x and y parameters may be useful when the shape needs to be relocated
    // when drawing. Use Graphics.translate(x, y) to draw the shape at different
    // coordinates (don't forget to undo the translater after that).
    public void draw(Graphics2D g, int x, int y) {
        g.fill(awtShape);
    }

}

通过实现 toString()、hashCode() 和 equals() 方法,您可以将 MyShape 类用作组合框项(toString() 控制组合框中的显示,hashCode/equals 建立标识 - 您可能希望根据不同的方式实现它们详细要求)。

于 2012-07-25T13:55:58.820 回答