⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myapplet.java

📁 使用Swing完成基本流程图的绘制
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.lang.Math;
import javax.swing.*;

public class MyApplet extends JApplet
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    JTextField t = new JTextField(10);

    MyDraw myDraw = new MyDraw();

    JMenu[] menus = { new JMenu("菜单"), new JMenu("设置"),
            new JMenu("帮助") };

    JMenuItem[] menuItems = { new JMenuItem("Fee"), new JMenuItem("Fi"),
            new JMenuItem("Fo"), new JMenuItem("Zip"), new JMenuItem("Zap"),
            new JMenuItem("Zop"), new JMenuItem("Olly"), new JMenuItem("Oxen"),
            new JMenuItem("Free") };

    ActionListener al = new ActionListener()
    {

        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated
            // method stub
            t.setText(((JMenuItem) e.getSource()).getText());
        }
    };

    private JLabel statusBar; // 显示鼠标状态的提示条

    private JToolBar toolBar; // 定义按钮面板

    private JButton[] choices; // 按钮数组,存放以下名称的功能按钮

    private String[] names = { "", "", "", "mouse", "Line", "Rect", "fRect",
            "Oval", "fOval", "Circle", "fCircle", "RoundRect", "frRect",
            "Arrow", "Color", "Stroke", "Word" /* 画图板上面有的基本的几个绘图单元按钮 */
    };

    private final static int STATUS_MOUSE = 3;
    private final static int STATUS_LINE = 4;
    private final static int STATUS_RECT = 5;
    private final static int STATUS_FRECT = 6;
    private final static int STATUS_OVAL = 7;
    private final static int STATUS_FOVAL = 8;
    private final static int STATUS_CIRCLE = 9;
    private final static int STATUS_FCIRCLE = 10;
    private final static int STATUS_ROUNDRECT = 11;
    private final static int STATUS_FROUNDRECT = 12;
    private final static int STATUS_ARROW = 13;
    private final static int STATUS_COLOR = 14;
    private final static int STATUS_STROKE = 15;
    private final static int STATUS_WORD = 16;
    
    private Icon[] items;

    private String[] tipText = {
            // 这里是鼠标移动到相应按钮上面上停留时给出的提示说明条
            // 读者可以参照上面的按钮定义对照着理解
            "Draw a new picture", "Open a saved picture",
            "Save current drawing", "鼠标", "画一条直线", "画空心的矩形", "画一个用当前颜色填充的矩形",
            "画一个空心椭圆", "画一个用当前颜色填充的椭圆", "画一个圆", "画一个带填充色的圆", "画一个圆角矩形",
            "画一个带填充色的圆角矩形", "流程连接", "选择当前绘画的颜色", "设置线条的粗细", "在鼠标点击位置输入文字" };

    // 设置默认画图状态为鼠标
    int currentChoice = 3;

    ButtonHandle buttonHandle = new ButtonHandle();

    FlowItems[] itemList = new FlowItems[5000]; // 用来存放基本图形的数组

    int index = 0; // 当前已经绘制的图形数目

    int R, G, B; // 用来存放当前色彩值

    private float stroke = 1.0f; // 设置画笔粗细,默认值为1.0f

    int x_old;

    int y_old;

    public void init()
    {
        for (int i = 0; i < menuItems.length; i++)
        {
            menuItems[i].addActionListener(al);
            menus[i % 3].add(menuItems[i]);
        }

        JMenuBar menuBar = new JMenuBar();
        for (int i = 0; i < menus.length; i++)
        {
            menuBar.add(menus[i]);
        }
        setJMenuBar(menuBar);

        Container cp = getContentPane();

        statusBar = new JLabel();
        statusBar.setText("流程图测试——令狐彬");

        choices = new JButton[names.length];
        toolBar = new JToolBar(JToolBar.HORIZONTAL);
        items = new ImageIcon[names.length];

        // 导入我们需要的图形图标,这些图标都存放在与源文件相同的目录下面
        for (int i = 3; i < choices.length; i++)
        {
            // 如果在jbuilder下运行本程序,则应该用这条语句导入图片
            try
            {
                items[i] = new ImageIcon (new URL(getCodeBase(), names[i] + ".gif"));
            }
            catch (MalformedURLException e)
            {
                throw new RuntimeException( "MalformedURLException Exception encountered", e);
            }

            // 默认的在jdk或者jcreator下运行,用此语句导入图片
            choices[i] = new JButton("", items[i]);
            choices[i].setPreferredSize(new Dimension(41, 35));
            choices[i].setToolTipText(tipText[i]);
            toolBar.add(choices[i]);

            choices[i].addActionListener(buttonHandle);
        }

        cp.add(toolBar, BorderLayout.NORTH);
        cp.add(BorderLayout.CENTER, myDraw);
        cp.add(statusBar, BorderLayout.SOUTH);
    }

    public static void main(String[] args)
    {
        Console.run(new MyApplet(), 500, 500);
    }

    class ButtonHandle implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            // TODO Auto-generated method stub
            for (int i = 3; i < choices.length; i++) // 选中鼠标
            {
                if (i == 3 && e.getSource() == choices[i])
                {
                    currentChoice = i;
                    break;
                }
                else if (e.getSource() == choices[i])
                {
                    currentChoice = i;
                    createNewItem();
                    repaint();
                    break;
                }
            }
        }
    }

    class MyDraw extends JPanel
    {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public MyDraw()
        {
            // 定义鼠标图形
            setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
            setBackground(Color.WHITE);
            addMouseListener(new mouseA());
            addMouseMotionListener(new mouseB());
        }

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g; // 定义画笔

            int j = 0;
            while (j <= index && index != 0)
            {
                draw(g2d, itemList[j]);
                j++;
            }
        }

        void draw(Graphics2D g2d, FlowItems i)
        {
            i.draw(g2d);// 将画笔传入到各个子类中,用来完成各自的绘图
        }
    }

    // 鼠标事件mouseA类,继承了MouseAdapter,用来完成鼠标相应事件操作
    class mouseA extends MouseAdapter
    {
        public void mousePressed(MouseEvent e)
        {
            statusBar.setText("     Mouse Pressed @:[" + e.getX() + ", "
                    + e.getY() + "]");// 设置状态提示
            x_old = e.getX();
            y_old = e.getY();

            if (currentChoice == STATUS_MOUSE)
            {
                CPoint point = new CPoint(e.getPoint());
                for (int i = 0; i < index; i++)
                {
                    if (point.pointInArea(itemList[i]))
                    {     
                        itemList[i].pressCount++;
                        
                        if (!itemList[i].isSelected)
                        {
                            itemList[i].R = 255;
                            itemList[i].G = 0;
                            itemList[i].B = 0;
                            itemList[i].isSelected = true;                        
                        }
                        else if (itemList[i].isSelected && itemList[i].pressCount == 2)
                        {
                            itemList[i].R = 0;
                            itemList[i].G = 0;
                            itemList[i].B = 0;
                            itemList[i].isSelected = false;
                            //itemList[i].pressCount = 0;
                        }                
                        repaint();
                        break;
                    }// */
                }
            }
            else if(currentChoice == STATUS_ARROW)
            {
                CPoint point = new CPoint(e.getPoint());
                int i;
                for (i = 0; i < index; i++)
                {
                    if (point.pointInArea(itemList[i]))
                    {
                        if (itemList[i].type == 5)
                        {
                            itemList[index].x1 = itemList[i].x2;
                            itemList[index].y1 = (itemList[i].y1 + itemList[i].y2)/2;
                        }
                        //repaint();
                        break;
                    }
                }
                // */
                if (i == index)
                {
                    itemList[index].x1 = itemList[index].x2 = e.getX();
                    itemList[index].y1 = itemList[index].y2 = e.getY();
                }
            }
            else
            {
                itemList[index].x1 = itemList[index].x2 = e.getX();
                itemList[index].y1 = itemList[index].y2 = e.getY();
            }
        }

        public void mouseReleased(MouseEvent e)
        {
            statusBar.setText("     Mouse Released @:[" + e.getX() + ", "
                    + e.getY() + "]");

            if (currentChoice == STATUS_MOUSE)
            {
                CPoint point = new CPoint(e.getPoint());
                for (int i = 0; i < index; i++)
                {
                    if (point.pointInArea(itemList[i]) && itemList[i].isDraging)
                    {
                        itemList[i].isDraging = false;   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -