資源簡介
做一個畫筆小程序。
基本功能要求:
1.這個畫圖程序,最基本的功能是,作為一個畫筆,能利用它實現(xiàn)在屏幕上任意繪畫、書寫的功能;
2.利用這個畫圖程序,通過選擇畫直線、畫圓形、畫矩形等功能選項并且在屏幕上拖動鼠標操作能實現(xiàn)畫出相應(yīng)圖形的功能;
3.這個畫圖程序,還應(yīng)該做到能夠改變畫筆顏色的功能;
4.這個畫圖程序,還應(yīng)該具備能夠清除已繪制圖畫的功能。

代碼片段和文件信息
package?lab5;
import?java.awt.*;
import?java.awt.event.*;?
import?java.awt.geom.*;?
import?javax.swing.*;
import?java.util.*;
import?java.io.*;
import?java.awt.image.BufferedImage;
import?javax.imageio.ImageIO;
class?MyShape{
Shape?s;
boolean?fi=false;
?Color?c;
}
public?class?DrawPad?extends?Jframe?implements?ActionListener{
public?Color?pc;
JButton?penlinerectellipwordchcolorbackgrclearholdopen;
Choice?f;
boolean?fill;
public?DrawPanel?drp;
public?int?cho;
public?int[]?xy;
DrawPad(){
cho=0;
x=new?int[2];
y=new?int[2];
for(int?i=0;i<2;i++){
x[i]=0;
y[i]=0;
}
fill=false;
this.settitle(“JAVA畫圖本“);
pc=new?Color(0?0?0);
pen=new?JButton(“畫筆“);
line=new?JButton(“直線“);
rect=new?JButton(“矩形“);
ellip=new?JButton(“橢圓“);
word=new?JButton(“文字“);
f=new?Choice();
f.add(“不填充“);
f.add(“填充“);
chcolor=new?JButton(“選擇顏色“);
backgr=new?JButton(“設(shè)置背景顏色“);
clear=new?JButton(“清空“);
hold=new?JButton(“保存“);
open=new?JButton(“打開“);
drp=new?DrawPanel();
drp.setBackground(Color.WHITE);
JPanel?jp=new?JPanel();
jp.setLayout(new?GridLayout(111));
jp.add(drp);
jp.add(pen);
jp.add(line);
jp.add(rect);
jp.add(ellip);
jp.add(word);
jp.add(f);
jp.add(chcolor);
jp.add(backgr);
jp.add(clear);
jp.add(hold);
jp.add(open);
this.setLayout(new?BorderLayout());
this.add(jpBorderLayout.WEST);
this.add(drpBorderLayout.CENTER);
drp.addMouseMotionListener(drp);
drp.addMouseListener(drp);
pen.addActionListener(this);
line.addActionListener(this);
rect.addActionListener(this);
ellip.addActionListener(this);
word.addActionListener(this);
f.addItemListener(new?ItemListener(){
@Override
public?void?itemStateChanged(ItemEvent?arg0)?{
if((String)arg0.getItem()==“填充“)
fill=true;
else
fill=false;
}
});
chcolor.addActionListener(this);
backgr.addActionListener(this);
clear.addActionListener(this);
hold.addActionListener(this);
open.addActionListener(this);
}
class?DrawPanel?extends?JPanel?implements?MouseListener?MouseMotionListener?{
DrawPanel(){super();}
ArrayList?Shapes=new?ArrayList();
boolean?released;
public?void?paintComponent(Graphics?g)?{
super.paintComponent(g);
Graphics2D?g2d?=?(Graphics2D)?g;
BasicStroke?size?=?new?BasicStroke(2?BasicStroke.CAP_BUTTBasicStroke.JOIN_BEVEL);
g2d.setStroke(size);
g2d.setColor(pc);
MyShape?s=new?MyShape();
s.c=pc;
s.fi=fill;
switch?(cho)?{
case?1:
Rectangle?r=new?Rectangle();
r.setBounds(x[0]?y[0]11);
s.s=r;
Shapes.add(s);
break;
case?2:
Line2D?line2?=?new?Line2D.Double(x[0]?y[0]?x[1]?y[1]);
g2d.draw(line2);
s.s=line2;
if(released){Shapes.add(s);}
break;
case?3:
Rectangle2D?rect=null;
if(y[1]>=y[0]&&x[1]>=x[0])
rect?=?new?Rectangle2D.Double(x[0]?y[0]
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-12-22?20:01??Paint\
?????文件???????15267??2016-11-25?16:22??Paint.docx
?????文件?????????301??2015-12-22?20:01??Paint\.classpath
?????文件?????????381??2015-12-22?20:01??Paint\.project
?????目錄???????????0??2015-12-22?20:01??Paint\.settings\
?????文件?????????598??2015-12-22?20:01??Paint\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2015-12-22?20:02??Paint\bin\
?????目錄???????????0??2015-12-24?19:44??Paint\bin\lab5\
?????文件?????????796??2015-12-24?19:50??Paint\bin\lab5\DrawPad$1.class
?????文件????????5258??2015-12-24?19:50??Paint\bin\lab5\DrawPad$DrawPanel.class
?????文件????????6395??2015-12-24?19:50??Paint\bin\lab5\DrawPad.class
?????文件?????????358??2015-12-24?19:50??Paint\bin\lab5\MyShape.class
?????目錄???????????0??2015-12-22?20:02??Paint\src\
?????目錄???????????0??2015-12-22?20:58??Paint\src\lab5\
?????文件????????7617??2015-12-24?19:50??Paint\src\lab5\DrawPad.java
- 上一篇:JAVA-CD租售店
- 下一篇:Socket通信Des加密筆記
評論
共有 條評論