java 画板Word下载.docx
- 文档编号:18730520
- 上传时间:2022-12-31
- 格式:DOCX
- 页数:12
- 大小:17.23KB
java 画板Word下载.docx
《java 画板Word下载.docx》由会员分享,可在线阅读,更多相关《java 画板Word下载.docx(12页珍藏版)》请在冰豆网上搜索。
JMenufile=newJMenu("
文件"
);
JLabelappearance=newJLabel("
形状:
JLabel.RIGHT);
JMenuabout=newJMenu("
关于"
JMenuItemopen=newJMenuItem("
打开"
JMenuItemsave=newJMenuItem("
保存"
JMenuItemoff=newJMenuItem("
退出"
JButtoncurve=newJButton("
曲线"
JButtonline=newJButton("
直线"
JButtoncircle=newJButton("
椭圆"
JButtonrectangle=newJButton("
矩形"
JFileChooserchooser=newJFileChooser();
staticStringiniPicName="
.\\ini.jpg"
;
staticFilepic=newFile(iniPicName);
staticintappChooser=0;
staticintkey=0;
staticintfillKey=0;
staticintserialNum=0;
staticStringfileName="
privateStringimageFormat="
jpg"
Dimensiond=Toolkit.getDefaultToolkit().getScreenSize();
privatemyLabelboard=newmyLabel();
privateLabeldrawWidth=null;
privateLabeldrawCap=null;
privateButtonColorActionbuttonColorAction=null;
privateButtonCursorbuttonCursor=null;
publicmyPaint(){
super("
标题"
this.setJMenuBar(mb);
mb.add(file);
mb.add(appearance);
file.add(open);
file.add(save);
file.add(off);
appearance.add(curve);
appearance.add(line);
appearance.add(circle);
appearance.add(rectangle);
open.addActionListener(this);
save.addActionListener(this);
off.addActionListener(this);
curve.addActionListener(this);
line.addActionListener(this);
rectangle.addActionListener(this);
circle.addActionListener(this);
for(inti=0;
i<
9;
i++){
cb.addItem(size[i]);
}
cb.setBackground(Color.white);
cb.addItemListener(this);
this.add(board);
this.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
setExtendedState(Frame.MAXIMIZED_BOTH);
setLocation(15,15);
Panelpane=newPanel(newGridLayout(2,1));
PanelpaneColor=newPanel(newGridLayout(1,13));
Button[]buttonColor=newButton[12];
Color[]color={Color.black,Color.blue,Color.cyan,Color.darkGray,
Color.gray,Color.green,Color.magenta,Color.orange,
Color.pink,Color.red,
Color.white,Color.yellow};
paneColor.add(colorChooser);
buttonColorAction=newButtonColorAction();
buttonCursor=newButtonCursor();
buttonColor.length;
i++){
buttonColor[i]=newButton();
buttonColor[i].setBackground(color[i]);
buttonColor[i].addActionListener(buttonColorAction);
buttonColor[i].addMouseListener(buttonCursor);
paneColor.add(buttonColor[i]);
Panelappearance_pane=newPanel(newGridLayout(1,7));
drawWidth=newLabel("
Label.CENTER);
drawCap=newLabel("
●"
drawWidth.setBackground(Color.lightGray);
drawCap.setBackground(Color.lightGray);
appearance_pane.add(lb);
appearance_pane.add(cb);
appearance_pane.add(appearance);
appearance_pane.add(curve);
appearance_pane.add(line);
appearance_pane.add(rectangle);
appearance_pane.add(circle);
pane.add(appearance_pane);
pane.add(paneColor);
this.add(pane,BorderLayout.SOUTH);
this.setSize(1300,700);
}
publicstaticvoidmain(Stringarg[]){
newmyPaint().setVisible(true);
publicvoidactionPerformed(ActionEventarg0){
//TODOAuto-generatedmethodstub
ObjecteventSource=arg0.getSource();
if(eventSource==open){
chooser.setDialogTitle("
打开文件"
chooser.setApproveButtonText("
chooser.showOpenDialog(this);
Filefile=chooser.getSelectedFile();
if(file!
=null){
pic=file;
try{
myLabel.image=ImageIO.read(newFileInputStream(myPaint.pic));
fileName=pic.toString();
repaint();
}catch(IOExceptione){
System.out.println("
Error"
}
}elseif(eventSource==save){
try{
BufferedImagescreenshot=(newRobot()).createScreenCapture(new
Rectangle(0,0,(int)d.getWidth(),(int)d.getHeight()));
serialNum++;
Stringname=fileName+String.valueOf(serialNum)+"
."
+imageFormat;
Filef=newFile(name);
System.out.print("
SaveFile"
+name);
ImageIO.write(screenshot,imageFormat,f);
..Finished!
\n"
}
catch(Exceptionex){
System.out.println(ex);
}elseif(eventSource==off){
System.exit(0);
}elseif(eventSource==line){
appChooser=1;
key=1;
}elseif(eventSource==rectangle){
appChooser=2;
}elseif(eventSource==circle){
appChooser=3;
}elseif(eventSource==curve){
appChooser=0;
key=0;
classButtonColorActionimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
Colorcolor_temp=((Button)e.getSource()).getBackground();
board.setColor(color_temp);
classButtonCursorextendsMouseAdapter{
publicvoidmouseEntered(MouseEvente){
((Button)e.getSource()).setCursor(newCursor(Cursor.HAND_CURSOR));
publicvoidmouseExited(MouseEvente){
((Button)e.getSource())
.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
publicvoiditemStateChanged(ItemEvente){
if(e.getSource()==cb){
inti=0;
Stringstr=(String)e.getItem();
for(intj=0;
j<
j++)
if(str==size[j])
i=cb.getSelectedIndex();
board.setStroke(newBasicStroke(i+2));
}
classmyLabelextendsJLabelimplementsMouseListener,
MouseMotionListener{
privateintmouseX=0;
privateintoldMouseX=0;
privateintmouseY=0;
privateintoldMouseY=0;
privateintnewMouseX=0;
privateintnewMouseY=0;
privateinttemMouseX=0;
privateinttemMouseY=0;
privateintwidth=0;
privateintheight=0;
privateColorcolor=null;
privateBasicStrokestroke=null;
staticBufferedImageimage=null;
publicmyLabel(){
this.addMouseListener(this);
this.addMouseMotionListener(this);
color=newColor(0,0,0);
stroke=newBasicStroke(3.0f,BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
image=newBufferedImage(1300,1024,
BufferedImage.TYPE_INT_RGB);
image.getGraphics().setColor(Color.white);
image.getGraphics().fillRect(0,0,1280,1024);
try{
image=ImageIO.read(newFileInputStream(myPaint.iniPicName));
}catch(IOExceptione){
System.out.println("
/**
*重写paint绘图方法
*/
publicvoidpaint(Graphicsg){
Graphics2Dg2d=(Graphics2D)g;
Graphics2Dbg=image.createGraphics();
bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
bg.setColor(color);
bg.setStroke(stroke);
if((newMouseX-temMouseX)>
=0){
width=newMouseX-temMouseX;
if((newMouseY-temMouseY)>
=0)
height=newMouseY-temMouseY;
else{
height=temMouseY-newMouseY;
if(myPaint.appChooser!
=1)
temMouseY=newMouseY;
}else{
width=temMouseX-newMouseX;
temMouseX=newMouseX;
}else{
=1){
if(myPaint.key==0){
if(myPaint.appChooser==0)
bg.drawLine(oldMouseX,oldMouseY,mouseX,mouseY);
if(myPaint.appChooser==1){
bg.drawLine(temMouseX,temMouseY,newMouseX,newMouseY);
if(myPaint.appChooser==2){
bg.drawRect(temMouseX,temMouseY,width,height);
if(myPaint.appChooser==3){
bg.drawOval(temMouseX,temMouseY,width,height);
g2d.drawImage(image,0,0,this);
*重写update方法
publicvoidupdate(Graphicsg){
this.paint(g);
publicBasicStrokegetStroke(){
returnstroke;
publicvoidsetStroke(BasicStrokestroke){
this.stroke=stroke;
publicvoidsetStroke(floatwidth){
this.stroke=newBasicStroke(width,stroke.getEndCap(),
stroke.getLineJoin());
publicvoidsetStroke(intcap){
this.stroke=newBasicStroke(stroke.getLineWidth(),cap,
publicColorgetColor(){
returncolor;
publicvoidsetColor(Colorcolor){
this.color=color;
publicvoidmouseClicked(MouseEventmouseEvent){
publicvoidmousePressed(MouseEventmouseEvent){
this.oldMouseX=this.mouseX=mouseEvent.getX();
this.oldMouseY=this.mouseY=mouseEvent.getY();
if(myPaint.appChooser!
temMouseX=mouseX;
temMouseY=mouseY;
if(myPaint.appChooser==0&
&
myPaint.key==0){
repaint();
publicvoidmouseReleased(MouseEventmouseEvent){
newMouseX=mouseX;
newMouseY=mouseY;
myPaint.key=0;
publicvoidmouseEntered(MouseEventmouseEvent){
this.setCursor(newCursor(Cursor.CROSSHAIR_CURSOR));
publicvoidmouseExited(MouseEventmouseEvent){
this.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
publicvoidmouseDragged(MouseEventmouseEvent){
this.oldMouseX=this.mouseX;
this.oldMouseY=this.mouseY;
this.mouse
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 画板