Java實(shí)現(xiàn)的簡(jiǎn)單畫圖板示例
本文實(shí)例講述了Java實(shí)現(xiàn)的簡(jiǎn)單畫圖板。分享給大家供大家參考,具體如下:
這個(gè)畫圖板是我好久之前做的,之后浙大的同學(xué)需要做課設(shè)然后就花了一點(diǎn)時(shí)間將它改了一下,變得簡(jiǎn)單些能夠方便擴(kuò)充功能,同時(shí)學(xué)習(xí)java基礎(chǔ)
先截圖一下吧,就可以知道有哪些功能了~

三個(gè)分區(qū),上面選擇圖形,下面選擇顏色,立體圓就是一個(gè)分形,也先放著不需要的同學(xué)可以注釋了它
代碼很簡(jiǎn)單,就是JPanel進(jìn)行分區(qū),得到畫筆,同時(shí)使用畫圖的函數(shù)就可以做到了
貼代碼應(yīng)該很快就會(huì)了~
主類
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("簡(jiǎn)單畫板");
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null);
jf.setLayout(new BorderLayout());
// 實(shí)例化事件監(jiān)聽類
DrawListener dl = new DrawListener(this);
// 實(shí)現(xiàn)中間面板
this.setBackground(Color.WHITE);
jf.add(this, BorderLayout.CENTER);
// 實(shí)現(xiàn)性狀面板
JPanel ShapePanel = new JPanel();
ShapePanel.setBackground(Color.black);
ShapePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
ShapePanel.setBackground(Color.gray);
;
String[] Shape = { "直線", "曲線", "圓", "噴槍", "橡皮擦", "矩形", "橢圓", "圓角矩形",
"弧線", "多邊形", "圖形", "三角形", "立體圓", };
for (int i = 0; i < Shape.length; i++) {
JButton button = new JButton(Shape[i]);
button.setBackground(Color.WHITE);
button.addActionListener(dl); // 添加事件監(jiān)聽機(jī)制
ShapePanel.add(button);
}
jf.add(ShapePanel, BorderLayout.NORTH);
// 實(shí)現(xiàn)顏色面板
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); // 添加事件監(jiān)聽機(jī)制
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);
}
}
監(jiān)聽輔助類
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);
}
}
// 實(shí)現(xiàn)畫筆
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;
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 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);
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);
}
}
}
}
代碼量也還是挺小的,因?yàn)槭呛?jiǎn)單畫板嘛~~
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
SpringBoot文件上傳功能的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot文件上傳功能的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java class文件格式總結(jié)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java class文件格式總結(jié)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-06-06
mybatis-plus報(bào)錯(cuò)net.sf.jsqlparser.statement.select.SelectBody的
本文主要介紹了mybatis-plus報(bào)錯(cuò)net.sf.jsqlparser.statement.select.SelectBody的問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08
SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證
本文主要介紹了SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

