📄 applet1.java~853~
字号:
package paint;import java.awt.*;import java.awt.event.*;import java.applet.*;import java.lang.*;import java.net.*;import java.io.*;import com.borland.jbcl.layout.*;import java.awt.font.*;import java.awt.image.*;public class Applet1 extends Applet{ boolean isStandalone = false; Point test; int ImageRow,ImageCol; byte temp[]; String Str_Command="自绘"; //绘图指示字 boolean IsBlack=true; //颜色指示字 boolean IsBian=false; //上一步操作是不是反色 //实际存储的数据 int Data[][]=new int[30][100]; //缓冲取数据 int OldData[][]=new int[30][100]; static final int PORT = 8586;//定义的服务器端口 String hostname;//定义的服务器的地址 Button ControlButton = new Button("发送"); Panel ControlPanel = new Panel(); Choice choice1 = new Choice(); Label label2 = new Label(); TextField textField1 = new TextField(); Label label1 = new Label(); DrawPanel mypanel = new DrawPanel(); Panel temppanel = new Panel(); Panel panel=new Panel(); GridLayout gridLayout1 = new GridLayout(); BorderLayout borderLayout1 = new BorderLayout(); TextField tf_Str = new TextField(); Label label3 = new Label(); GridLayout gridLayout2 = new GridLayout(); Checkbox checkbox1 = new Checkbox(); Choice choice2 = new Choice(); //获取RGB值,并给Data赋值 public void handlesinglepixel(int x, int y, int pixel) { //得到RGB值 int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel ) & 0xff; // 给Data数组赋值 if ( green==255 ) {if (IsBlack==true) Data[y][x]=1; else Data[y][x]=0; } } //获得像素并进行处理 public void handlepixels(Image img, int x, int y, int w, int h) { int[] pixels = new int[w * h]; PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); return; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return; } for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { handlesinglepixel(x+i, y+j, pixels[j * w + i]); } } } class DrawPanel extends Panel implements MouseListener, MouseMotionListener { int colindex,rowindex; int colstartindex,rowstartindex,colendindex,rowendindex; int tempcolindex,tempcololdindex; int temprowindex,temprowoldindex; public DrawPanel() { setBackground(new Color(0,190,255)); addMouseMotionListener(this); addMouseListener(this); colindex=0; rowindex=0; colendindex=0; rowendindex=0; colstartindex=0; rowstartindex=0; tempcolindex=0; tempcololdindex=0; temprowindex=0; temprowoldindex=0; // setCursor(new Cursor(1)); } public void mouseDragged(MouseEvent e) { //得到鼠标位置 tempcolindex=e.getX(); temprowindex=e.getY(); if((e.getModifiers()&e.BUTTON3_MASK)!=0) return ; else { if (Str_Command=="自绘") //自由描绘 {// int colindex,rowindex; test = e.getPoint(); rowindex=(test.y/5)-1; colindex=(test.x/5)-1; if (IsBlack==true) Data[rowindex][colindex]=1; else Data[rowindex][colindex]=0; repaint(); } else if (Str_Command=="橡皮") { // int colindex,rowindex; colindex=(e.getX()/5)-1; rowindex=(e.getY()/5)-1; Data[rowindex][colindex]=0; repaint(); } else if (Str_Command=="直线") { Graphics g=mypanel.getGraphics(); g.setXORMode(Color.red); if (tempcololdindex!=0 && temprowoldindex!=0 ) g.drawLine(colstartindex*5,rowstartindex*5,tempcololdindex,temprowoldindex); g.drawLine(colstartindex*5,rowstartindex*5,tempcolindex,temprowindex); } else if (Str_Command=="矩形") { Graphics g=mypanel.getGraphics(); g.setXORMode(Color.red); if (tempcololdindex!=0 && temprowoldindex!=0 ) g.drawRect(colstartindex*5,rowstartindex*5, tempcololdindex-colstartindex*5, temprowoldindex-rowstartindex*5); g.drawRect(colstartindex*5,rowstartindex*5, tempcolindex-colstartindex*5, temprowindex-rowstartindex*5); } else if (Str_Command=="填充矩形") { Graphics g=mypanel.getGraphics(); g.setXORMode(Color.red); if (tempcololdindex!=0 && temprowoldindex!=0 ) g.fillRect(colstartindex*5,rowstartindex*5, tempcololdindex-colstartindex*5, temprowoldindex-rowstartindex*5); g.fillRect(colstartindex*5,rowstartindex*5, tempcolindex-colstartindex*5, temprowindex-rowstartindex*5); } else if (Str_Command=="椭圆") { Graphics g=mypanel.getGraphics(); g.setXORMode(Color.red); if (tempcololdindex!=0 && temprowoldindex!=0 ) g.drawOval(colstartindex*5,rowstartindex*5, tempcololdindex-colstartindex*5, temprowoldindex-rowstartindex*5); g.drawOval(colstartindex*5,rowstartindex*5, tempcolindex-colstartindex*5, temprowindex-rowstartindex*5); } else if (Str_Command=="填充椭圆") { Graphics g=mypanel.getGraphics(); g.setXORMode(Color.red); if (tempcololdindex!=0 && temprowoldindex!=0 ) g.fillOval(colstartindex*5,rowstartindex*5, tempcololdindex-colstartindex*5, temprowoldindex-rowstartindex*5); g.fillOval(colstartindex*5,rowstartindex*5, tempcolindex-colstartindex*5, temprowindex-rowstartindex*5); } } tempcololdindex=tempcolindex; temprowoldindex=temprowindex; } public void mouseMoved(MouseEvent e) { int x=e.getX(); int y=e.getY(); Graphics g=this.getGraphics(); g.clearRect(250,90,100,15); if (x>5 && x<29 && y>80 && y<104) g.drawString("自绘曲线",250,100); else if (x>29 && x<53 && y>80 && y<104 ) g.drawString("直线",250,100); else if (x>53 && x<77 && y>80 && y<104 ) g.drawString("矩形",250,100); else if (x>77 && x<101 && y>80 && y<104 )g.drawString("填充的矩形",250,100); else if (x>101 && x<125 && y>80 && y<104 ) g.drawString("椭圆",250,100); else if (x>5 && x<29 && y>104 && y<128 ) g.drawString("填充的椭圆",250,100); else if (x>29 && x<53 && y>104 && y<128 ) g.drawString("反色",250,100); else if (x>53 && x<77 && y>104 && y<128 ) g.drawString("清空",250,100); else if (x>77 && x<101 && y>104 && y<128 ) g.drawString("文字",250,100); else if (x>101 && x<125 && y>104 && y<128 ) g.drawString("撤销",250,100); else if (x>125 && x<149 && y>104 && y<128 ) g.drawString("橡皮",250,100); } public void mousePressed(MouseEvent e) { colstartindex=e.getX()/5; rowstartindex=e.getY()/5; if ((((colstartindex-1)<=ImageCol) && ((rowstartindex-1)<=ImageRow)) || (e.getX()>29 && e.getX()<77 && e.getY()>104 && e.getY()<128 )) { for (int i=0;i<29;i++) { for (int j=0;j<99;j++) { OldData[i][j]=Data[i][j]; } } } int x,y; int Size=10; //取反操作 test = e.getPoint(); rowindex=test.y/5-1; colindex=test.x/5-1; if (Str_Command=="自绘") { if (Data[rowindex][colindex]==0) Data[rowindex][colindex]=1; else if (Data[rowindex][colindex]==1) Data[rowindex][colindex]=0; } else if (Str_Command=="橡皮") { Data[rowindex][colindex]=0; } //获得鼠标位置 x=e.getX(); y=e.getY(); //绘制文字 if ((Str_Command=="文字") && (y<75)) { this.setBackground(Color.blue); Image offscreenimage=createImage(72,14);//创建屏外绘图区 Graphics offscreen=offscreenimage.getGraphics();//取得绘图环境 switch(choice2.getSelectedIndex()) { case 0: Size=12; break; case 1: Size=10; break; case 2: Size=11; break; case 3: Size=13; break; } offscreen.setFont(new Font("宋体",Font.PLAIN,Size)); offscreen.setColor(Color.green); String Str_temp=tf_Str.getText(); offscreen.drawString(Str_temp,colindex,rowindex+5); handlepixels(offscreenimage,0,0,72,14); this.setBackground(new Color(0,190,255)); } //设置指示字 if (x>5 && x<29 && y>80 && y<104) { Str_Command="自绘"; IsBian=false;} else if (x>29 && x<53 && y>80 && y<104 ) { Str_Command="直线"; IsBian=false;} else if (x>53 && x<77 && y>80 && y<104 ) { Str_Command="矩形"; IsBian=false;} else if (x>77 && x<101 && y>80 && y<104 ) { Str_Command="填充矩形"; IsBian=false;} else if (x>101 && x<125 && y>80 && y<104 ) { Str_Command="椭圆"; IsBian=false; } else if (x>5 && x<29 && y>104 && y<128 ) { Str_Command="填充椭圆"; IsBian=false; } //取反操作 else if (x>29 && x<53 && y>104 && y<128 ) { for (int i=0;i<15;i++) { for (int j=0;j<75;j++) { if (Data[i][j]==1) Data[i][j]=0; else Data[i][j]=1; } } IsBian=true; if (IsBlack==true) IsBlack=false; else IsBlack=true; this.getGraphics().clearRect(0,0,5*ImageCol,5*ImageRow); this.getGraphics().clearRect(250,100,250+1*ImageCol,100+1*ImageRow); } //清空操作 else if (x>53 && x<77 && y>104 && y<128 ) { for (int i=0;i<15;i++) { for (int j=0;j<75;j++) { IsBlack=true; Data[i][j]=0; } } IsBian=false; this.getGraphics().clearRect(5,5,5*ImageCol,5*ImageRow); this.getGraphics().clearRect(250,110,ImageCol,ImageRow); } else if (x>77 && x<101 && y>104 && y<128 ) { tf_Str.requestFocus(); tf_Str.selectAll(); tf_Str.setEnabled(true); Str_Command="文字"; IsBian=false; } else if (x>101 && x<125 && y>104 && y<128 ) { int tempData; for (int i=0;i<29;i++) { for (int j=0;j<99;j++) { tempData=Data[i][j]; Data[i][j]=OldData[i][j]; OldData[i][j]=tempData; } } if (IsBian==true) { if (IsBlack==true) IsBlack=false; else IsBlack=true; IsBian=false; } } else if (x>125 && x<149 && y>104 && y<128 ) { Str_Command="橡皮"; IsBian=false; } if (Str_Command!="文字") tf_Str.setEnabled(false); repaint(); } public void mouseReleased(MouseEvent e) { test = e.getPoint(); rowendindex=test.y/5; colendindex=test.x/5; //利用双缓冲 this.setBackground(Color.blue); Image offscreenimage=createImage(72,14);//创建屏外绘图区 Graphics offscreen=offscreenimage.getGraphics();//取得绘图环境 offscreen.setColor(Color.green); if (Str_Command=="直线") offscreen.drawLine((colstartindex-1),(rowstartindex-1),(colendindex-1),(rowendindex-1)); else if (Str_Command=="矩形") offscreen.drawRect((colstartindex-1),(rowstartindex-1), colendindex-colstartindex,rowendindex-rowstartindex); else if (Str_Command=="椭圆") offscreen.drawOval((colstartindex-1),(rowstartindex-1), colendindex-colstartindex,rowendindex-rowstartindex); else if (Str_Command=="填充矩形") offscreen.fillRect((colstartindex-1),(rowstartindex-1), colendindex-colstartindex,rowendindex-rowstartindex); if (Str_Command=="填充椭圆") offscreen.fillOval((colstartindex-1),(rowstartindex-1), colendindex-colstartindex,rowendindex-rowstartindex); handlepixels(offscreenimage,0,0,72,14); this.setBackground(new Color(0,190,255)); repaint(); tempcololdindex=0; temprowoldindex=0; } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void update(Graphics g) { g.clearRect(5*(colindex+1),5*(rowindex+1),5,5); g.clearRect(250+colindex,110+rowindex,1,1); paint(g); } public void mouseClicked(MouseEvent e) {/* int x,y; int Size=10; //取反操作 test = e.getPoint(); rowindex=test.y/5-1; colindex=test.x/5-1; if (Str_Command=="自绘") { if (Data[rowindex][colindex]==0) Data[rowindex][colindex]=1; else if (Data[rowindex][colindex]==1) Data[rowindex][colindex]=0; } else if (Str_Command=="橡皮") { Data[rowindex][colindex]=0; } //获得鼠标位置 x=e.getX(); y=e.getY(); //绘制文字 if ((Str_Command=="文字") && (y<70)) { this.setBackground(Color.blue); Image offscreenimage=createImage(72,14);//创建屏外绘图区 Graphics offscreen=offscreenimage.getGraphics();//取得绘图环境 switch(choice2.getSelectedIndex()) { case 0: Size=12; break; case 1: Size=10; break; case 2: Size=11; break; case 3: Size=13; break; } offscreen.setFont(new Font("宋体",Font.PLAIN,Size)); offscreen.setColor(Color.green); String Str_temp=tf_Str.getText(); offscreen.drawString(Str_temp,colindex,rowindex+5); handlepixels(offscreenimage,0,0,72,14); this.setBackground(new Color(0,190,255)); } //设置指示字 if (x>5 && x<29 && y>80 && y<104) { Str_Command="自绘"; IsBian=false;} else if (x>29 && x<53 && y>80 && y<104 ) { Str_Command="直线"; IsBian=false;} else if (x>53 && x<77 && y>80 && y<104 ) { Str_Command="矩形"; IsBian=false;} else if (x>77 && x<101 && y>80 && y<104 ) { Str_Command="填充矩形"; IsBian=false;} else if (x>101 && x<125 && y>80 && y<104 ) { Str_Command="椭圆"; IsBian=false; } else if (x>5 && x<29 && y>104 && y<128 ) { Str_Command="填充椭圆"; IsBian=false; } //取反操作 else if (x>29 && x<53 && y>104 && y<128 ) { for (int i=0;i<15;i++) { for (int j=0;j<75;j++) { if (Data[i][j]==1) Data[i][j]=0; else Data[i][j]=1; } } IsBian=true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -