搞定JUI编程

简介: 搞定JUI编程


1.初始Jframe

import javax.swing.*;
import java.awt.*;
public class Test1 {
    public static void main(String[] args) {
        //事件分发线程
        EventQueue.invokeLater(() -> {
            JFrame frame = new JFrame("Welcome");
            //窗口大小
            frame.setSize(500, 500);
            //是否可改变窗口大小
            frame.setResizable(false);
            //关闭的时候,传递的功能
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //显示jframe窗口
            frame.setVisible(true);
        });
    }
}

2.创建属于自己的第一个frame子类

AlgoFrame

import javax.swing.*;
import java.awt.*;
public class AlgoFrame extends JFrame {
    public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
        super(title);
        //窗口大小
        this.setSize(canvasWidth, canvasHeight);
        //是否可改变窗口大小
        this.setResizable(false);
        //关闭的时候,传递的功能
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示jframe窗口
        this.setVisible(true);
    }
}

测试

import java.awt.*;
public class Test2 {
    public static void main(String[] args) {
        //事件分发线程
        EventQueue.invokeLater(() -> {
            AlgoFrame frame = new AlgoFrame("Welcome", 500, 500);
        });
    }
}

运行结果:

改进iframe

package com.juidemo.algoframe;
import javax.swing.*;
import java.awt.*;
public class AlgoFrame extends JFrame {
    private int canvasWidth;
    private int canvasHeight;
    public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
        super(title);
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;
        //窗口大小
        this.setSize(canvasWidth, canvasHeight);
        //是否可改变窗口大小
        this.setResizable(false);
        //关闭的时候,传递的功能
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示jframe窗口
        this.setVisible(true);
    }
    public AlgoFrame(String title) throws HeadlessException {
        this(title, 1024, 768);
    }
    public int getCanvasWidth() {
        return canvasWidth;
    }
    public int getCanvasHeight() {
        return canvasHeight;
    }
}

3.设置画布与图形绘制基础

案例:绘制一个圆。

package com.juidemo.algoframe;
import javax.swing.*;
import java.awt.*;
public class AlgoFrame extends JFrame {
    private int canvasWidth;
    private int canvasHeight;
    public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
        super(title);
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;
        //画布相关
        AlgoCanvas canvas = new AlgoCanvas();
//        canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        this.setContentPane(canvas);
        //跟进加载进内容的大小,调整窗口的大小
        this.pack();
        //窗口大小 删除这行代码,使用上面画布的大小
//        this.setSize(canvasWidth, canvasHeight);
        //是否可改变窗口大小
        this.setResizable(false);
        //关闭的时候,传递的功能
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示jframe窗口
        this.setVisible(true);
    }
    public AlgoFrame(String title) throws HeadlessException {
        this(title, 1024, 768);
    }
    public int getCanvasWidth() {
        return canvasWidth;
    }
    public int getCanvasHeight() {
        return canvasHeight;
    }
    private class AlgoCanvas extends JPanel {
        //绘制组件,绘制一个圆
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawOval(50,50,300,300);
        }
        //代替第17行代码
        // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        //根据面板自动调整窗口大小
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }
    }
}

测试

import java.awt.*;
public class Test2 {
    public static void main(String[] args) {
        //事件分发线程
        EventQueue.invokeLater(() -> {
            AlgoFrame frame = new AlgoFrame("Welcome", 500, 500);
//            AlgoFrame frame = new AlgoFrame("Welcome");
        });
    }
}

效果图:

4.使用Graphics2D

package com.juidemo.algoframe;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class AlgoFrame extends JFrame {
    private int canvasWidth;
    private int canvasHeight;
    public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
        super(title);
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;
        //画布相关
        AlgoCanvas canvas = new AlgoCanvas();
//        canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        this.setContentPane(canvas);
        //跟进加载进内容的大小,调整窗口的大小
        this.pack();
        //窗口大小 删除这行代码,使用上面画布的大小
//        this.setSize(canvasWidth, canvasHeight);
        //是否可改变窗口大小
        this.setResizable(false);
        //关闭的时候,传递的功能
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示jframe窗口
        this.setVisible(true);
    }
    public AlgoFrame(String title) throws HeadlessException {
        this(title, 1024, 768);
    }
    public int getCanvasWidth() {
        return canvasWidth;
    }
    public int getCanvasHeight() {
        return canvasHeight;
    }
    private class AlgoCanvas extends JPanel {
        //绘制组件
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
//            g.drawOval(50,50,300,300);
            //Graphics2D绘制
            Graphics2D g2d = (Graphics2D) g;
            //设置边框宽度
            int strokeWidth = 5;
            g2d.setStroke(new BasicStroke(strokeWidth));
            //基本图形封装在Ellipse2D对象中
            //设置颜色
            g2d.setColor(Color.RED);
            //图形坐标 左上角坐标,长和宽
            Ellipse2D circle = new Ellipse2D.Double(50,50,300,300);
            //绘制空心圆
            g2d.draw(circle);
            //设置颜色
            g2d.setColor(Color.BLUE);
            Ellipse2D circle2 = new Ellipse2D.Double(50,50,300,300);
            //绘制实心圆
            g2d.fill(circle2);
        }
        //代替第17行代码
        // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        //根据面板自动调整窗口大小
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }
    }
}

测试:

package com.juidemo.algoframe;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class AlgoFrame extends JFrame {
    private int canvasWidth;
    private int canvasHeight;
    public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
        super(title);
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;
        //画布相关
        AlgoCanvas canvas = new AlgoCanvas();
//        canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        this.setContentPane(canvas);
        //跟进加载进内容的大小,调整窗口的大小
        this.pack();
        //窗口大小 删除这行代码,使用上面画布的大小
//        this.setSize(canvasWidth, canvasHeight);
        //是否可改变窗口大小
        this.setResizable(false);
        //关闭的时候,传递的功能
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示jframe窗口
        this.setVisible(true);
    }
    public AlgoFrame(String title) throws HeadlessException {
        this(title, 1024, 768);
    }
    public int getCanvasWidth() {
        return canvasWidth;
    }
    public int getCanvasHeight() {
        return canvasHeight;
    }
    private class AlgoCanvas extends JPanel {
        //绘制组件
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
//            g.drawOval(50,50,300,300);
            //Graphics2D绘制
            Graphics2D g2d = (Graphics2D) g;
            //设置边框宽度
            int strokeWidth = 5;
            g2d.setStroke(new BasicStroke(strokeWidth));
            //基本图形封装在Ellipse2D对象中
            //设置颜色
            g2d.setColor(Color.RED);
            //图形坐标 左上角坐标,长和宽
            Ellipse2D circle = new Ellipse2D.Double(50,50,300,300);
            //绘制空心圆
            g2d.draw(circle);
            //设置颜色
            g2d.setColor(Color.BLUE);
            Ellipse2D circle2 = new Ellipse2D.Double(50,50,300,300);
            //绘制实心圆
            g2d.fill(circle2);
        }
        //代替第17行代码
        // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        //根据面板自动调整窗口大小
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }
    }
}

效果:

整理绘制工具类

工具类:

import java.awt.*;
import java.awt.geom.Ellipse2D;
public class AlgoVisHelper {
    private AlgoVisHelper(){}
    public static void setStrokeWidth(Graphics2D g2d, int w){
        int strokeWidth = w;
        //第二个参数,绘制的线条的端点是圆形的
        //第3个参数,在拐弯的时候,画出来的是平滑的实线
        g2d.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
    }
    public static void setColor(Graphics2D g2d, Color color){
        g2d.setColor(color);
    }
    public static void strokeCircle(Graphics2D g2d, int x, int y, int r){
        Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
        g2d.draw(circle);
    }
    public static void fillCircle(Graphics2D g2d, int x, int y, int r){
        Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
        g2d.fill(circle);
    }
}

改造原来的代码

import com.juidemo.tools.AlgoVisHelper;
import javax.swing.*;
import java.awt.*;
public class AlgoFrame extends JFrame {
    private int canvasWidth;
    private int canvasHeight;
    public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
        super(title);
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;
        //画布相关
        AlgoCanvas canvas = new AlgoCanvas();
//        canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        this.setContentPane(canvas);
        //跟进加载进内容的大小,调整窗口的大小
        this.pack();
        //窗口大小 删除这行代码,使用上面画布的大小
//        this.setSize(canvasWidth, canvasHeight);
        //是否可改变窗口大小
        this.setResizable(false);
        //关闭的时候,传递的功能
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示jframe窗口
        this.setVisible(true);
    }
    public AlgoFrame(String title) throws HeadlessException {
        this(title, 1024, 768);
    }
    public int getCanvasWidth() {
        return canvasWidth;
    }
    public int getCanvasHeight() {
        return canvasHeight;
    }
    private class AlgoCanvas extends JPanel {
        //绘制组件
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
//            g.drawOval(50,50,300,300);
            //Graphics2D绘制
            Graphics2D g2d = (Graphics2D) g;
            //设置边框宽度
//            int strokeWidth = 5;
//            g2d.setStroke(new BasicStroke(strokeWidth));
            AlgoVisHelper.setStrokeWidth(g2d, 5);
            //基本图形封装在Ellipse2D对象中
            //设置颜色
//            g2d.setColor(Color.BLUE);
            AlgoVisHelper.setColor(g2d, Color.BLUE);
//            Ellipse2D circle2 = new Ellipse2D.Double(50,50,300,300);
//            //绘制实心圆
//            g2d.fill(circle2);
            //实心圆
            AlgoVisHelper.fillCircle(g2d, canvasWidth/2, canvasHeight/2, 200);
            //设置颜色
//            g2d.setColor(Color.RED);
            AlgoVisHelper.setColor(g2d, Color.RED);
            //图形坐标 左上角坐标,长和宽
//            Ellipse2D circle = new Ellipse2D.Double(50,50,300,300);
//            //绘制空心圆
//            g2d.draw(circle);
            AlgoVisHelper.strokeCircle(g2d, canvasWidth/2, canvasHeight/2, 200);
        }
        //代替第17行代码
        // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        //根据面板自动调整窗口大小
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }
    }
}

测试效果

import java.awt.*;
public class Test2 {
    public static void main(String[] args) {
        //事件分发线程
        EventQueue.invokeLater(() -> {
            AlgoFrame frame = new AlgoFrame("Welcome", 500, 500);
//            AlgoFrame frame = new AlgoFrame("Welcome");
        });
    }
}

6.抗锯齿+双缓存

抗锯齿

官网地址:

https://docs.oracle.com/javase/tutorial/2d/advanced/quality.html

import com.juidemo.tools.AlgoVisHelper;
import javax.swing.*;
import java.awt.*;
public class AlgoFrame extends JFrame {
    private int canvasWidth;
    private int canvasHeight;
    public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
        super(title);
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;
        //画布相关
        AlgoCanvas canvas = new AlgoCanvas();
//        canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        this.setContentPane(canvas);
        //跟进加载进内容的大小,调整窗口的大小
        this.pack();
        //窗口大小 删除这行代码,使用上面画布的大小
//        this.setSize(canvasWidth, canvasHeight);
        //是否可改变窗口大小
        this.setResizable(false);
        //关闭的时候,传递的功能
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示jframe窗口
        this.setVisible(true);
    }
    public AlgoFrame(String title) throws HeadlessException {
        this(title, 1024, 768);
    }
    public int getCanvasWidth() {
        return canvasWidth;
    }
    public int getCanvasHeight() {
        return canvasHeight;
    }
    private class AlgoCanvas extends JPanel {
        //绘制组件
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
//            g.drawOval(50,50,300,300);
            //Graphics2D绘制
            Graphics2D g2d = (Graphics2D) g;
            //抗锯齿
            RenderingHints hints = new RenderingHints(
                                            RenderingHints.KEY_ANTIALIASING,
                                            RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.addRenderingHints(hints);
            //设置边框宽度
//            int strokeWidth = 5;
//            g2d.setStroke(new BasicStroke(strokeWidth));
            AlgoVisHelper.setStrokeWidth(g2d, 5);
            //基本图形封装在Ellipse2D对象中
            //设置颜色
//            g2d.setColor(Color.BLUE);
            AlgoVisHelper.setColor(g2d, Color.BLUE);
//            Ellipse2D circle2 = new Ellipse2D.Double(50,50,300,300);
//            //绘制实心圆
//            g2d.fill(circle2);
            //实心圆
            AlgoVisHelper.fillCircle(g2d, canvasWidth/2, canvasHeight/2, 200);
            //设置颜色
//            g2d.setColor(Color.RED);
            AlgoVisHelper.setColor(g2d, Color.RED);
            //图形坐标 左上角坐标,长和宽
//            Ellipse2D circle = new Ellipse2D.Double(50,50,300,300);
//            //绘制空心圆
//            g2d.draw(circle);
            AlgoVisHelper.strokeCircle(g2d, canvasWidth/2, canvasHeight/2, 200);
        }
        //代替第17行代码
        // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
        //根据面板自动调整窗口大小
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }
    }
}

关键代码:

//抗锯齿
            RenderingHints hints = new RenderingHints(
                                            RenderingHints.KEY_ANTIALIASING,
                                            RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.addRenderingHints(hints);

效果:

双缓存

jPanel默认实现了双缓存


相关文章
|
人工智能
新手必看,写歌词的技巧和方法新分享,妙笔生词AI智能写歌词软件
对于新手,写歌词不再难。本文分享了写歌词的实用技巧,如积累生活素材、明确主题、合理安排主副歌、简洁有力的语言表达等。推荐使用“妙笔生词智能写歌词软件”,其AI功能可助你灵感不断,轻松创作。
|
数据挖掘 Linux 数据处理
Linux命令shuf详解:随机排序与数据分析的得力助手
`shuf`是Linux的命令行工具,用于随机排序和抽样数据。它能对文件或标准输入进行随机处理,适用于数据测试、播放列表和样本选择。主要参数包括:-e处理命令行输入,-i指定数字范围,-n选择行数,-o输出到文件,-r允许重复,-z用NULL分隔。结合其他命令使用能增强其功能。注意输出重定向和随机性的保证。是数据分析的有力助手。
|
11月前
|
缓存 Ubuntu 网络安全
使用 Docker 快速搭建最新版 Flarum 论坛
本文分享了使用Docker在4核4GB的Ubuntu 20.04云服务器上搭建Flarum轻论坛的经验。通过Nginx-Proxy和ACME伴侣自动配置SSL,并使用Docker Compose部署Flarum及MariaDB容器。关键步骤包括:创建Nginx-Proxy容器、配置Flarum容器及其环境变量、设置桥网络连接以及更新Flarum版本。文中提供了详细的Docker Compose配置示例和必要的环境变量设置,帮助读者顺利搭建并运行Flarum论坛。
|
存储 安全 搜索推荐
Elasticsearch 近实时
【11月更文挑战第1天】
263 5
|
JavaScript
vue3表格编辑(数据回显)和删除功能实现
vue3表格编辑(数据回显)和删除功能实现
344 1
|
机器学习/深度学习 PyTorch 算法框架/工具
时间序列pytorch搭建lstm用电量预测 完整代码数据
时间序列pytorch搭建lstm用电量预测 完整代码数据
976 0
|
消息中间件 数据库 RocketMQ
分布式事物【库存微服务业务层实现、实现充值微服务、充值微服务之业务层实现、账户微服务之业务层实现】(九)-全面详解(学习总结---从入门到深化)
分布式事物【库存微服务业务层实现、实现充值微服务、充值微服务之业务层实现、账户微服务之业务层实现】(九)-全面详解(学习总结---从入门到深化)
324 0
|
关系型数据库 索引 Perl
理解 postgresql.conf 的work_mem 参数配置
主要是通过具体的实验来理解 work_mem
7337 0
|
存储 JSON Java
Java基于API接口爬取淘宝商品数据
随着互联网的普及和电子商务的快速发展,越来越多的商家选择在淘宝等电商平台上销售商品。对于开发者来说,通过API接口获取淘宝商品数据,可以更加便捷地进行数据分析和商业决策。本文将介绍如何使用Java基于淘宝API接口爬取商品数据,包括请求API、解析JSON数据、存储数据等步骤,并提供相应的代码示例
|
资源调度 前端开发 JavaScript
Vite教程 安装
Vite教程 安装
1718 0
Vite教程 安装