Java swing学习篇之JFrame窗口简单创立基于NetBeans平台,实现简单绘画

本文通过NetBeans平台,展示了如何使用Java Swing创建一个简单的绘画窗口,实现鼠标移动时跟随及按压鼠标画直线的功能。提供的代码片段为关键功能的实现,后续计划扩展和完善。
摘要由CSDN通过智能技术生成

别的不多说直接上截图和代码,不会创建项目的朋友还希望你们自己百度如何创建好了大笑

设计窗口如下,本代码只实现了跟随鼠标移动以及按下鼠标绘画直线的简单绘画,后期进行更改以及完善代码。

部分功能代码如下

/*

 * 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值