搞定JUI编程

深藏阁楼爱情的钟 2022-09-10 05:17 201阅读 0赞

文章目录

    • 1.初始Jframe
    • 2.创建属于自己的第一个frame子类
    • 3.设置画布与图形绘制基础
    • 4.使用Graphics2D
    • 整理绘制工具类
    • 6.抗锯齿+双缓存
      • 抗锯齿
      • 双缓存

1.初始Jframe

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class Test1 {
  4. public static void main(String[] args) {
  5. //事件分发线程
  6. EventQueue.invokeLater(() -> {
  7. JFrame frame = new JFrame("Welcome");
  8. //窗口大小
  9. frame.setSize(500, 500);
  10. //是否可改变窗口大小
  11. frame.setResizable(false);
  12. //关闭的时候,传递的功能
  13. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. //显示jframe窗口
  15. frame.setVisible(true);
  16. });
  17. }
  18. }

在这里插入图片描述

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

AlgoFrame

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class AlgoFrame extends JFrame {
  4. public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
  5. super(title);
  6. //窗口大小
  7. this.setSize(canvasWidth, canvasHeight);
  8. //是否可改变窗口大小
  9. this.setResizable(false);
  10. //关闭的时候,传递的功能
  11. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12. //显示jframe窗口
  13. this.setVisible(true);
  14. }
  15. }

测试

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

运行结果:
在这里插入图片描述
改进iframe

  1. package com.juidemo.algoframe;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. public class AlgoFrame extends JFrame {
  5. private int canvasWidth;
  6. private int canvasHeight;
  7. public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
  8. super(title);
  9. this.canvasWidth = canvasWidth;
  10. this.canvasHeight = canvasHeight;
  11. //窗口大小
  12. this.setSize(canvasWidth, canvasHeight);
  13. //是否可改变窗口大小
  14. this.setResizable(false);
  15. //关闭的时候,传递的功能
  16. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17. //显示jframe窗口
  18. this.setVisible(true);
  19. }
  20. public AlgoFrame(String title) throws HeadlessException {
  21. this(title, 1024, 768);
  22. }
  23. public int getCanvasWidth() {
  24. return canvasWidth;
  25. }
  26. public int getCanvasHeight() {
  27. return canvasHeight;
  28. }
  29. }

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

在这里插入图片描述
在这里插入图片描述
案例:绘制一个圆。

  1. package com.juidemo.algoframe;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. public class AlgoFrame extends JFrame {
  5. private int canvasWidth;
  6. private int canvasHeight;
  7. public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
  8. super(title);
  9. this.canvasWidth = canvasWidth;
  10. this.canvasHeight = canvasHeight;
  11. //画布相关
  12. AlgoCanvas canvas = new AlgoCanvas();
  13. // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  14. this.setContentPane(canvas);
  15. //跟进加载进内容的大小,调整窗口的大小
  16. this.pack();
  17. //窗口大小 删除这行代码,使用上面画布的大小
  18. // this.setSize(canvasWidth, canvasHeight);
  19. //是否可改变窗口大小
  20. this.setResizable(false);
  21. //关闭的时候,传递的功能
  22. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23. //显示jframe窗口
  24. this.setVisible(true);
  25. }
  26. public AlgoFrame(String title) throws HeadlessException {
  27. this(title, 1024, 768);
  28. }
  29. public int getCanvasWidth() {
  30. return canvasWidth;
  31. }
  32. public int getCanvasHeight() {
  33. return canvasHeight;
  34. }
  35. private class AlgoCanvas extends JPanel {
  36. //绘制组件,绘制一个圆
  37. @Override
  38. public void paintComponent(Graphics g){
  39. super.paintComponent(g);
  40. g.drawOval(50,50,300,300);
  41. }
  42. //代替第17行代码
  43. // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  44. //根据面板自动调整窗口大小
  45. @Override
  46. public Dimension getPreferredSize(){
  47. return new Dimension(canvasWidth, canvasHeight);
  48. }
  49. }
  50. }

测试

  1. import java.awt.*;
  2. public class Test2 {
  3. public static void main(String[] args) {
  4. //事件分发线程
  5. EventQueue.invokeLater(() -> {
  6. AlgoFrame frame = new AlgoFrame("Welcome", 500, 500);
  7. // AlgoFrame frame = new AlgoFrame("Welcome");
  8. });
  9. }
  10. }

效果图:
在这里插入图片描述

4.使用Graphics2D

在这里插入图片描述

  1. package com.juidemo.algoframe;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.geom.Ellipse2D;
  5. public class AlgoFrame extends JFrame {
  6. private int canvasWidth;
  7. private int canvasHeight;
  8. public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
  9. super(title);
  10. this.canvasWidth = canvasWidth;
  11. this.canvasHeight = canvasHeight;
  12. //画布相关
  13. AlgoCanvas canvas = new AlgoCanvas();
  14. // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  15. this.setContentPane(canvas);
  16. //跟进加载进内容的大小,调整窗口的大小
  17. this.pack();
  18. //窗口大小 删除这行代码,使用上面画布的大小
  19. // this.setSize(canvasWidth, canvasHeight);
  20. //是否可改变窗口大小
  21. this.setResizable(false);
  22. //关闭的时候,传递的功能
  23. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. //显示jframe窗口
  25. this.setVisible(true);
  26. }
  27. public AlgoFrame(String title) throws HeadlessException {
  28. this(title, 1024, 768);
  29. }
  30. public int getCanvasWidth() {
  31. return canvasWidth;
  32. }
  33. public int getCanvasHeight() {
  34. return canvasHeight;
  35. }
  36. private class AlgoCanvas extends JPanel {
  37. //绘制组件
  38. @Override
  39. public void paintComponent(Graphics g){
  40. super.paintComponent(g);
  41. // g.drawOval(50,50,300,300);
  42. //Graphics2D绘制
  43. Graphics2D g2d = (Graphics2D) g;
  44. //设置边框宽度
  45. int strokeWidth = 5;
  46. g2d.setStroke(new BasicStroke(strokeWidth));
  47. //基本图形封装在Ellipse2D对象中
  48. //设置颜色
  49. g2d.setColor(Color.RED);
  50. //图形坐标 左上角坐标,长和宽
  51. Ellipse2D circle = new Ellipse2D.Double(50,50,300,300);
  52. //绘制空心圆
  53. g2d.draw(circle);
  54. //设置颜色
  55. g2d.setColor(Color.BLUE);
  56. Ellipse2D circle2 = new Ellipse2D.Double(50,50,300,300);
  57. //绘制实心圆
  58. g2d.fill(circle2);
  59. }
  60. //代替第17行代码
  61. // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  62. //根据面板自动调整窗口大小
  63. @Override
  64. public Dimension getPreferredSize(){
  65. return new Dimension(canvasWidth, canvasHeight);
  66. }
  67. }
  68. }

测试:

  1. package com.juidemo.algoframe;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.geom.Ellipse2D;
  5. public class AlgoFrame extends JFrame {
  6. private int canvasWidth;
  7. private int canvasHeight;
  8. public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
  9. super(title);
  10. this.canvasWidth = canvasWidth;
  11. this.canvasHeight = canvasHeight;
  12. //画布相关
  13. AlgoCanvas canvas = new AlgoCanvas();
  14. // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  15. this.setContentPane(canvas);
  16. //跟进加载进内容的大小,调整窗口的大小
  17. this.pack();
  18. //窗口大小 删除这行代码,使用上面画布的大小
  19. // this.setSize(canvasWidth, canvasHeight);
  20. //是否可改变窗口大小
  21. this.setResizable(false);
  22. //关闭的时候,传递的功能
  23. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. //显示jframe窗口
  25. this.setVisible(true);
  26. }
  27. public AlgoFrame(String title) throws HeadlessException {
  28. this(title, 1024, 768);
  29. }
  30. public int getCanvasWidth() {
  31. return canvasWidth;
  32. }
  33. public int getCanvasHeight() {
  34. return canvasHeight;
  35. }
  36. private class AlgoCanvas extends JPanel {
  37. //绘制组件
  38. @Override
  39. public void paintComponent(Graphics g){
  40. super.paintComponent(g);
  41. // g.drawOval(50,50,300,300);
  42. //Graphics2D绘制
  43. Graphics2D g2d = (Graphics2D) g;
  44. //设置边框宽度
  45. int strokeWidth = 5;
  46. g2d.setStroke(new BasicStroke(strokeWidth));
  47. //基本图形封装在Ellipse2D对象中
  48. //设置颜色
  49. g2d.setColor(Color.RED);
  50. //图形坐标 左上角坐标,长和宽
  51. Ellipse2D circle = new Ellipse2D.Double(50,50,300,300);
  52. //绘制空心圆
  53. g2d.draw(circle);
  54. //设置颜色
  55. g2d.setColor(Color.BLUE);
  56. Ellipse2D circle2 = new Ellipse2D.Double(50,50,300,300);
  57. //绘制实心圆
  58. g2d.fill(circle2);
  59. }
  60. //代替第17行代码
  61. // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  62. //根据面板自动调整窗口大小
  63. @Override
  64. public Dimension getPreferredSize(){
  65. return new Dimension(canvasWidth, canvasHeight);
  66. }
  67. }
  68. }

效果:
在这里插入图片描述

整理绘制工具类

在这里插入图片描述
在这里插入图片描述
工具类:

  1. import java.awt.*;
  2. import java.awt.geom.Ellipse2D;
  3. public class AlgoVisHelper {
  4. private AlgoVisHelper(){ }
  5. public static void setStrokeWidth(Graphics2D g2d, int w){
  6. int strokeWidth = w;
  7. //第二个参数,绘制的线条的端点是圆形的
  8. //第3个参数,在拐弯的时候,画出来的是平滑的实线
  9. g2d.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
  10. }
  11. public static void setColor(Graphics2D g2d, Color color){
  12. g2d.setColor(color);
  13. }
  14. public static void strokeCircle(Graphics2D g2d, int x, int y, int r){
  15. Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
  16. g2d.draw(circle);
  17. }
  18. public static void fillCircle(Graphics2D g2d, int x, int y, int r){
  19. Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
  20. g2d.fill(circle);
  21. }
  22. }

改造原来的代码

  1. import com.juidemo.tools.AlgoVisHelper;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. public class AlgoFrame extends JFrame {
  5. private int canvasWidth;
  6. private int canvasHeight;
  7. public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
  8. super(title);
  9. this.canvasWidth = canvasWidth;
  10. this.canvasHeight = canvasHeight;
  11. //画布相关
  12. AlgoCanvas canvas = new AlgoCanvas();
  13. // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  14. this.setContentPane(canvas);
  15. //跟进加载进内容的大小,调整窗口的大小
  16. this.pack();
  17. //窗口大小 删除这行代码,使用上面画布的大小
  18. // this.setSize(canvasWidth, canvasHeight);
  19. //是否可改变窗口大小
  20. this.setResizable(false);
  21. //关闭的时候,传递的功能
  22. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23. //显示jframe窗口
  24. this.setVisible(true);
  25. }
  26. public AlgoFrame(String title) throws HeadlessException {
  27. this(title, 1024, 768);
  28. }
  29. public int getCanvasWidth() {
  30. return canvasWidth;
  31. }
  32. public int getCanvasHeight() {
  33. return canvasHeight;
  34. }
  35. private class AlgoCanvas extends JPanel {
  36. //绘制组件
  37. @Override
  38. public void paintComponent(Graphics g){
  39. super.paintComponent(g);
  40. // g.drawOval(50,50,300,300);
  41. //Graphics2D绘制
  42. Graphics2D g2d = (Graphics2D) g;
  43. //设置边框宽度
  44. // int strokeWidth = 5;
  45. // g2d.setStroke(new BasicStroke(strokeWidth));
  46. AlgoVisHelper.setStrokeWidth(g2d, 5);
  47. //基本图形封装在Ellipse2D对象中
  48. //设置颜色
  49. // g2d.setColor(Color.BLUE);
  50. AlgoVisHelper.setColor(g2d, Color.BLUE);
  51. // Ellipse2D circle2 = new Ellipse2D.Double(50,50,300,300);
  52. // //绘制实心圆
  53. // g2d.fill(circle2);
  54. //实心圆
  55. AlgoVisHelper.fillCircle(g2d, canvasWidth/2, canvasHeight/2, 200);
  56. //设置颜色
  57. // g2d.setColor(Color.RED);
  58. AlgoVisHelper.setColor(g2d, Color.RED);
  59. //图形坐标 左上角坐标,长和宽
  60. // Ellipse2D circle = new Ellipse2D.Double(50,50,300,300);
  61. // //绘制空心圆
  62. // g2d.draw(circle);
  63. AlgoVisHelper.strokeCircle(g2d, canvasWidth/2, canvasHeight/2, 200);
  64. }
  65. //代替第17行代码
  66. // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  67. //根据面板自动调整窗口大小
  68. @Override
  69. public Dimension getPreferredSize(){
  70. return new Dimension(canvasWidth, canvasHeight);
  71. }
  72. }
  73. }

测试效果

  1. import java.awt.*;
  2. public class Test2 {
  3. public static void main(String[] args) {
  4. //事件分发线程
  5. EventQueue.invokeLater(() -> {
  6. AlgoFrame frame = new AlgoFrame("Welcome", 500, 500);
  7. // AlgoFrame frame = new AlgoFrame("Welcome");
  8. });
  9. }
  10. }

在这里插入图片描述

6.抗锯齿+双缓存

抗锯齿

在这里插入图片描述
官网地址:
https://docs.oracle.com/javase/tutorial/2d/advanced/quality.html
在这里插入图片描述

  1. import com.juidemo.tools.AlgoVisHelper;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. public class AlgoFrame extends JFrame {
  5. private int canvasWidth;
  6. private int canvasHeight;
  7. public AlgoFrame(String title, int canvasWidth, int canvasHeight) throws HeadlessException {
  8. super(title);
  9. this.canvasWidth = canvasWidth;
  10. this.canvasHeight = canvasHeight;
  11. //画布相关
  12. AlgoCanvas canvas = new AlgoCanvas();
  13. // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  14. this.setContentPane(canvas);
  15. //跟进加载进内容的大小,调整窗口的大小
  16. this.pack();
  17. //窗口大小 删除这行代码,使用上面画布的大小
  18. // this.setSize(canvasWidth, canvasHeight);
  19. //是否可改变窗口大小
  20. this.setResizable(false);
  21. //关闭的时候,传递的功能
  22. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23. //显示jframe窗口
  24. this.setVisible(true);
  25. }
  26. public AlgoFrame(String title) throws HeadlessException {
  27. this(title, 1024, 768);
  28. }
  29. public int getCanvasWidth() {
  30. return canvasWidth;
  31. }
  32. public int getCanvasHeight() {
  33. return canvasHeight;
  34. }
  35. private class AlgoCanvas extends JPanel {
  36. //绘制组件
  37. @Override
  38. public void paintComponent(Graphics g){
  39. super.paintComponent(g);
  40. // g.drawOval(50,50,300,300);
  41. //Graphics2D绘制
  42. Graphics2D g2d = (Graphics2D) g;
  43. //抗锯齿
  44. RenderingHints hints = new RenderingHints(
  45. RenderingHints.KEY_ANTIALIASING,
  46. RenderingHints.VALUE_ANTIALIAS_ON);
  47. g2d.addRenderingHints(hints);
  48. //设置边框宽度
  49. // int strokeWidth = 5;
  50. // g2d.setStroke(new BasicStroke(strokeWidth));
  51. AlgoVisHelper.setStrokeWidth(g2d, 5);
  52. //基本图形封装在Ellipse2D对象中
  53. //设置颜色
  54. // g2d.setColor(Color.BLUE);
  55. AlgoVisHelper.setColor(g2d, Color.BLUE);
  56. // Ellipse2D circle2 = new Ellipse2D.Double(50,50,300,300);
  57. // //绘制实心圆
  58. // g2d.fill(circle2);
  59. //实心圆
  60. AlgoVisHelper.fillCircle(g2d, canvasWidth/2, canvasHeight/2, 200);
  61. //设置颜色
  62. // g2d.setColor(Color.RED);
  63. AlgoVisHelper.setColor(g2d, Color.RED);
  64. //图形坐标 左上角坐标,长和宽
  65. // Ellipse2D circle = new Ellipse2D.Double(50,50,300,300);
  66. // //绘制空心圆
  67. // g2d.draw(circle);
  68. AlgoVisHelper.strokeCircle(g2d, canvasWidth/2, canvasHeight/2, 200);
  69. }
  70. //代替第17行代码
  71. // canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  72. //根据面板自动调整窗口大小
  73. @Override
  74. public Dimension getPreferredSize(){
  75. return new Dimension(canvasWidth, canvasHeight);
  76. }
  77. }
  78. }

关键代码:

  1. //抗锯齿
  2. RenderingHints hints = new RenderingHints(
  3. RenderingHints.KEY_ANTIALIASING,
  4. RenderingHints.VALUE_ANTIALIAS_ON);
  5. g2d.addRenderingHints(hints);

效果:
在这里插入图片描述

双缓存

在这里插入图片描述

jPanel默认实现了双缓存
在这里插入图片描述
在这里插入图片描述


发表评论

表情:
评论列表 (有 0 条评论,201人围观)

还没有评论,来说两句吧...

相关阅读

    相关 轻松EasyUI

            最近在学习easyUI,顾名思义easyUI很简单,总结一下我在学习EasyUI的时候是怎么学习的,学习的时候主要从四个方面入手:         ①什么是

    相关 需求变化

    坊间流传一句话——“杀一个程序员不用枪,改三次需求就可以了”。问君能有几多愁,恰似调完代码改需求。需求变化是程序员眼中最大的痛,没有之一。 对程序员来讲,最理想的情况是,需求