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默认实现了双缓存
完