如何在旧版本的JDK中绘制形状?

时间:2015-09-27 14:53:49

标签: java swing awt

现在我的程序运行并编译没有任何问题。我想要它绘制三角形,正方形,椭圆形和矩形。我知道我需要多边形方法,但是如何使用旧版本的JDK呢?它与新版本相同吗?

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Random;
import javax.swing.*;

public class Combo2 extends JFrame
{
    public Combo2()
    {
        super("Combo2");
        final ComboPanel comboPanel = new ComboPanel();
        String[] shapeItems = {
            "square", "oval", "rectangle", "circle"
        };
        JComboBox<String> shapeBox = new JComboBox<>(shapeItems);
        shapeBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                if(e.getStateChange() == ItemEvent.SELECTED)
                {
                    String item = (String)e.getItem();
                    if(item.equals("square"))
                        comboPanel.makeSquares();
                    if(item.equals("oval"))
                        comboPanel.makeOvals();
                    if(item.equals("rectangle"))
                        comboPanel.makeRectangles();
                    if(item.equals("circle"))
                        comboPanel.makeCircles();
                }
            }
        });
        JPanel southPanel = new JPanel();
        southPanel.add(shapeBox);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().add(comboPanel, "Center");
        getContentPane().add(southPanel, "South");
        setSize(500,700);
        setLocation(200,200);
        setVisible(true);
    }

    private class ComboPanel extends JPanel
    {
        int w, h;
        Random seed;
        static final int
            OVAL = 0,
            RECT = 1;
        int shapeType = -1;

        public ComboPanel()
        {
            seed = new Random();
            setBackground(Color.white);
        }

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int width = getWidth();
            int height = getHeight();
            int x, y;
            Shape s = null;
            for(int i = 0; i < 20; i++)
            {
                x = seed.nextInt(width - w);
                y = seed.nextInt(height - h);
                switch(shapeType)
                {
                    case OVAL:
                        s = new Ellipse2D.Double(x, y, w, h);
                        break;
                    case RECT:
                        s = new Rectangle2D.Double(x, y, w, h);
                }
                if(shapeType > -1)
                    g2.draw(s);
            }
        }

        public void makeSquares()
        {
            shapeType = RECT;
            w = 50;
            h = 50;
            repaint();
        }

        public void makeOvals()
        {
            shapeType = OVAL;
            w = 80;
            h = 60;
            repaint();
        }

        public void makeRectangles()
        {
            shapeType = RECT;
            w = 80;
            h = 40;
            repaint();
        }

        public void makeCircles()
        {
            shapeType = OVAL;
            w = 75;
            h = 75;
            repaint();
        }
    }

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

0 个答案:

没有答案