当前位置:网站首页>Low level version of drawing interface (explain each step in detail)
Low level version of drawing interface (explain each step in detail)
2022-07-03 15:54:00 【Zhangjian Tianya 2.0】
package com.lk.login1029;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
// Don't look at the source documents and references in the whole process, plus some of your own ideas
// First create a form
// The size of the form needs to be set ; In the middle ; When the form closes, the program closes itself and ends ;
// You need to add buttons to the form ; Note that the button can only be added when the form is displayed ; Brushes and the like ;
// When the demand reaches the button to be added, you will think about where to add it , So you need to add a layout manager
// When you need to add multiple buttons, you find that their steps are similar ; So use functions instead of repetitive work
// Need to add text ; The button is created by itself ; Form required ; Need a listener
// You need to install a listener on the form ; By looking at the original code, it is found that the contents in brackets need an interface data type ; When you need interface data types, you need to rewrite a class as data
// You need an array to match str Fill the occupied position
// When you need to add color buttons, you find ; The contents of the array have changed ; So create a function to add a button with all colors
public class DrawUI2 {
String[] strs = {" A straight line "," Square "," The ellipse "," an isosceles triangle "," Any triangle "," Freeform "};// Note that it is stored in array form, not in list form
Color[] color = {Color.black,Color.blue,Color.red,Color.green,Color.yellow};// Color is also a type
public void showUI() {
JFrame jf =new JFrame();
jf.setSize(900, 900);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(3);
jf.setVisible(true);
FlowLayout flow =new FlowLayout();
jf.setLayout(flow);
//JButton jb =new JButton("text");
//jf.add(jb);
//jb.addActionListener(mouse);
Mouse2 mou =new Mouse2();
// Add listeners to the interface
jf.addMouseListener(mou);
// Add multiple buttons on the interface
// This kind of general belt xxx. Is either an attribute or a method
for (int i=0;i<strs.length;i++) {
AddButton(strs[i],jf,mou);}
for (int i=0;i<color.length;i++) {
AddColor(color[i],jf,mou);
}
Graphics gr = jf.getGraphics();
mou.g = gr;
}
public void AddButton(String str,JFrame jf,ActionListener al) {
JButton jb = new JButton(str);
jf.add(jb);
jb.addActionListener(al);
}
public void AddColor(Color color,JFrame jf,ActionListener al) {
JButton jb = new JButton(" ");// The space function increases the size of the button
jb.setBackground(color);
jf.add(jb);
jb.addActionListener(al);
}
public static void main(String[] args) {
DrawUI2 ui=new DrawUI2();
ui.showUI();
}
}
package com.lk.login1029;
import javax.swing.*;
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 javax.swing.JButton;
public class Mouse2 implements ActionListener,MouseListener{
Graphics g;
String typestr,str ;
int x1,y1,x2,y2;
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println(" The button is clicked ");
str =e.getActionCommand();
if(str.equals(" ")){
System.out.println(" This is a color button ");
JButton jbu =(JButton)e.getSource();// This means to record the event source obtained
Color color = jbu.getBackground();
g.setColor(color);// The brush gets the background color
}
else {
typestr =str;
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(" The button is clicked ");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x1 = e.getX();
y1 =e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
x2 =e.getX();
y2 = e.getY();
g.drawLine(x1, y1, x2, y2);
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(" Get into ");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println(" sign out ");
}
}
The front is the whole program
// First Create a form
// Form needs Set size Ah, something like that ; In the middle ; When When the form closes, the program closes itself and ends ;
// You need to add buttons to the form ; Note that the button can only be added when the form is displayed ; Brushes and the like ;
// When the demand reaches the button to be added, you will think about where to add it , So you need to add Layout manager
// When you need to add multiple buttons, you find that their steps are similar ; So use functions instead of repetitive work
// Need to add text ; The button is created by itself ; Form required ; Need a listener
// You need to install a listener on the form ; By looking at the original code, it is found that the contents in brackets need an interface data type ; When interface data types are required Rewrite a class as data
// You need an array to match str Fill the occupied position
// When you need to add color buttons, you find ; The contents of the array have changed ; So create a Function to add a button with all colors
边栏推荐
猜你喜欢
整形和浮点型是如何在内存中的存储
Unity function - unity offline document download and use
秒杀系统3-商品列表和商品详情
工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!
CString的GetBuffer和ReleaseBuffer使用说明
Reading notes of "micro service design" (Part 2)
App移动端测试【5】文件的写入、读取
秒殺系統3-商品列錶和商品詳情
Visual upper system design and development (Halcon WinForm) -1 Process node design
Create gradle project
随机推荐
Principles of several common IO models
2022年Q2加密市场投融资报告:GameFi成为投资关键词
请做好3年内随时失业的准备?
win32创建窗口及按钮(轻量级)
Unityshader - materialcapture material capture effect (Emerald axe)
深度学习之三维重建
使用AUR下载并安装常用程序
几种常见IO模型的原理
The difference between RAR and zip files
A Fei's expectation
秒杀系统1-登录功能
App移动端测试【5】文件的写入、读取
驱动与应用程序通信
Unity function - unity offline document download and use
阿飞的期望
Subclass hides the function with the same name of the parent class
UnityShader——MaterialCapture材质捕捉效果 (翡翠斧头)
潘多拉 IOT 开发板学习(HAL 库)—— 实验5 外部中断实验(学习笔记)
How to use annotations such as @notnull to verify and handle global exceptions
“用Android复刻Apple产品UI”(3)—优雅的数据统计图表