Tutorial on how to create a simple drawing board using Java


package Cbs;import java.awt.Color;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;/** * Draw类,用于界面的初始化 * @author CBS */@SuppressWarnings("serial")public class Draw extends JFrame {// 界面初始化方法public void showUI() { setTitle("画图");//窗体名称setSize(1200, 900);//窗体大小setDefaultCloseOperation(3); setLocationRelativeTo(null);//窗体居中//流式布局左对齐FlowLayout layout = new FlowLayout(FlowLayout.LEFT); setLayout(layout);//窗体使用流式布局管理器this.setResizable(false);//窗体大小不变 //使用数组保存按钮名String buttonName[] = { "画直线", "画椭圆", "画曲线", "多边形", "橡皮擦", "拖动线","三角形", "画球形", "笔刷", "喷枪", "色子", "立体矩形", "立体圆", "立体三角","迭代分形", "现代分形", "枫叶", "画树", "mandelbrot集", "L-System", "迭代画线","迭代三角形", "谢尔宾斯基地毯", "画字符", "清空", "吸管" ,"矩形","五角星","多线","字符"};//用于保存图形按钮,使用网格布局JPanel jp1=new JPanel(new GridLayout(15, 2,10,10)); jp1.setPreferredSize(new Dimension(200, 800)); //循环为按钮面板添加按钮for (int i = 0; i



package Cbs;import java.awt.Color;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import javax.swing.JButton;public class DrawListener implements ActionListener, MouseListener, MouseMotionListener {private Color color;//颜色属性private Graphics g;//画笔属性private String str;//保存按钮上的字符串,区分不同的按钮private int x1,y1,x2,y2;//(x1,y1),(x2,y2)分别为鼠标的按下和释放时的坐标private JButton nowColor;//当前颜色按钮 //获取Draw类的画笔对象public void setG(Graphics g) {this.g = g; }//获取当前颜色按钮public void setNowColor(JButton nowColor) {this.nowColor = nowColor; } @Override//鼠标拖动的方法public void mouseDragged(MouseEvent e) {//画曲线的方法if ("画曲线".equals(str)) {int x, y; x = e.getX(); y = e.getY(); g.drawLine(x, y, x1, y1); x1 = x; y1 = y; } } @Override//鼠标移动方法public void mouseMoved(MouseEvent e) { } @Override//鼠标单击方法public void mouseClicked(MouseEvent e) { } @Override//鼠标按下方法public void mousePressed(MouseEvent e) { g.setColor(color);//改变画笔的颜色 x1=e.getX();//获取按下时鼠标的x坐标y1=e.getY();//获取按下时鼠标的y坐标 } @Override//鼠标释放方法public void mouseReleased(MouseEvent e) { x2=e.getX();//获取释放时鼠标的x坐标y2=e.getY();//获取释放时鼠标的y坐标//画直线的方法if ("画直线".equals(str)) { g.drawLine(x1, y1, x2, y2); } } @Override//鼠标进入方法public void mouseEntered(MouseEvent e) { } @Override//鼠标退出方法public void mouseExited(MouseEvent e) { } @Override//处理按钮上的鼠标点击动作public void actionPerformed(ActionEvent e) {//判断是颜色按钮还是图形按钮if ("".equals(e.getActionCommand())) { JButton jb = (JButton) e.getSource(); color = jb.getBackground(); nowColor.setBackground(color);//处理当前颜色} else { str = e.getActionCommand(); } } }
Draw类也要做一些修改,为按钮和面板添加监听:


1 package Cbs; 2 3 import java.awt.Color; 4 import java.awt.Dimension; 5 import java.awt.FlowLayout; 6 import java.awt.Graphics; 7 import java.awt.GridLayout; 8 9 import javax.swing.JButton;10 import javax.swing.JFrame;11 import javax.swing.JPanel;12 13 /**14 * Draw类,用于界面的初始化15 * @author CBS16 */17 @SuppressWarnings("serial")18 public class Draw extends JFrame {19 private DrawListener dl;20 private Graphics g;21 // 界面初始化方法22 public void showUI() {23 setTitle("画图");//窗体名称24 setSize(1200, 900);//窗体大小25 setDefaultCloseOperation(3);26 setLocationRelativeTo(null);//窗体居中27 //流式布局左对齐28 FlowLayout layout = new FlowLayout(FlowLayout.LEFT);29 setLayout(layout);//窗体使用流式布局管理器30 this.setResizable(false);//窗体大小不变31 32 //使用数组保存按钮名33 String buttonName[] = { "画直线", "画椭圆", "画曲线", "多边形",34 "橡皮擦", "拖动线","三角形", "画球形", "笔刷", "喷枪", 35 "色子", "立体矩形", "立体圆", "立体三角","迭代分形",36 "现代分形", "枫叶", "画树", "mandelbrot集", "L-System", 37 "迭代画线","迭代三角形", "谢尔宾斯基地毯", "画字符", "清空",38 "吸管" ,"矩形","五角星","多线","字符"};39 //用于保存图形按钮,使用网格布局40 JPanel jp1=new JPanel(new GridLayout(15, 2,10,10));41 jp1.setPreferredSize(new Dimension(200, 800));42 43 //实例化DrawListener对象44 dl=new DrawListener();45 //循环为按钮面板添加按钮46 for (int i = 0; i


//图形接口package Cbs;//图形集合public interface NetJavaShape {public abstract void draw(); }//直线类package Cbs;import java.awt.Color;import java.awt.Graphics;import Cbs.NetJavaShape;public class ImpLine implements NetJavaShape{ Graphics g;int x1, y1,x2, y2; Color c;public ImpLine(Graphics g,int x1,int y1,int x2,int y2,Color c){this.g=g;this.c=c;this.x1=x1;this.y1=y1;this.x2=x2;this.y2=y2; }public void draw() { g.setColor(c); g.drawLine(x1, y1, x2, y2); } }//DrawListener类package Cbs;import java.awt.Color;import java.awt.Graphics;import java.util.List;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.util.ArrayList;import Cbs.NetJavaShape;import javax.swing.JButton;public class DrawListener implements ActionListener, MouseListener, MouseMotionListener {private Color color=Color.BLACK;//颜色属性,初始值为黑色private Graphics g;//画笔属性private String str;//保存按钮上的字符串,区分不同的按钮private int x1,y1,x2,y2;//(x1,y1),(x2,y2)分别为鼠标的按下和释放时的坐标private JButton nowColor;//当前颜色按钮//保存图形对象的集合private List<netjavashape> shapesArray = new ArrayList<netjavashape>();//图形private NetJavaShape shape;//在draw类中获取集合public List<netjavashape> getShapesArray() {return shapesArray; }//获取Draw类的画笔对象public void setG(Graphics g) {this.g = g; }//获取当前颜色按钮public void setNowColor(JButton nowColor) {this.nowColor = nowColor; } @Override//鼠标拖动的方法public void mouseDragged(MouseEvent e) {//画曲线的方法if ("画曲线".equals(str)) {int x, y; x = e.getX(); y = e.getY();//实例化对象,曲线也是直线画的所以不同新建一个曲线类了shape=new ImpLine(g,x,y,x1,y1,color);//调用画图方法 shape.draw();//将图形存入集合中 shapesArray.add(shape);// g.drawLine(x, y, x1, y1);x1 = x; y1 = y; } } @Override//鼠标移动方法public void mouseMoved(MouseEvent e) { } @Override//鼠标单击方法public void mouseClicked(MouseEvent e) { } @Override//鼠标按下方法public void mousePressed(MouseEvent e) { g.setColor(color);//改变画笔的颜色 x1=e.getX();//获取按下时鼠标的x坐标y1=e.getY();//获取按下时鼠标的y坐标 } @Override//鼠标释放方法public void mouseReleased(MouseEvent e) { x2=e.getX();//获取释放时鼠标的x坐标y2=e.getY();//获取释放时鼠标的y坐标//画直线的方法if ("画直线".equals(str)) {//实例化对象,shape=new ImpLine(g,x1,y1,x2,y2,color);//调用画图方法 shape.draw();//将图形存入集合中 shapesArray.add(shape);// g.drawLine(x1, y1, x2, y2); } } @Override//鼠标进入方法public void mouseEntered(MouseEvent e) { } @Override//鼠标退出方法public void mouseExited(MouseEvent e) { } @Override//处理按钮上的鼠标点击动作public void actionPerformed(ActionEvent e) { if ("".equals(e.getActionCommand())) { JButton jb = (JButton) e.getSource(); color = jb.getBackground(); nowColor.setBackground(color);//处理当前颜色} else { str = e.getActionCommand(); } } }//draw类package Cbs;import java.awt.Color;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.GridLayout;import java.util.ArrayList;import java.util.List;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;/** * Draw类,用于界面的初始化 * @author CBS */@SuppressWarnings("serial")public class Draw extends JFrame {private DrawListener dl;private Graphics g;//保存图形对象的集合private List<netjavashape> shapesArray = new ArrayList<netjavashape>();// 界面初始化方法public void showUI() { setTitle("画图");//窗体名称setSize(1200, 900);//窗体大小setDefaultCloseOperation(3); setLocationRelativeTo(null);//窗体居中FlowLayout layout = new FlowLayout(FlowLayout.LEFT);//流式布局左对齐setLayout(layout);//窗体使用流式布局管理器this.setResizable(false);//窗体大小不变 //使用数组保存按钮名String buttonName[] = { "画直线", "画椭圆", "画曲线", "多边形", "橡皮擦", "拖动线","三角形", "画球形", "笔刷", "喷枪", "色子", "立体矩形", "立体圆", "立体三角","迭代分形", "现代分形", "枫叶", "画树", "mandelbrot集", "L-System", "迭代画线","迭代三角形", "谢尔宾斯基地毯", "画字符", "清空","吸管" ,"矩形","五角星","多线","字符"}; JPanel jp1=new JPanel(new GridLayout(15, 2,10,10));//用于保存图形按钮,使用网格布局jp1.setPreferredSize(new Dimension(200, 800)); //实例化DrawListener对象dl=new DrawListener();//循环为按钮面板添加按钮for (int i = 0; i </netjavashape></netjavashape></netjavashape></netjavashape></netjavashape>
The production of drawing board mainly uses Swing components, event listening mechanism, Graphics drawing and drawing board redrawing and the use of collections. Abstract classes or interfaces serve as standard graphics classes.
##Listener class
import java.awt.event.MouseEvent;
import java.awt.event. MouseListener;
//Color
Panel p;
//Canvas
Graphics g;
//Action command
private SimpleDraw draw;
//Give the coordinates
private int x1,y1,x2,y2;
//Construction method
public MyMouseListener(SimpleDraw draw){
this .g=draw.getGraphics();//Assign the passed object value to the attribute g
this.draw=draw;
}
//The mouse enters Called when the component is on
@Override
public void mouseClicked(MouseEvent e) {
}
//Called when the mouse leaves the component
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
x1=e.getX() ;
y1=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e. getY();
String command = draw.getCommand();
g.setColor(Color.blue);
if(command.equals("straight line")){
g.drawLine(x1, y1, x2, y2);
}else if(command.equals("rectangle")){
g.drawRect(x1, y1, x2-x1, y2-y1);
}else if(command.equals("ellipse")){
g.drawOval(x1, y1, x2-x1, y2-y1);
}
}
}
The above is the detailed content of Tutorial on how to create a simple drawing board using Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
