?? minidrawpad.java~32~
字號:
jMenuFile.add(jMenuFileOpen);
jMenuFile.add(jMenuFileSave);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileExit);
jMenuColor.add(jMenuColorChoose);
jMenuStroke.add(jMenuStrokeChoose);
jMenuHelp.add(jMenuAbout);
contentPane.add(drawingArea, java.awt.BorderLayout.CENTER);
contentPane.add(statusBar, java.awt.BorderLayout.SOUTH);
jPopupMenu1.add(jMenuItem1);
jPopupMenu1.add(jMenuItem2);
jPopupMenu1.add(jMenuItem3);
createNewItem();
}
/**
* Help | About action performed.
*
* @param actionEvent ActionEvent
*/
void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
MiniDrawPad_AboutBox dlg = new MiniDrawPad_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.setVisible(true);
}
/**鼠標事件mouseA類,繼承了MouseAdapter,用來完成鼠標相應事件操作**/
class mouseA extends MouseAdapter{ //鼠標事件mouseA類,繼承了MouseAdapter
public void mousePressed(MouseEvent e){ //定義鼠標按下事件
statusBar.setText("當前坐標在:["+e.getX()+","+e.getY()+"]"+"已經畫了"+index+"個圖...");
//設置狀態提示
itemList[index].x1 = itemList[index].x2 = e.getX();//設定坐標X軸
itemList[index].y1 = itemList[index].y2 = e.getY();//設定坐標Y軸
/**如果當前選擇的圖形是隨筆畫或者橡皮擦,則進行下面的操作**/
if(currentChoice == 3 || currentChoice == 13){
itemList[index].x1 = itemList[index].x2 = e.getX();//設定坐標X軸
itemList[index].y1 = itemList[index].y2 = e.getY();//設定坐標Y軸
index++; //指針加1
createNewItem(); //創建新圖
}
/**如果當前選擇的圖形式文字輸入,則進行下面操作**/
if(currentChoice==18){
itemList[index].x1 = e.getX(); //設定坐標X軸
itemList[index].y1 = e.getY(); //設定坐標Y軸
String input; //輸入字符串
input = JOptionPane.showInputDialog(
"請鍵入文字!"); //顯示輸入對話框內容
itemList[index].s1 = input;
itemList[index].x2 = f1; //設定坐標X軸
itemList[index].y2 = f2; //設定坐標Y軸
itemList[index].s2 = style1; //設定字體風格
index++; //指針加1
currentChoice = 18;
createNewItem(); //創建新圖
drawingArea.repaint(); //圖形式文字輸入
}
}
/**鼠標釋放**/
public void mouseReleased(MouseEvent e){ //定義鼠標釋放
statusBar.setText("當前坐標在:["+e.getX()+","+e.getY()+"]"+"已經畫了"+index+"個圖...");
//設置狀態提示
if(currentChoice == 3 || currentChoice == 13){
itemList[index].x1 = e.getX(); //設定坐標X軸
itemList[index].y1 = e.getY(); //設定坐標Y軸
}
itemList[index].x2 = e.getX(); //設定坐標X軸
itemList[index].y2 = e.getY(); //設定坐標Y軸
repaint();
index++; //指針加1
createNewItem(); //創建新圖
}
/**鼠標進入**/
public void mouseEntered(MouseEvent e){ //定義鼠標進入
statusBar.setText("當前坐標在:"+e.getX()+","+e.getY()+"]"+"已經畫了"+index+"個圖...");
} //設置狀態提示
/**鼠標退出**/
public void mouseExited(MouseEvent e){ ////定義鼠標退出
statusBar.setText("當前坐標在:"+e.getX()+","+e.getY()+"]"+"已經畫了"+index+"個圖...");
} //設置狀態提示
/**鼠標右鍵**/
public void mouseClicked(MouseEvent e){ //定義鼠標右鍵
statusBar.setText("當前坐標在:"+e.getX()+","+e.getY()+"]"+"已經畫了"+index+"個圖...");
int mods=e.getModifiers(); //設置狀態提示
if((mods&InputEvent.BUTTON3_MASK)!=0){ //右鍵判斷
jPopupMenu1.show(drawingArea,e.getX(),e.getY());
//顯示快捷菜單
}
}
}//mouseA完成
/**鼠標事件mouseB類繼承了MouseMotionAdapter,用來完成鼠標拖動和鼠標移動時的相應操作**/
class mouseB extends MouseMotionAdapter{
/**鼠標拖移**/
public void mouseDragged(MouseEvent e){
statusBar.setText("當前坐標在:["+e.getX()+","+e.getY()+"]"+"已經畫了"+index+"個圖...");
//設置狀態提示
if(currentChoice == 3 || currentChoice == 13){
itemList[index-1].x1 = itemList[index].x2 = itemList[index].x1 = e.getX();//設定坐標X軸
itemList[index-1].y1 = itemList[index].y2 = itemList[index].y1 = e.getY();//設定坐標Y軸
index++; //指針加1
createNewItem(); //創建新圖
}
else{
itemList[index].x2 = e.getX(); //設定坐標X軸
itemList[index].y2 = e.getY(); //設定坐標Y軸
}
repaint(); //重畫
}
/**鼠標移動**/
public void mouseMoved(MouseEvent e){
statusBar.setText("當前坐標在:["+e.getX()+","+e.getY()+"]"+"已經畫了"+index+"個圖...");
} //設置狀態提示
}
/**選擇字體風格時候用到的時間偵聽器類,加入到字體風格的選擇框中**/
//checkBoxHandler類繼承ItemListener
private class checkBoxHandler implements ItemListener{
public void itemStateChanged(ItemEvent e){ //定義字體改變事件
if(e.getSource() == blod)
if(e.getStateChange() == ItemEvent.SELECTED)
f1 = Font.BOLD;
else
f1 = Font.PLAIN;
if(e.getSource() == italic)
if(e.getStateChange() == ItemEvent.SELECTED)
f2 = Font.ITALIC;
else
f2 = Font.PLAIN;
}
}
/**畫圖板面板類,用來畫圖**/
class DrawPanel extends JPanel{ //畫圖面板類繼承JPanel
/**定義畫圖面板**/
public DrawPanel(){ //定義畫圖面板
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
//設置光標
setBackground(Color.white); //設置背景色為白色
addMouseListener(new mouseA()); //添加鼠標mouseA類偵聽
addMouseMotionListener(new mouseB()); //添加鼠標mouseb類偵聽
}
public void paintComponent(Graphics g){ //調用該UI委托的paint方法
super.paintComponent(g); //定義繪畫組件
Graphics2D g2d = (Graphics2D)g; //定義畫筆
int j = 0;
while(j <= index){
draw(g2d,itemList[j]);
j++;
}
}
void draw(Graphics2D g2d,drawings i){
i.draw(g2d); //將畫筆傳入到各個子類中,用來完成各自的繪圖
}
}
/**新建一個畫圖基本單元對象的程序段**/
void createNewItem(){
if(currentChoice == 16) //進行相應的游標設置
drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
else
drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
switch(currentChoice){
case 3:
itemList[index] = new Pencil(); //隨筆化
break;
case 4:
itemList[index] = new Line(); //直線
break;
case 5:
itemList[index] = new Rect(); //矩形
break;
case 6:
itemList[index] = new fillRect(); //填充矩形
break;
case 7:
itemList[index] = new Oval(); //橢圓
break;
case 8:
itemList[index] = new fillOval(); //填充橢圓
break;
case 9:
itemList[index] = new Circle(); //圓形
break;
case 10:
itemList[index] = new fillCircle(); //填充圓形
break;
case 11:
itemList[index] = new RoundRect(); //圓角矩形
break;
case 12:
itemList[index] = new fillRoundRect();//填充圓角矩形
break;
case 13:
itemList[index] = new Rubber(); //橡皮擦
break;
case 14:
itemList[index] = new Polygon1(); //三角形
break;
case 15:
itemList[index] = new fillPolygon1();//填充三角形
break;
case 16:
itemList[index] = new Word(); //文字
break;
}
itemList[index].type = currentChoice; //當前顏色
itemList[index].R = R;
itemList[index].G = G;
itemList[index].B = B;
itemList[index].stroke = stroke; //當前粗細
}
/**選擇當前顏色程序段**/
public void chooseColor(){
color = JColorChooser.showDialog(MiniDrawPad.this,"請選擇顏色",color);
R = color.getRed();
G = color.getGreen();
B = color.getBlue(); //得到顏色
itemList[index].R = R;
itemList[index].G = G;
itemList[index].B = B; //保存為當前顏色
}
/**選擇當前線條粗細程序段**/
public void setStroke(){
String input; //從對話框得到畫筆粗細值
input = JOptionPane.showInputDialog("請鍵入畫筆粗細值(大于0且小于100)");
stroke = Float.parseFloat(input);
itemList[index].stroke = stroke; //保存為當前畫筆粗細值
}
/**保存圖形文件程序段**/
public void saveFile(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showSaveDialog(this);
if(result == JFileChooser.CANCEL_OPTION)
return;
File fileName = fileChooser.getSelectedFile();
fileName.canWrite();
if(fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
"Invalid File Name",JOptionPane.ERROR_MESSAGE);
else{
try{
fileName.delete();
FileOutputStream fos = new FileOutputStream(fileName);
output = new ObjectOutputStream(fos);
drawings record;
output.writeInt(index);
for(int i=0;i<index;i++){
drawings p = itemList[i];
output.writeObject(p);
output.flush(); //將所有圖形信息強制轉換成父類線性化存儲到文件中
}
output.close();
fos.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
/**打開一個圖形文件程序段**/
public void loadFile(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(this);
if(result == JFileChooser.CANCEL_OPTION)
return;
File fileName = fileChooser.getSelectedFile();
fileName.canRead();
if(fileName == null || fileName.getName().equals(""))
JOptionPane.showMessageDialog(fileChooser,"Invalid File Name",
"Invalid File Name",JOptionPane.ERROR_MESSAGE);
else{
try{
FileInputStream fis = new FileInputStream(fileName);
input = new ObjectInputStream(fis);
drawings inputRecord;
int countNumber = 0;
countNumber = input.readInt();
for(index = 0;index < countNumber;index++){
inputRecord = (drawings)input.readObject();
itemList[index] = inputRecord;
}
createNewItem();
input.close();
repaint();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -