Java笔记 绘图

绘图最重要的是获取画布

画布我们可以利用面板来充当

//画小人例程
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class test {
    
    

    public static void main(String[] args)
    {
    
    
        // 创建相框
        JFrame jFrame = new JFrame();
        // 创建画板
        JPanel jpanel = new JPanel()
        {
    
    
            public void paint(Graphics graphics) 
            {
    
    
                // 用画笔Graphics,在画板JPanel上画一个小人
                graphics.drawOval(100, 70, 30, 30);// 头部(画圆形)
                graphics.fillRect(105, 100, 20, 30);// 身体(画矩形)
                graphics.drawLine(105, 100, 75, 120);// 左臂(画直线)
                graphics.drawLine(125, 100, 150, 120);// 右臂(画直线)
                graphics.drawLine(105, 130, 75, 150);// 左腿(画直线)
                graphics.drawLine(125, 130, 150, 150);// 右腿(画直线)
            }
        };
        //将绘有小人图像的画板嵌入到相框中
        jFrame.add(jpanel);
        // 设置画框大小(宽度,高度),默认都为0
        jFrame.setSize(300, 300);
        // 将画框展示出来。true设置可见,默认为false隐藏
        jFrame.setVisible(true);
    }
}

在这里插入图片描述

Graphics graphics是正儿八经的画布,我们都是要利用这个类来进行绘图

画椭圆graphics.drawOval(100, 70, 30, 30);
画矩形graphics.fillRect(105, 100, 20, 30);填充的
画直线 graphics.drawLine(105, 100, 75, 120)
画圆角矩形graphics.fillRoundRect(100, 70, 300, 150, 40,30);后面两个参数是用来调试圆角的程度的
在这里插入图片描述


我们也可以利用另一个类Graphics2D,这个画布类可以使用一个画笔类BasicStroke(int),参数是画笔的粗细
使用Graphics2D g.setStroke来设置画布的画笔
使用`g.setColor(Color.red); 来设置画笔颜色

当然我们也可以不重写paint函数,自己可以新写一个函数`

// 画圆函数
        public void paintCircle(int X, int Y)
        {
    
    
            Graphics2D g = (Graphics2D)getGraphics();  // 使用 Graphics2D 来绘画
            g.setStroke(new BasicStroke(2));    // 设置画笔大小
            g.setColor(Color.blue);                    // 设置颜色
            g.fillOval(X - 10 ,Y - 10 ,20,20);
        }

这个方法同样是在面板中实现
最重要的是Graphics2D g = (Graphics2D)getGraphics();
这个getGraphics()函数是容器中自带的函数
使用getGraphics()函数获取面板中的画布,强制转换为Graphics2D
再使用这个类来完成后序操作

其实也可以直接从窗口获取画布Frame.getGraphics()


清除函数
清除矩形内的绘图

g.clearRect(0, 0, 400, 300);  

最后别忘了实例化面板和监听器,添加组件,注册监听器
监听器要注册给获取画布的组件,不然获得的坐标会有偏差

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


class MyFrame extends JFrame
{
    
    
    Mypanel mypanel;  // 建立画板画图
    Police police;    // 监听器

    MyFrame(String s)
    {
    
    
        // 初始化组件
        super(s);
        mypanel = new Mypanel();
        police = new Police();

        // 添加组件和监听器
        add(mypanel);
        mypanel.addMouseListener(police);  

        // 设置窗口参数
        setBounds(350,350,400,300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    class Mypanel extends JPanel
    {
    
    
        // 画圆函数
        public void paintCircle(int X, int Y)
        {
    
    
            Graphics2D g = (Graphics2D)getGraphics();  // 使用 Graphics2D 来绘画
            g.setStroke(new BasicStroke(2));    // 设置画笔大小
            g.setColor(Color.blue);                    // 设置颜色
            g.fillOval(X - 10 ,Y - 10 ,20,20);
        }

        // 画矩形
        public void paintRect(int X, int Y)
        {
    
    
            Graphics2D g = (Graphics2D)getGraphics();  // 使用 Graphics2D 来绘画
            g.setStroke(new BasicStroke(2));    // 设置画笔大小
            g.setColor(Color.red);                    // 设置颜色
            g.fillRect(X - 17, Y - 10, 35, 20);
        }

        // 清空函数
        public void clear()
        {
    
    
            Graphics2D g = (Graphics2D)getGraphics();  // 使用 Graphics2D 来绘画
            g.clearRect(0, 0, 400, 300);   // 清楚矩形范围内的画
        }
    }

    // 实现监听器
    class Police extends MouseAdapter
    {
    
    
        public void mouseClicked(MouseEvent e)
        {
    
    
            if(e.getClickCount() == 2)  // 双击清空
                mypanel.clear();
            else if(e.getButton() == e.BUTTON1)
                mypanel.paintCircle(e.getX(), e.getY());  // 左键画圆
            else if(e.getButton() == e.BUTTON3)
                mypanel.paintRect(e.getX(), e.getY());  // 右键画矩形
        }
    }

}

public class test
{
    
    
    public static void main(String[] args)
    {
    
    
        MyFrame myFrame = new MyFrame("drawPoint");
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/yogur_father/article/details/109063361
今日推荐