?? minidrawpad.java
字號:
itemList[index-1].y1=itemList[index].y2=itemList[index].y1=e.getY(); index++; createNewItem(); } else { itemList[index].x2=e.getX(); itemList[index].y2=e.getY(); } repaint(); } public void mouseMoved(MouseEvent e) {statusBar.setText(" Mouse Moved @:[" + e.getX() + ", " + e.getY() + "]");} }//選擇字體風格時候用到的事件偵聽器類,加入到字體風格的選擇框中private class checkBoxHandler implements ItemListener { public void itemStateChanged(ItemEvent e) { if(e.getSource()==bold) 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 { public DrawPanel() { 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) { draw(g2d,itemList[j]); j++; } } void draw(Graphics2D g2d,drawings i) { i.draw(g2d);//將畫筆傳入到各個子類中,用來完成各自的繪圖 } }//新建一個畫圖基本單元對象的程序段 void createNewItem() { if(currentChoice==14)//進行相應的游標設置 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 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, "Choose a color",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( "Please input a float stroke value! ( >0 )"); 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(); } catch(EOFException endofFileException){ JOptionPane.showMessageDialog(this,"no more record in file", "class not found",JOptionPane.ERROR_MESSAGE ); } catch(ClassNotFoundException classNotFoundException){ JOptionPane.showMessageDialog(this,"Unable to Create Object", "end of file",JOptionPane.ERROR_MESSAGE ); } catch (IOException ioException){ JOptionPane.showMessageDialog(this,"error during read from file", "read Error",JOptionPane.ERROR_MESSAGE ); } } }//新建一個文件程序段 public void newFile() { index=0; currentChoice=3; color=Color.black; stroke=1.0f; createNewItem(); repaint();//將有關值設置為初始狀態,并且重畫 }//主函數段 public static void main(String args[]) {try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch ( Exception e ) {}//將界面設置為當前windows風格 MiniDrawPad newPad=new MiniDrawPad(); newPad.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e) {System.exit(0);}}); }}//定義畫圖的基本圖形單元class drawings implements Serializable//父類,基本圖形單元,用到串行化接口,保存時所用 { int x1,y1,x2,y2; //定義坐標屬性 int R,G,B; //定義色彩屬性 float stroke; //定義線條粗細屬性 int type; //定義字體屬性 String s1; String s2; //定義字體風格屬性 void draw(Graphics2D g2d){};//定義繪圖函數 }/******************************************************************************* 下面是各種基本圖形單元的子類,都繼承自父類drawings,請仔細理解繼承的概念********************************************************************************/ class Line extends drawings //直線類 { void draw(Graphics2D g2d) {g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL)); g2d.drawLine(x1,y1,x2,y2); } } class Rect extends drawings//矩形類 { void draw(Graphics2D g2d) {g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.drawRect(Math.min(x1,x2),Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2)); } } class fillRect extends drawings//實心矩形類 { void draw(Graphics2D g2d) {g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.fillRect(Math.min(x1,x2),Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2)); } } class Oval extends drawings//橢圓類 { void draw(Graphics2D g2d) {g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.drawOval(Math.min(x1,x2),Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2)); } } class fillOval extends drawings//實心橢圓 { void draw(Graphics2D g2d) {g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.fillOval(Math.min(x1,x2),Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2)); } } class Circle extends drawings//圓類 { void draw(Graphics2D g2d) {g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.drawOval(Math.min(x1,x2),Math.min(y1,y2), Math.max(Math.abs(x1-x2),Math.abs(y1-y2)), Math.max(Math.abs(x1-x2),Math.abs(y1-y2)) ); } } class fillCircle extends drawings//實心圓 { void draw(Graphics2D g2d) {g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.fillOval(Math.min(x1,x2),Math.min(y1,y2), Math.max(Math.abs(x1-x2),Math.abs(y1-y2)), Math.max(Math.abs(x1-x2),Math.abs(y1-y2)) ); } } class RoundRect extends drawings//圓角矩形類 { void draw(Graphics2D g2d) {g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.drawRoundRect(Math.min(x1,x2),Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2), 50,35); } } class fillRoundRect extends drawings//實心圓角矩形類 { void draw(Graphics2D g2d) {g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke)); g2d.fillRoundRect(Math.min(x1,x2),Math.min(y1,y2), Math.abs(x1-x2),Math.abs(y1-y2), 50,35); } } class Pencil extends drawings//隨筆畫類 { void draw(Graphics2D g2d) {g2d.setPaint(new Color(R,G,B)); g2d.setStroke(new BasicStroke(stroke, BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL)); g2d.drawLine(x1,y1,x2,y2); } } class Rubber extends drawings//橡皮擦類 { void draw(Graphics2D g2d) {g2d.setPaint(new Color(255,255,255)); g2d.setStroke(new BasicStroke(stroke+4, BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL)); g2d.drawLine(x1,y1,x2,y2); } } class Word extends drawings//輸入文字類 { void draw(Graphics2D g2d) { g2d.setPaint(new Color(R,G,B)); g2d.setFont(new Font(s2,x2+y2,((int)stroke)*18)); if (s1!= null ) g2d.drawString(s1,x1,y1); } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -