Flask接口签名sign原理与实例代码浅析
295
2022-09-04
Java实现简易画图板
本文实例为大家分享了java实现简易画图板的具体代码,供大家参考,具体内容如下
这是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 frame.add(getButton(name[i],dl,true)); } for(int i=0;i frame.add(getButton("",dl,false)); } frame.addMouseListener(dl); frame.setVisible(true); // 获取画笔 --- 必须要放在 可视化之后 Graphics g=frame.getGraphics(); dhttp://l.g=g; http:// 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 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.WokuLIsVJuequals("")){ 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 } }
frame.add(getButton(name[i],dl,true));
}
for(int i=0;i frame.add(getButton("",dl,false)); } frame.addMouseListener(dl); frame.setVisible(true); // 获取画笔 --- 必须要放在 可视化之后 Graphics g=frame.getGraphics(); dhttp://l.g=g; http:// 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 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.WokuLIsVJuequals("")){ 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 } }
frame.add(getButton("",dl,false));
}
frame.addMouseListener(dl);
frame.setVisible(true);
// 获取画笔 --- 必须要放在 可视化之后
Graphics g=frame.getGraphics();
dhttp://l.g=g;
http:// 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 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.WokuLIsVJuequals("")){ 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 } }
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.WokuLIsVJuequals("")){
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
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~