?? myapplet.java
字號:
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.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 + -