📄 drawandview.java
字号:
fbutton.setActionCommand("Free");//添加工具栏中的空白域,使图形选择和颜色选择分开
buttonPane.addSeparator();//分隔线
buttonPane.addSeparator();
JButton cbutton=new JButton("颜色");//将颜色选项添加到工具栏中
buttonPane.add(cbutton);
cbutton.addActionListener(new ActionListener()//匿名内部类事件处理
{
public void actionPerformed(ActionEvent event)
{
color = JColorChooser.showDialog(drawAndViewPanel.this,"select a new color...",color);
}});
cbutton.setActionCommand("Color");
ButtonGroup group = new ButtonGroup();
fill = new JRadioButton("实心",false);//将实心还是空心按钮添加到工具栏中
fill.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{ isFill = true; }
});
group.add(fill);
buttonPane.add(fill);
draw = new JRadioButton("空心",true);
draw.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
isFill = false;
}});
group.add(draw);
buttonPane.add(draw);
add(buttonPane,"North");//置边框布局北方
toDraw=1;
fill.setEnabled(false);
draw.setEnabled(false);
drawField =new drawAndViewField();//新建一个画图的区域
add(drawField,"Center");//置边框布局中央
}
public void actionPerformed(ActionEvent event)//按钮的事件处理
{
String a=event.getActionCommand();
if (a.equals("Line")) toDraw=1;
else if (a.equals("Ellipse")) toDraw=2;
else if (a.equals("Rectangle")) toDraw=3;
else if (a.equals("Free")) toDraw=4;
else toDraw=1;
if (toDraw==1||toDraw==4)//若是画直线或是自由画就屏蔽掉实心或空心的按钮
{
fill.setEnabled(false);
draw.setEnabled(false);
}
else
{
fill.setEnabled(true);
draw.setEnabled(true);
}
}
public void newImage()
{
drawField.drawlist.clear();//数组清零
drawField.backgrand=null;
drawField.repaint();//重绘图
}
public void openImage(ArrayList<VImage> a,Image i)
{
newImage();
if(a!=null)
drawField.drawlist=a;
drawField.backgrand=i;
if(i!=null)
System.out.println("加载图片");
}
public ArrayList getImageArray()
{
return drawField.drawlist;
}
public Image getBackgrand()
{
return drawField.backgrand;
}
public BufferedImage getPicture()
{
BufferedImage bufferImage = new BufferedImage(drawField.getWidth(),drawField.getHeight(),BufferedImage.TYPE_INT_RGB) ;
Graphics buffer= bufferImage.createGraphics();
Graphics2D buffer2=(Graphics2D)buffer;
buffer2.setPaint(Color.WHITE);
buffer2.fill(new Rectangle2D.Double(0,0,getWidth(),getHeight()));
buffer2.setPaint(Color.BLACK);
if(drawField.backgrand!=null)
buffer2.drawImage(drawField.backgrand,0,0,this);
drawField.paintMap(buffer2);
return bufferImage;
}
private class KeyHandler extends KeyAdapter//对键盘事件的处理方法
{
public void keyPressed(KeyEvent event)
{
isSquare = event.isAltDown();//如果Alt被按下,则是画正方形或者圆形
if(event.isControlDown()&&event.getKeyCode()==KeyEvent.VK_Z)//当按下Ctrl+Z则取消上一次的操作
drawField.removeEle();//调用removeEle方法
}
public void keyReleased(KeyEvent event)
{
isSquare = event.isAltDown();//如果Alt被按下则画正方形
}
}
//画板域
private class drawAndViewField extends JPanel implements MouseListener,MouseMotionListener
{
public ArrayList<VImage> drawlist=new ArrayList();//建立数组
public VImage a;
public boolean isPaint=false;
public Image backgrand=null;
public drawAndViewField()
{
setBackground(new Color(255,255,255));//背景颜色
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(new KeyHandler());//添加键盘事件处理
setFocusable(true);
}
public void removeEle()//取消操作的处理
{
int i=drawlist.size();
if (i>0)
{
drawlist.remove(i-1);
repaint();
}
}
public void paintComponent(Graphics g)//绘图方法
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;//强制类型转换
if(backgrand!=null)
g2.drawImage(backgrand,0,0,this);
paintMap(g2);
}
public Graphics2D paintMap(Graphics2D g2)
{
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.WHITE);
if(backgrand!=null)
g2.drawImage(backgrand,0,0,this);
else g2.fill(new Rectangle2D.Double(0,0,getWidth(),getHeight()));
g2.setPaint(Color.BLACK);
for(int i=0;i<drawlist.size();i++)
{
(drawlist.get(i)).draw(g2);
}
return g2;
}
public void rpaint(Graphics2D g2)
{
VImage i=(drawlist.get(drawlist.size()-1));
i.draw(g2);
}
//鼠标事件的处理抽象方法
public void mousePressed(MouseEvent e)//抽象方法1
{ isPaint=true;
if (toDraw==4)
a=new VImage(e.getX(),e.getY(),toDraw,color);//自由画不用考虑实心或空心
else
a=new VImage(e.getX(),e.getY(),toDraw,color,isFill,isSquare);
drawlist.add(a);
setFocusable(true);
}
public void mouseReleased(MouseEvent e)//抽象方法2
{
isPaint=false;
Graphics2D g2=(Graphics2D)getGraphics();
repaint();}
public void mouseClicked(MouseEvent e){} //抽象方法3
public void mouseMoved(MouseEvent e){} //抽象方法4
public void mouseDragged(MouseEvent e)//抽象方法5
{
Graphics2D g2=(Graphics2D)getGraphics();
Color color=getBackground();
if (toDraw==4)
{
p1=new Point2D.Double(e.getX(),e.getY());//建立2D点对象
a.points.add(p1);
}else
{ g2.setXORMode(color);
rpaint(g2);
a.compare();
a.isSquare=isSquare;
a.ly=e.getY();
a.lx=e.getX();
}
rpaint(g2);//绘制图形
}
public void mouseExited(java.awt.event.MouseEvent e){} //抽象方法6
public void mouseEntered(java.awt.event.MouseEvent e)//抽象方法7
{
requestFocusInWindow();
}
}
}
class VImage implements Serializable//图像类
{
public int fx,fy,lx,ly;//坐标起始点和终止点
public int lu,ld;//图形左上方的点
public int length;//图形的宽
public int height;//图形的高
public int type;//图形类型
public boolean isFill=false;//是否填充
public ArrayList points;
public Color color;//图形颜色
public boolean isSquare;//是否是方形
public VImage(int fx,int fy,int type,Color color ,boolean isFill,boolean isSquare)
{
this.fx=fx;
this.fy=fy;
this.type=type;
this.color=color;
this.isFill=isFill;
this.isSquare=isSquare;
lx=fx;
ly=fy;
}
public VImage(int x,int y,int type,Color color)
{
this.type=type;
this.color=color;
points=new ArrayList();
points.add(new Point2D.Double(x,y));
}
public void draw(Graphics2D g2)//绘图方法
{
g2.setPaint(color);
g2.setStroke(new BasicStroke(2,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER));
if (isFill)
switch (type){
case 1: g2.draw(new Line2D.Double(fx,fy,lx,ly)); break;//直线
case 2: g2.fill(new Ellipse2D.Double(lu,ld,length,height));break;//椭圆
case 3: g2.fill(new Rectangle2D.Double(lu,ld,length,height));break;//矩形
case 4: for(int i=0;i<points.size()-1;i++)
g2.draw(new Line2D.Double((Point2D)points.get(i),(Point2D)points.get(i+1)));break;
}
else
switch (type){
case 1: g2.draw(new Line2D.Double(fx,fy,lx,ly)); break;
case 2: g2.draw(new Ellipse2D.Double(lu,ld,length,height));break;
case 3: g2.draw(new Rectangle2D.Double(lu,ld,length,height));break;
case 4: for(int i=0;i<points.size()-1;i++)
g2.draw(new Line2D.Double((Point2D)points.get(i),(Point2D)points.get(i+1)));break;
}
}
public void compare()//处理坐标
{
length = Math.abs(lx-fx);
height = Math.abs(ly-fy);
if (isSquare)
{
if (height>length)
height = length;
else length=height;
}//是方形时宽和高相等
if (lx>fx)
{
lu=fx;
}
else
{
lu=fx-length;
}
if(ly>fy)
{
ld=fy;
}
else
{
ld=fy-height;
}
}
}
public class drawAndView
{//主方法
public static void main(String args[]) {
System.out.println("Starting Draw...");
drawAndViewFrame mainFrame = new drawAndViewFrame();//构造方法
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);//jdk1.5用mainFrame.setVisible代替
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -