别的不多说直接上截图和代码,不会创建项目的朋友还希望你们自己百度如何创建好了
设计窗口如下,本代码只实现了跟随鼠标移动以及按下鼠标绘画直线的简单绘画,后期进行更改以及完善代码。
部分功能代码如下
/*
* To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mypaint;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JColorChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* @author Asus
*/
public class NewJFrame extends javax.swing.JFrame implements MouseMotionListener,MouseListener{
public Color color=new Color(0,0,0);
public static int flag=0,flag2=0;
public static int startx,starty,endx,endy,x,y;
@Override
public void paint(Graphics g) {
//super.paint(g); //To change body of generated methods, choose Tools | Templates.
g.setColor(Color.BLACK);
if(flag==1){
g.drawLine(startx, starty, endx, endy);
}
}
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setBackground(new java.awt.Color(102, 102, 102));
jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
jButton1.setText("矩形");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerfo