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

三個(gè)分區(qū),上面選擇圖形,下面選擇顏色,立體圓就是一個(gè)分形,也先放著不需要的同學(xué)可以注釋了它
代碼很簡(jiǎn)單,就是JPanel進(jìn)行分區(qū),得到畫(huà)筆,同時(shí)使用畫(huà)圖的函數(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)單畫(huà)板");
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null);
jf.setLayout(new BorderLayout());
// 實(shí)例化事件監(jiān)聽(tīng)類
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)聽(tīng)機(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)聽(tīng)機(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)聽(tīng)輔助類
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)畫(huà)筆
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)單畫(huà)板嘛~~
更多關(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)文章
spring boot使用i18n時(shí)properties文件中文亂碼問(wèn)題的解決方法
這篇文章主要介紹了spring boot使用i18n時(shí)properties文件中文亂碼問(wèn)題的解決方法,需要的朋友可以參考下2017-11-11
如何通過(guò)一張圖搞懂springBoot自動(dòng)注入原理
這篇文章主要給大家介紹了關(guān)于如何通過(guò)一張圖搞懂springBoot自動(dòng)注入原理的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
Javaweb基礎(chǔ)入門(mén)HTML之table與form
HTML的全稱為超文本標(biāo)記語(yǔ)言,是一種標(biāo)記語(yǔ)言。它包括一系列標(biāo)簽.通過(guò)這些標(biāo)簽可以將網(wǎng)絡(luò)上的文檔格式統(tǒng)一,使分散的Internet資源連接為一個(gè)邏輯整體。HTML文本是由HTML命令組成的描述性文本,HTML命令可以說(shuō)明文字,圖形、動(dòng)畫(huà)、聲音、表格、鏈接等2022-03-03
使用java的milo框架訪問(wèn)OPCUA服務(wù)的過(guò)程
這篇文章主要介紹了使用java的milo框架訪問(wèn)OPCUA服務(wù)的方法,本次采用KEPServerEX5模擬服務(wù)端,使用milo開(kāi)發(fā)的程序作為客戶端,具體操作使用過(guò)程跟隨小編一起看看吧2022-01-01
Lombok中@EqualsAndHashCode注解的使用及說(shuō)明
這篇文章主要介紹了Lombok中@EqualsAndHashCode注解的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring Boot 2.4新特性減少95%內(nèi)存占用問(wèn)題
這篇文章主要介紹了Spring Boot 2.4新特性減少95%內(nèi)存占用問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

