1 //ubuntu windows endl2 3 importjava.awt.*;4 importjava.awt.event.*;5 importjava.awt.image.*;6 importjava.io.*;7 importjavax.swing.*;8 importjavax.imageio.*;9 10 classPainterCanvasextendsCanvas {11 12 publicPainterCanvas( Color foregroundColor,intinitPaintType,floatlineWidth,float[] dashArray,floatdashPhase, Dimension size ) {13 setColor( foregroundColor );14 setBackground( COLOR_BACK );15 paintType=LINE;16 setPaintType( initPaintType );17 paintStroke=newBasicStroke();18 setLineWidth( lineWidth );19 setLineDash( dashArray, dashPhase );20 setBufferedDimension( size );21 startX=startY=endX=endY=0;22 drawBuffer=false;23 pressed=false;24 setCursor( Cursor.getPredefinedCursor( Cursor.CROSSHAIR_CURSOR ) );25 addMouseListener(new26 MouseAdapter() {27 publicvoidmousePressed( MouseEvent me ) {28 if(!pressed ) {29 startX=me.getX();30 startY=me.getY();31 pressed=true;32 }33 }34 publicvoidmouseReleased( MouseEvent me ) {35 if( pressed ) {36 endX=me.getX();37 endY=me.getY();38 pressed=false;39 drawBuffer=true;40 repaint();41 }42 }43 }44 );45 addMouseMotionListener(newMouseMotionAdapter() {46 publicvoidmouseDragged( MouseEvent me ) {47 endX=me.getX();48 endY=me.getY();49 repaint();50 }51 } );52 }53 54 publicvoidsetPaintType(intt ) {55 if( ( t==LINE )||( t==TRIA )||( t==OVAL )||( t==RECT ) ) {56 paintType=t;57 }58 }59 60 publicvoidsetColor( Color col ) {61 colorFore=col;62 }63 64 publicvoidsetLineWidth(floatlineWidth ) {65 if( lineWidth>0.0f) {66 paintStroke=newBasicStroke( lineWidth, paintStroke.getEndCap(),67 paintStroke.getLineJoin(), paintStroke.getMiterLimit(),68 paintStroke.getDashArray(), paintStroke.getDashPhase() );69 }70 }71 72 publicvoidsetLineDash(float[] dashArray,floatdashPhase ) {73 paintStroke=newBasicStroke( paintStroke.getLineWidth(), paintStroke.getEndCap(),74 paintStroke.getLineJoin(), paintStroke.getMiterLimit(),75 dashArray, dashPhase );76 }77 78 publicvoidsetBufferedDimension( Dimension size ) {79 setSize( size );80 dimSize=size;81 img=newBufferedImage( dimSize.width, dimSize.height, BufferedImage.TYPE_4BYTE_ABGR );82 graImg=img.createGraphics();83 graImg.setColor( COLOR_BACK );84 graImg.fillRect(0,0, dimSize.width, dimSize.height );85 }86 87 publicvoidsetBufferedImage( BufferedImage bufImg ) {88 dimSize.width=bufImg.getWidth();89 dimSize.height=bufImg.getHeight();90 setSize( dimSize );91 img=bufImg;92 graImg=img.createGraphics();93 }94 95 publicColor getColor() {96 returncolorFore;97 }98 99 publicBufferedImage getBufferedImage() {100 returnimg;101 }102 103 publicvoidpaint( Graphics g ) {104 g.drawImage( img,0,0, dimSize.width, dimSize.height,this);105 paintUpdate( g );106 }107 108 publicvoidupdate( Graphics g ) {109 g.drawImage( img,0,0, dimSize.width, dimSize.height,this);110 paintUpdate( g );111 }112 113 publicvoidpaintUpdate( Graphics gra ) {114 Graphics2D g=(Graphics2D)gra;115 g.setColor( colorFore );116 g.setStroke( paintStroke );117 graImg.setColor( colorFore );118 graImg.setStroke( paintStroke );119 intx=Math.min( startX, endX );120 inty=Math.min( startY, endY );121 intw=startX+endX-x-x;122 inth=startY+endY-y-y;123 switch( paintType ) {124 caseLINE :125 g.drawLine( startX, startY, endX, endY );126 if( drawBuffer ) {127 graImg.drawLine( startX, startY, endX, endY );128 }129 break;130 caseTRIA :131 g.drawPolygon(newint[]{ startX, x, endX },newint[]{ startY, ( startY+endY )/2, endY },3);132 if( drawBuffer ) {133 graImg.drawPolygon(newint[]{ startX, x, endX },newint[]{ startY, ( startY+endY )/2, endY },3);134 }135 break;136 caseOVAL :137 g.drawOval( x, y, w, h );138 if( drawBuffer ) {139 graImg.drawOval( x, y, w, h );140 }141 break;142 caseRECT :143 g.drawRect( x, y, w, h );144 if( drawBuffer ) {145 graImg.drawRect( x, y, w, h );146 }147 break;148 }149 drawBuffer=false;150 }151 152 privateintstartX, startY, endX, endY, paintType;153 privateColor colorFore;154 privatebooleanpressed, drawBuffer;155 privateBasicStroke paintStroke;156 privateGraphics2D graImg;157 privateBufferedImage img;158 privateDimension dimSize;159 160 privatestaticfinalColor COLOR_BACK=Color.WHITE;161 publicstaticfinalintLINE=0, TRIA=1, OVAL=2, RECT=3;162 163 }164 165 166 //------------------------------------------------------------------------------------------167 168 169 classPainterWinextendsFrameimplementsActionListener, ItemListener {170 171 publicPainterWin() {172 super("画板 --- 赵杰");173 initFrame();174 initMenu();175 initTool();176 initCanvas();177 setVisible(true);178 validate();179 }180 181 publicvoidactionPerformed( ActionEvent ae ) {182 Object obj=ae.getSource();183 if( obj==menuNew ) {184 onMenuNew();185 }186 elseif( obj==menuOpen ) {187 onMenuOpen();188 }189 elseif( obj==menuSave ) {190 onMenuSave();191 }192 elseif( obj==menuExit ) {193 onMenuExit();194 }195 elseif( obj==butLine ) {196 197 canvas.setPaintType( PainterCanvas.LINE );198 }199 elseif( obj==butTria ) {200 canvas.setPaintType( PainterCanvas.TRIA );201 }202 elseif( obj==butOval ) {203 canvas.setPaintType( PainterCanvas.OVAL );204 }205 elseif( obj==butRect ) {206 canvas.setPaintType( PainterCanvas.RECT );207 }208 }209 210 publicvoiditemStateChanged( ItemEvent ie ) {211 Object obj=ie.getSource();212 if( obj==choLineType ) {213 onChoiceLineType();214 }215 elseif( obj==choLineWidth ) {216 onChoiceLineWidth();217 }218 elseif( obj==choColor ) {219 onChoiceColor();220 }221 }222 223 privatevoidinitFrame() {224 setBounds(300,200,600,500);225 addWindowListener(newWindowAdapter() {226 publicvoidwindowClosing( WindowEvent we ) {227 onExit();228 }229 }230 );231 setLayout(newBorderLayout() );//default232 }233 234 privateMenuItem newMenuItem( Menu menu, String label, MenuShortcut sc ) {235 MenuItem item=newMenuItem( label, sc );236 item.addActionListener(this);237 menu.add( item );238 returnitem;239 }240 241 privatevoidinitMenu() {242 MenuBar menuBar=newMenuBar();243 Menu menu=newMenu("文件");244 menuNew=newMenuItem( menu,"新建",newMenuShortcut( KeyEvent.VK_N ) );245 menuOpen=newMenuItem( menu,"打开",newMenuShortcut( KeyEvent.VK_O ) );246 menuSave=newMenuItem( menu,"保存",newMenuShortcut( KeyEvent.VK_S ) );247 menuExit=newMenuItem( menu,"退出",newMenuShortcut( KeyEvent.VK_E ) );248 menuBar.add( menu );249 setMenuBar( menuBar );250 }251 252 privateChoice newChoice( Panel pan, String label ) {253 Choice cho=newChoice();254 cho.addItemListener(this);255 pan.add(newLabel( label ) );256 pan.add( cho );257 returncho;258 }259 260 privateButton newButton( Panel pan, String label ) {261 Button but=newButton( label );262 but.addActionListener(this);263 pan.add( but );264 returnbut;265 }266 267 privatevoidinitTool() {268 Panel pan=newPanel();269 pan.setLayout(newFlowLayout( FlowLayout.CENTER,10,1) );270 271 choLineType=newChoice( pan,"线条");272 choLineType.add("实线");273 choLineType.add("虚线");274 choLineType.select(0);275 276 choLineWidth=newChoice( pan,"线宽");277 choLineWidth.add("1");278 choLineWidth.add("4");279 choLineWidth.select(0);280 281 choColor=newChoice( pan,"颜色");282 choColor.add("黑");283 choColor.add("红");284 choColor.add("自定义");285 choColor.select(0);286 287 butLine=newButton( pan,"直线");288 butTria=newButton( pan,"三角形");289 butOval=newButton( pan,"圆");290 butRect=newButton( pan,"矩形");291 292 add( pan, BorderLayout.NORTH );293 }294 295 privatevoidinitCanvas() {296 float[] dashArray=newfloat[]{1.0f,0.0f};297 canvas=newPainterCanvas( Color.BLACK, PainterCanvas.LINE,1.0f, dashArray,0.0f,newDimension(600,400) );298 pl=newPanel(null);299 pl.add( canvas );300 canvas.setBounds(0,0, canvas.getSize().width, canvas.getSize().height );301 pl.setBounds(0,0, canvas.getSize().width, canvas.getSize().height );302 if( pane!=null) {303 remove( pane );304 }305 pane=newJScrollPane( pl );306 add( pane, BorderLayout.CENTER );307 }308 309 privatevoidonMenuNew() {310 if( onLeaveButCancel() ) {311 return;312 }313 initCanvas();314 validate();315 }316 317 privatevoidonMenuOpen() {318 if( onLeaveButCancel() ) {319 return;320 }321 JFileChooser fc=newJFileChooser();322 intcho=fc.showOpenDialog(this);323 if( cho==JFileChooser.APPROVE_OPTION ) {324 file=fc.getSelectedFile();325 BufferedImage img;326 try{327 img=ImageIO.read( file );328 }329 catch( IOException ex ) {330 JOptionPane.showMessageDialog(this,"图像载入失败!");331 file=null;332 return;333 }334 pl.setSize( img.getWidth(), img.getHeight() );335 canvas.setBufferedImage( img );336 file=null;337 }338 }339 340 privatebooleanonLeaveButCancel() {341 intcho=JOptionPane.showConfirmDialog(this,"保存当前图片?");342 switch( cho ) {343 caseJOptionPane.YES_OPTION :344 onSave();345 caseJOptionPane.NO_OPTION :346 file=null;347 returnfalse;348 }349 returntrue;350 }351 352 privatevoidonMenuSave() {353 onSave();354 }355 356 privatevoidonSave() {357 if( file==null) {358 JFileChooser fc=newJFileChooser();359 intcho=fc.showSaveDialog(this);360 if( cho==JFileChooser.APPROVE_OPTION ) {361 file=fc.getSelectedFile();362 }363 }364 if( file!=null) {365 try{366 ImageIO.write( canvas.getBufferedImage(),"PNG", file );367 }368 catch( IOException ex ) {369 JOptionPane.showMessageDialog(this,"保存失败!");370 }371 }372 }373 374 privatevoidonMenuExit() {375 onExit();376 }377 378 privatevoidonExit() {379 if(!onLeaveButCancel() ) {380 System.exit(0);381 }382 }383 384 privatevoidonChoiceLineType() {385 float[] da;386 switch( choLineType.getSelectedIndex() ) {387 case0:388 da=newfloat[]{1.0f,0.0f};389 canvas.setLineDash( da,0.0f);390 break;391 case1:392 da=newfloat[]{4.0f,8.0f};393 canvas.setLineDash( da,0.0f);394 break;395 }396 }397 398 privatevoidonChoiceLineWidth() {399 switch( choLineWidth.getSelectedIndex() ) {400 case0:401 canvas.setLineWidth(1.0f);402 break;403 case1:404 canvas.setLineWidth(4.0f);405 break;406 }407 }408 409 privatevoidonChoiceColor() {410 switch( choColor.getSelectedIndex() ) {411 case0:412 canvas.setColor( Color.BLACK );413 break;414 case1:415 canvas.setColor( Color.RED );416 break;417 case2:418 canvas.setColor( JColorChooser.showDialog(this,"自定义颜色", canvas.getColor() ) );419 break;420 }421 }422 423 privateMenuItem menuNew, menuOpen, menuSave, menuExit;424 privateChoice choLineType, choLineWidth, choColor;425 privateButton butLine, butTria, butOval, butRect;426 privatePainterCanvas canvas;427 privatePanel pl;428 privateJScrollPane pane=null;429 privateFile file=null;430 431 }432 433 434 //------------------------------------------------------------------------------------------435 436 437 publicclassPainter {438 439 publicstaticvoidmain( String [] args ) {440 newPainterWin();441 }442 443 }444 445
java优米乐园_画板 Java实现
最新推荐文章于 2022-10-17 15:15:52 发布