Java画图板
Benjamin. 人气:0这是Windows系统自带的画图板,我们就是模仿着用Java去写一个简易版的画图板。
效果图
图中的四个颜色按钮是可以配合要画的图形而改变线条颜色的,而且每次打开颜色按钮的颜色都是随机的。目前画图什么的都没什么问题,只是一些细节没有去完全修复了,但不影响使用,再加上近段时间有点太忙,就没有去完善相关细节了,有兴趣的小伙伴可以评论区帮忙提出指正意见哈。
画图板UI界面代码
import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import java.awt.Graphics; import java.util.Random; public class DrawGraphics { Random rand=new Random(); public static void main(String []args){ DrawGraphics draw=new DrawGraphics(); draw.displayUI(); } public JButton getButton(String s,DrawListener dl,boolean flag){ JButton btn=new JButton(); //每个按钮的尺寸 Dimension dim=new Dimension(200,50); //如果flag为true,创建文字按钮,否则创建颜色按钮 if(flag){ btn=new JButton(s); } else{ Color c= new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)); btn.setBackground(c); } btn.setPreferredSize(dim); btn.addActionListener(dl); return btn; } public void displayUI(){ JFrame frame=new JFrame(); FlowLayout f1=new FlowLayout(); frame.setTitle("画图板"); frame.setSize(1000,1000); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setLayout(f1); frame.setResizable(false); DrawListener dl=new DrawListener(); String []name={"直线","矩形","圆","三角形"}; for(int i=0;i<name.length;i++){ frame.add(getButton(name[i],dl,true)); } for(int i=0;i<name.length;i++){ frame.add(getButton("",dl,false)); } frame.addMouseListener(dl); frame.setVisible(true); // 获取画笔 --- 必须要放在 可视化之后 Graphics g=frame.getGraphics(); dl.g=g; dl.btnstr=name[0]; dl.name=name; } }
画图板UI监听器代码
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import java.awt.Color; import java.awt.Graphics; public class DrawListener implements MouseListener,ActionListener{ Graphics g; int x1,x2,y1,y2; int x3,x4,x5,y3,y4,y5; int count=0; String []name; String btnstr; int i; @Override public void actionPerformed(ActionEvent e) { btnstr=e.getActionCommand(); if(btnstr.equals("")){ JButton btn=(JButton)e.getSource(); Color btnColor=btn.getBackground(); g.setColor(btnColor); } else { for(i=0;i<name.length;i++){ if(btnstr.equals(name[i]))break; } } } @Override public void mouseClicked(MouseEvent e) { if(btnstr.equals(name[3])||btnstr.equals("")){ g.drawOval(e.getX()-4, e.getY()-4, 8, 8); if(count==0){ x3=e.getX(); y3=e.getY(); count++; } else if(count==1){ x4=e.getX(); y4=e.getY(); g.drawLine(x3, y3, x4, y4); count++; } else if(count==2){ x5=e.getX(); y5=e.getY(); g.drawLine(x3, y3, x5, y5); g.drawLine(x4, y4, x5, y5); count=0; } } } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub x1=e.getX(); y1=e.getY(); } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub x2=e.getX(); y2=e.getY(); if(btnstr.equals(name[0])||btnstr.equals("")){ g.drawLine(x1, y1, x2, y2); } else if(btnstr.equals(name[1])||btnstr.equals("")){ g.drawRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y1-y2)); } else if(btnstr.equals(name[2])||btnstr.equals("")){ g.drawOval(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y1-y2)); } } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } }
加载全部内容