画笔JavaWord文档下载推荐.docx
《画笔JavaWord文档下载推荐.docx》由会员分享,可在线阅读,更多相关《画笔JavaWord文档下载推荐.docx(12页珍藏版)》请在冰豆网上搜索。
this.y2=y2;
}
publicvoidsetColor(Colorc){
this.color=c;
publicabstractvoiddraw(Graphicsg);
PaintingBrush_Line.java文件
publicclassPaintingBrush_LineextendsPaintingBrush_Shape{
publicPaintingBrush_Line(){
publicPaintingBrush_Line(intx1,inty1,intx2,inty2){
setShape(x1,y1,x2,y2);
@Override
publicvoiddraw(Graphicsg){
g.setColor(color);
g.drawLine(x1,y1,x2,y2);
PaintingBrush_Rectangle.java文件
publicclassPaintingBrush_RectangleextendsPaintingBrush_Shape{
publicPaintingBrush_Rectangle(){
publicPaintingBrush_Rectangle(intx1,inty1,intx2,inty2){
//实现任意方向的绘制效果,否则直接绘制就只能支持向右下方的拖拽绘制
intnewX=Math.min(x1,x2);
intnewY=Math.min(y1,y2);
intnewWidth=Math.abs(x2-x1);
intnewHeight=Math.abs(y2-y1);
g.drawRect(newX,newY,newWidth,newHeight);
PaintingBrush_Circle.java文件
publicclassPaintingBrush_CircleextendsPaintingBrush_Shape{
publicPaintingBrush_Circle(){
publicPaintingBrush_Circle(intx1,inty1,intx2,inty2){
intnewWidth=Math.abs(x1-x2);
intnewHeight=Math.abs(y1-y2);
g.drawOval(newX,newY,newWidth,newHeight);
PaintingBrush_Triangle.java文件
publicclassPaintingBrush_TriangleextendsPaintingBrush_Shape{
publicPaintingBrush_Triangle(){
publicPaintingBrush_Triangle(intx1,inty1,intx2,inty2){
g.drawLine(x1+(x2-x1)/2,y1,x1,y2);
g.drawLine(x1+(x2-x1)/2,y1,x2,y2);
g.drawLine(x1,y2,x2,y2);
PainttingBrush.java文件
importjava.awt.BorderLayout;
importjava.awt.FlowLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.ItemEvent;
importjava.awt.event.ItemListener;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.awt.event.MouseMotionListener;
importjava.awt.image.BufferedImage;
importjavax.swing.ButtonGroup;
importjavax.swing.JFrame;
importjavax.swing.JMenu;
importjavax.swing.JMenuBar;
importjavax.swing.JMenuItem;
importjavax.swing.JPanel;
importjavax.swing.JRadioButton;
publicclassPainttingBrush{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
DemoWindow2dw=newDemoWindow2("
画笔"
);
dw.setBounds(dw.getToolkit().getScreenSize().width/4,dw.getToolkit()
.getScreenSize().height/4,
dw.getToolkit().getScreenSize().width/2,dw.getToolkit()
.getScreenSize().height/2);
dw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dw.setVisible(true);
classDemoWindow2extendsJFrameimplementsActionListener{
JMenuBarmenuBar=newJMenuBar();
JMenumenu=newJMenu("
文件"
JMenuItemmenuItem1=newJMenuItem("
打开文件"
JMenuItemmenuItem2=newJMenuItem("
保存文件"
DrawPaneldp=newDrawPanel();
ControlPanelcp=newControlPanel(dp);
publicDemoWindow2(Stringtitle){
super(title);
menu.add(menuItem1);
menu.add(menuItem2);
menuBar.add(menu);
setJMenuBar(menuBar);
menuItem1.addActionListener(this);
menuItem2.addActionListener(this);
add(dp,BorderLayout.CENTER);
add(cp,BorderLayout.SOUTH);
publicvoidactionPerformed(ActionEvente){
JMenuItemmenuItem=(JMenuItem)e.getSource();
if(menuItem==menuItem1){
}elseif(menuItem==menuItem2){
}
classDrawPanelextendsJPanelimplementsMouseListener,MouseMotionListener{
//起始位置
intx1;
inty1;
//终止位置
intx2;
inty2;
//存储上一次终止坐标的临时变量
intoldx2;
intoldy2;
//缓冲图像
BufferedImageim=newBufferedImage(1000,1000,BufferedImage.TYPE_INT_RGB);
PaintingBrush_IShapeshape=newPaintingBrush_Line();
//当前默认的前景色
Colorcolor=Color.green;
publicDrawPanel(){
//设置背景色
setBackground(Color.yellow);
//获取缓冲图像的绘图类型
Graphicsuig=im.createGraphics();
//使用白色填充缓冲图像的背景
uig.setColor(Color.gray);
uig.fillRect(0,0,1000,1000);
//添加事件监听器
addMouseListener(this);
addMouseMotionListener(this);
publicvoidmouseDragged(MouseEvente){
oldx2=x2;
oldy2=y2;
//获取新的终止坐标
x2=e.getX();
y2=e.getY();
//获取缓冲图像的绘图类变量
//设置异或绘图模式
uig.setXORMode(Color.white);
//在缓冲图像上绘制形状
//擦除上一次位置的绘制形状
shape.setColor(color);
shape.setShape(x1,y1,oldx2,oldy2);
shape.draw(uig);
//绘制新位置的形状
shape.setShape(x1,y1,x2,y2);
repaint();
publicvoidmouseMoved(MouseEvente){
publicvoidmouseClicked(MouseEvente){
publicvoidmousePressed(MouseEvente){
//设置3种坐标的初始值相等
//获取起始坐标
x1=e.getX();
y1=e.getY();
x2=x1;
y2=y1;
publicvoidmouseReleased(MouseEvente){
//重绘当前面板,显示所绘的缓冲图像
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
if(im!
=null){
g.drawImage(im,0,0,this);
publicvoidmouseEntered(MouseEvente){
publicvoidmouseExited(MouseEvente){
classControlPanelextendsJPanel{
//重载构造函数,添加颜色选择面板和形状选择面板
publicControlPanel(DrawPaneldp){
setLayout(newGridLayout(2,1));
add(newColorPanel(dp));
add(newShapePanel(dp));
//颜色选择面板类
classColorPanelextendsJPanelimplementsItemListener{
DrawPaneldp;
JRadioButtonjrb1=newJRadioButton("
红色"
JRadioButtonjrb2=newJRadioButton("
绿色"
JRadioButtonjrb3=newJRadioButton("
蓝色"
ButtonGroupbg=newButtonGroup();
publicColorPanel(DrawPaneldp){
//设置绘图类面板的引用
this.dp=dp;
bg.add(jrb1);
bg.add(jrb2);
bg.add(jrb3);
//设置单选按钮的默认选择状体
jrb2.setSelected(true);
setLayout(newFlowLayout());
add(jrb1);
add(jrb2);
add(jrb3);
jrb1.addItemListener(this);
jrb2.addItemListener(this);
jrb3.addItemListener(this);
publicvoiditemStateChanged(ItemEvente){
if(jrb1==e.getSource()){
dp.color=Color.red;
}elseif(jrb2==e.getSource()){
dp.color=Color.green;
}elseif(jrb3==e.getSource()){
dp.color=Color.blue;
//形状选择面板类
classShapePanelextendsJPanelimplementsItemListener{
直线"
矩形"
圆形"
JRadioButtonjrb4=newJRadioButton("
三角形"
publicShapePanel(DrawPaneldp){
bg.add(jrb4);
jrb1.setSelected(true);
this.setLayout(newFlowLayout());
add(jrb4);
jrb4.addItemListener(this);
if(jrb1.isSelected()){
dp.shape=newPaintingBrush_Line();
}elseif(jrb2.isSelected()){
dp.shape=newPaintingBrush_Rectangle();
}elseif(jrb3.isSelected()){
dp.shape=newPaintingBrush_Circle();
}elseif(jrb4.isSelected()){
dp.shape=newPaintingBrush_Triangle();