Flask接口签名sign原理与实例代码浅析
215
2023-01-23
Java实现的简单画图板示例
本文实例讲述了java实现的简单画图板。分享给大家供大家参考,具体如下:
这个画图板是我好久之前做的,之后浙大的同学需要做课设然后就花了一点时间将它改了一下,变得简单些能够方便扩充功能,同时学习java基础
先截图一下吧,就可以知道有哪些功能了~
三个分区,上面选择图形,下面选择颜色,立体圆就是一个分形,也先放着不需要的同学可以注释了它
代码很简单,就是JPanel进行分区,得到画笔,同时使用画图的函数就可以做到了
贴代码应该很快就会了~
主类
package awtDemo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class DrawMain extends JPanel {
public static void main(String[] args) {
// TODO Auto-generated method stub
DrawMain Draw = new DrawMain();
Draw.InitUI();
}
public void InitUI() {
JFrame jf = new JFrame();
jf.setSize(1000, 780);
jf.setTitle("简单画板");
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null);
jf.setLayout(new BorderLayout());
// 实例化事件监听类
DrawListener dl = new DrawListener(this);
// 实现中间面板
this.setBackground(Color.WHITE);
jf.add(this, BorderLayout.CENTER);
// 实现性状面板
JPanel ShapePanel = new JPanel();
ShapePanel.setBackground(Color.black);
ShapePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
ShapePanel.setBackground(Color.gray);
;
String[] Shape = { "直线", "曲线", "圆", "喷枪", KsdrG"橡皮擦", "矩形", "椭圆", "圆角矩形",
"弧线", "多边形", "图形", "三角形", "立体圆", };
for (int i = 0; i < Shape.length; i++) {
JButton button = new JButton(Shape[i]);
button.setBackground(Color.WHITE);
button.addActionListener(dl); // 添加事件监听机制
ShapePanel.add(button);
}
jf.add(ShapePanel, BorderLayout.NORTH);
// 实现颜色面板
JPanel ColorPanel = new JPanel();
ColorPanel.setBackground(Color.black);
ColorPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
ColorPanel.setBackground(Color.gray);
;
Color[] color = { Color.BLACK, Color.blue, Color.white, Color.gray,
Color.red, Color.CYAN, Color.green, Color.darkGray, Color.pink };
for (int i = 0; i < color.length; i++) {
JButton button = new JButton();
button.addActionListener(dl); // 添加事件监听机制
button.setPreferredSize(new Dimension(30, 30));
button.setBackground(color[i]);
ColorPanel.add(button);
}
jf.add(ColorPanel, BorderLayout.SOUTH);
jf.setVisible(true);
this.addMouseListener(dl);
this.addMouseMotionListener(dl);
}
}
监听辅助类
package awtDemo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JButton;
public class DrawListener extends MouseAdapter implements ActionListener {
private int x1, y1, x2, y2;
private int newx1, newy1, newx2, newy2;
private Graphics2D g;
private DrawMain df;
private boolean flag = false;
String shape = "直线";
Color color;
private int[] arrx = new int[4];
private int[] arry = new int[4];
private int temp = 0;
DrawListener(DrawMain d) {
df = d;
}
// 获取形状和颜色
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("")) {
JButton button = (JButton) e.getSource();
color = button.getBackground();
System.out.println("color = " + color);
} else {
JButton button = (JButton) e.getSource();
shape = button.getActionCommand();
System.out.println("String = " + shape);
}
}
// 实现画笔
public void mousePressed(MouseEvent e) {
g = (Graphics2D) df.getGraphics();
g.setColor(color);
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
if (shape.equals("直线")) {
g.drawLine(x1, y1, x2, y2);
} else if (shape.equals("弧线")) {
g.drawArc(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1), 0, 180);
} else if (shape.equals("多边形") && !flag) {
g.drawLine(x1, y1, x2, y2);
newx1 = x1;
newy1 = y1;
newx2 = x2;
newy2 = y2;
flag = true;
} else if (shape.equals("圆")) {
g.drawOval(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (shape.equals("矩形")) {
g.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
} else if (shape.equals("圆角矩形")) {
g.drawRoundRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1), 2, 10);
} else if (shape.equals("椭圆")) {
g.drawOval(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
}
}
public void mouseClicked(MouseEvent e) {
if (shape.equals("多边形") && flag) {
x2 = e.getX();
y2 = e.getY();
if (e.getClickCount() == 2) {
g.drawLine(newx1, newy1, newx2, newy2);
flag = false;
}
g.drawLine(newx2, newy2, x2, y2);
newx2 = x2;
newy2 = y2;
} else if (shape.equals("图形")) {
arrx[temp] = e.getX();
arry[temp] = e.getY();
temp++;
if (temp == 4) {
int x = arrx[3];
int y = arry[3];
for (int i = 0; i <= 10000; i++) {
Random ran = new Random();
int k = ran.nextInt(3);
x = (x + arrx[k]) / 2;
y = (y + arry[k]) / 2;
g.drawLine(x, y, x, y);
}
temp = 0;
}
} else if (shape.equals("立体圆")) {
// double a=-2,b=-2,c=-1.2,d=2;
double a = 1.40, b = 1.56, c = 1.40, d = -6.56;
KsdrG double x = 0, xo = 0;
double y = 0, yo = 0;
Color[] Col = { Color.BLUE, Color.cyan, Color.green, Color.magenta,
Color.red, Color.yellow };
for (int i = 0; i <= 90000; i++) {
Random r = new Random(); // 增加颜色
int R = r.nextInt(Col.length);
g.setColor(Col[R]);
// x=Math.sin(a*yo)-Math.cos(b*xo);
// y=Math.sin(c*xo)-Math.cos(d*yo);
x = d * Math.sin(a * xo) - Math.sin(b * yo);
y = c * Math.cos(a * xo) + Math.cos(b * yo);
int temp_x = (int) (x * 50);
int temp_y = (int) (y * 50);
g.drawLine(temp_x + 500, temp_y + 300, temp_x + 500,
temp_y + 300);
xo = x;
yo = y;
}
} else if (shape.equals("三角形")) {
double a = -2, b = -2, c = -1.2, d = 2;
double x = 0, xo = 0;
double y = 0, yo = 0;
Color[] Col = { Color.BLUE, Color.cyan, Color.green, Color.magenta,
Color.red, Color.yellow };
for (int i = 0; i <= 90000; i++) {
Random r = new RandoKsdrGm(); // 增加颜色
int R = r.nextInt(Col.length);
g.setColor(Col[R]);
x = Math.sin(a * yo) - Math.cos(b * xo);
y = Math.sin(c * xo) - Math.cos(d * yo);
int temp_x = (int) (x * 50);
int temp_y = (int) (y * 50);
g.drawLine(temp_x + 500, temp_y + 300, temp_x + 500,
temp_y + 300);
xo = x;
yo = y;
}
}
}
public void mouseDragged(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
if (shape.equals("曲线")) {
// g.setStroke(new BasicStroke(10));
// g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
// RenderingHints.VALUE_ANTIALIAS_ON);
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
} else if (shape.equals("橡皮擦")) {
g.setStroke(new BasicStroke(80));
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
} else if (shape.equals("喷枪")) {
// g.setStroke(new BasicStroke(2)); //不用加粗
// g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
// RenderingHints.VALUE_ANTIALIAS_ON);
for (int k = 0; k < 20; k++) {
Random i = new Random();
int a = i.nextInt(8);
int b = i.nextInt(10);
g.drawLine(x2 + a, y2 + b, x2 + a, y2 + b);
}
}
}
}
代码量也还是挺小的,因为是简单画板嘛~~
更多关于java相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~