无法弄清楚如何返回要添加到框架的多边形绘图数据

时间:2012-07-25 13:14:49

标签: java swing

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

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

我将在下面发布我的代码并在我需要关注的地方写下“需要帮助1”和“需要帮助2”。这是我的程序粗略看起来的image

感谢任何提前尝试对我的问题进行抨击的人。

    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从头开始重建程序。

1 个答案:

答案 0 :(得分:3)

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

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建立标识 - 您可能希望实现它们根据细节要求而有所不同。)