Java swing简单实现随心画图

这篇博客介绍了如何使用Java Swing库创建一个简单的绘图应用,允许用户选择颜色并自由绘制图形。虽然目前存在一些已知的bug,但作者计划在未来进行修复和完善。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * 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 firsttext;


import javax.swing.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FlowLayout;
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;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;
import jdk.nashorn.internal.codegen.CompilerConstants;
import sun.security.x509.X400Address;


/**
 *
 * @author Asus
 */
public class Firsttext extends  JFrame implements MouseListener,ActionListener,MouseInputListener{
    public static final int Width=1000;
    public static final int Height=1000;
    public static int jishu=0;
    MyPanel myPanel=new MyPanel();
    public static int X1=0,Y1=0;


    @Override
    public void paint(Graphics g) {
        //super.paint(g); //To change body of generated methods, choose Tools | Templates.
        this.setBackground(Color.white);
        Color color=g.getColor();
        g.setColor(myPanel.color);
        g.fillOval(X1,Y1,5,5);
    }


   
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new Firsttext();
        
        
    }
   public  Firsttext(){
       /*JFrame jFrame=new JFrame("第一个测试窗口");//上层容器
        jFrame.setSize(Width,Height);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
        
       JPanel contJPanel=new JPanel();//中间容器
       jFrame.setContentPane(contJPanel);
        jFrame.getContentPane().add(myPanel);
        jFrame.addMouseListener(this);
        //jFrame.pack();*/
       this.setSize(WIDTH, HEIGHT);
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       this.setVisible(true);
       this.getContentPane().add(myPanel);
       this.addMouseListener(this);
       this.addMouseMotionListener(this);
       this.pack();
}


   
   


    @Override
    public void mouseClicked(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
         // 
        // X1=e.getX();
        //Y1=e.getY();
        //System.out.println(X1+"   "+Y1);
       // repaint();
        // JOptionPane.showConfirmDialog(this, "你很帅吗");
        if(jishu==0){
            jishu=1;
        }
        else jishu=0;
         


    }


    @Override
    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   
    }


    @Override
    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    @Override
    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    
    }


    @Override
    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    @Override
    public void mouseDragged(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   
    }


    @Override
    public void mouseMoved(MouseEvent e) {
       //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
     if(jishu==1){
       X1=e.getX();
      Y1=e.getY();
      repaint();
     }
    }
}
class MyPanel extends JPanel implements ActionListener,MouseListener{
        public JButton button,red,rgb,green,blue;
        public Color color=new Color(0,0,0);
        public MyPanel(){
            
            button=new JButton("Get Color");
            rgb=new JButton("RGB:");
            red=new JButton("Red:");
            green=new JButton("Green:");
            blue=new JButton("Blue:");
            button.addActionListener((ActionListener) this);
            setPreferredSize(new Dimension(550,250));
            setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
            setBackground(color);
            add(button);
            add(rgb);
            add(red);
            add(green);
            add(blue);
            
        }
        public void actionPerformed(ActionEvent e){
            color=JColorChooser.showDialog(MyPanel.this,"Choose Color", color);
            //setBackground(color);
            button.setText("Get Again");
            rgb.setText("RGB:"+color.getRGB());
            red.setText("Red:"+color.getRed());
            green.setText("Green:"+color.getGreen());
            blue.setText("Blue:"+color.getBlue());
            
        }


    @Override
    public void mouseClicked(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        //JOptionPane.showConfirmDialog(this, "dasda");
    }


    @Override
    public void mousePressed(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    @Override
    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    @Override
    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    @Override
    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
        

  }


代码可以实现选择颜色并进行随心绘画,但还有一些bug,后期会进行更改


如图所示可以进行任意绘画


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值