搞定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);
        });
    }
}


运行结果:

1dc618a0ed9580ce8bfa6facb208c08f.png

改进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.设置画布与图形绘制基础


1dc618a0ed9580ce8bfa6facb208c08f.png5d4c6812c8535adbb050f4ddf2e1bce8.png

案例:绘制一个圆。


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");
        });
    }
}


效果图:

1dc618a0ed9580ce8bfa6facb208c08f.png


4.使用Graphics2D


5d4c6812c8535adbb050f4ddf2e1bce8.png

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);
        }
    }
}


效果:

1dc618a0ed9580ce8bfa6facb208c08f.png


整理绘制工具类


5d4c6812c8535adbb050f4ddf2e1bce8.png46a9d80a6e05e4e3b19d57a0ee70bcdf.png

工具类:



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");
        });
    }
}


1dc618a0ed9580ce8bfa6facb208c08f.png


6.抗锯齿+双缓存


抗锯齿


5d4c6812c8535adbb050f4ddf2e1bce8.png

官网地址:

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


46a9d80a6e05e4e3b19d57a0ee70bcdf.png


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);

效果:

1dc618a0ed9580ce8bfa6facb208c08f.png


双缓存


5d4c6812c8535adbb050f4ddf2e1bce8.png


jPanel默认实现了双缓存

46a9d80a6e05e4e3b19d57a0ee70bcdf.png66ba272a0bfc97be54a5fa679e3d5482.png




相关文章
|
8月前
|
缓存 Java
搞定JUI编程
搞定JUI编程
|
8月前
|
人工智能 JavaScript 前端开发
NUS CS1101S:SICP JavaScript 描述:前言、序言和致谢
NUS CS1101S:SICP JavaScript 描述:前言、序言和致谢
70 0
OpenJudge NOI 1.11 05:派
OpenJudge NOI 1.11 05:派
103 0
|
供应链 安全 区块链
在 Gear 上开发的案例有哪些?
Gear 是一个完备的区块链网络,也是 Polkadot 和 Kusama 上最先进的智能合约平台。它能够使开发者用最简单、最高效的方式部署去中心化应用。在 Gear 上,所有智能合约都是用不同编程语言编译的 WebAssembly 程序。这大大降低了 dApp 开发者的准入门槛,对区块链的编程语言不太熟悉的开发者们,可以在熟悉的编程语言环境中构建去中心化应用。
121 0
 在 Gear 上开发的案例有哪些?
|
SQL 人工智能 自动驾驶
Jeff Dean只是冰山一角!盘点劈柴哥的17个「贤内助」
最近,Business Insider披露了谷歌内部最新的组织结构图,CEO皮采的核心团队成员曝光,其中不仅包括谷歌AI负责人Jeff Dean,还有众多资深高管,一起来看看谷歌这个1.3万亿美元市值的科技巨头的掌舵团队吧。
232 0
Jeff Dean只是冰山一角!盘点劈柴哥的17个「贤内助」
|
算法 Unix 编译器
哭了!2020图灵奖颁给编程的回忆——Jeff Dean 的编译启蒙书(中)
刚刚,2020年图灵奖揭晓!影响了数代人的「龙书」作者——阿尔佛雷德·艾侯 (Alfred Aho)和杰弗里·戴维·乌尔曼(Jeffrey David Ullman)获奖。
485 0
哭了!2020图灵奖颁给编程的回忆——Jeff Dean 的编译启蒙书(中)
|
机器学习/深度学习 JavaScript 前端开发
哭了!2020图灵奖颁给编程的回忆——Jeff Dean 的编译启蒙书(下)
刚刚,2020年图灵奖揭晓!影响了数代人的「龙书」作者——阿尔佛雷德·艾侯 (Alfred Aho)和杰弗里·戴维·乌尔曼(Jeffrey David Ullman)获奖。
225 0
哭了!2020图灵奖颁给编程的回忆——Jeff Dean 的编译启蒙书(下)
|
算法 编译器 程序员
哭了!2020图灵奖颁给编程的回忆——Jeff Dean 的编译启蒙书(上)
刚刚,2020年图灵奖揭晓!影响了数代人的「龙书」作者——阿尔佛雷德·艾侯 (Alfred Aho)和杰弗里·戴维·乌尔曼(Jeffrey David Ullman)获奖。
225 0
哭了!2020图灵奖颁给编程的回忆——Jeff Dean 的编译启蒙书(上)
|
机器学习/深度学习 算法
台湾大学林轩田机器学习基石课程学习笔记7 -- The VC Dimension
前几节课着重介绍了机器能够学习的条件并做了详细的推导和解释。
181 0
台湾大学林轩田机器学习基石课程学习笔记7 -- The VC Dimension