当前位置: 首页 > article >正文

java画图_Java实现的简单画图板示例

本文实例讲述了Java实现的简单画图板。分享给大家供大家参考,具体如下:

这个画图板是我好久之前做的,之后浙大的同学需要做课设然后就花了一点时间将它改了一下,变得简单些能够方便扩充功能,同时学习java基础

先截图一下吧,就可以知道有哪些功能了~

34a2420948d1f9331ec9217a70e56e39.png

三个分区,上面选择图形,下面选择颜色,立体圆就是一个分形,也先放着不需要的同学可以注释了它

代码很简单,就是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 = { "直线", "曲线", "圆", "喷枪", "橡皮擦", "矩形", "椭圆", "圆角矩形",

"弧线", "多边形", "图形", "三角形", "立体圆", };

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;

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);

}

}

}

}

代码量也还是挺小的,因为是简单画板嘛~~

希望本文所述对大家java程序设计有所帮助。

http://www.lryc.cn/news/2415600.html

相关文章:

  • MIDP2.0中对图片象素级处理(2)
  • 80个让你笑爆肚皮的程序员段子,不好笑算我输!
  • c/c++高质量编程
  • iOS开发笔记之五——Xcode 6.0 Beta编译工程时可能遇到的问题及参考解决方案
  • A 驾驶员辅助系统开发的基础2
  • LCD液晶显示屏工作原理
  • 冯·诺伊曼结构(英語:),也称馮·紐曼模型(Von Neumann model),是一种将程序指令存储器和数据存储器合并在一起的電腦設計概念结构。
  • Adobe Dreamweaver所有版本的序列号
  • 机架服务器 远程电源管理 备忘
  • 史上最详细SharePoint 2010安装步骤图解新手教程
  • Android 学习指南
  • Windows Media Player Real Player ActiveX 属性及方法
  • Computer Graphics From Scratch - Chapter 5
  • 新的UnityAnswers社区站点
  • 13个免费资源网站,你想要的全都有!【各类宝藏资源,建议收藏】
  • wordpress友联_一段代码开启WordPress友情链接管理
  • TM2008 Preview体验
  • 初学者一定要看!Perl 语言入门学习
  • web服务IIS服务器搭建
  • 度量空间
  • Eclipse 3.4+MyEclipse 6.01+MyEclipse 6.5+MyEclipse 6.01注册机+Eclipse汉化包官方下载地址
  • Python之Pandas中Series、DataFrame实践
  • OpenCV学习笔记(二):读取mnist数据集
  • 个人DIY一套全智能机器人基本流程
  • kindeditor在php中的使用,PHP中使用kindeditor
  • 微信小程序实现简单的树形选择控件------treeSelect
  • 干啥啥不行,摸鱼第一名
  • Dell BIOS/CMOS设置诠释
  • 干货分享新华三、华为、思科认证,到底选择哪一个?
  • 程序员常见系统错误代码大全:1到15841