关于awt/swing组件中paint, update, paintComponent, repaint的经验

本文详细探讨了Java中Applet、JApplet、Frame、JFrame及Panel等组件的绘图机制,包括它们如何响应repaint事件并调用paint、update方法等。同时对比了AWT和Swing组件在双缓冲支持上的不同表现。

1、Applet/JApplet:
 repaint+paint       -- dragged, paint
 repaint+update+paint  -- dragged, update

  Applet:  repaint+paint -- 自动重画
    repaint+update --  不自动重画
  JApplet:都不自动重画

  结论:awt组件中的paint方法自动重画背景,实际上可以说是update方法自动重画背景,由于重写了update方法,所以不自动重画了!!

  双缓冲:update中画背景缓冲,paint中将背景缓冲绘制到前台图形中

2 、Frame/JFrame:
 repaint+paint       -- dragged, paint
 repaint+update+paint  -- dragged, update

  Frame:  repaint+paint -- 自动重画
   repaint+update --  不自动重画
  JFrame: 都不自动重画

  问题:双缓冲时,因为背景Frame/JFrame是不可显示的(isDisplayable()),所以无法执行createImage()方法,难道基于框架的绘图不支持双缓冲?

3、 Panel:
 repaint+paint       -- dragged, paint
 repaint+update+paint  -- dragged, update
 repaint不执行paintComponent

 JPanel:
 repaint+update+paint  -- dragged, update
 repaint+paint+paintComponent  -- dragged, paint
 repaint+paintComponent  -- dragged, paintComponent
 repaint执行paintComponent, 不执行paintComponents, 使用super.paintComponent(g)可达到填充背景的目的。

 
import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.Random; // 粒子类 class Particle { int x, y; int vx, vy; Color color; int life; // 生命周期(帧数) public Particle(int x, int y, int vx, int vy, Color color, int life) { this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.color = color; this.life = life; } } // 烟花类 class Firework { ArrayList<Particle> particles = new ArrayList<>(); boolean alive = true; public Firework(int x, int y) { Random rand = new Random(); int particleCount = rand.nextInt(50) + 100; // 100-150个粒子 // 生日色系(粉、黄、蓝、紫、金、橙) Color[] colors = { new Color(255, 105, 180), new Color(255, 215, 0), new Color(135, 206, 235), new Color(148, 0, 211), new Color(255, 223, 130), new Color(255, 69, 0) }; // 生成粒子(随机方向和速度) for (int i = 0; i < particleCount; i++) { double angle = rand.nextDouble() * Math.PI * 2; // 0-360度 int speed = rand.nextInt(5) + 3; // 速度3-8 int vx = (int) (speed * Math.cos(angle)); int vy = (int) (speed * Math.sin(angle)) - 2; // 向上偏移 Color color = colors[rand.nextInt(colors.length)]; int life = rand.nextInt(60) + 40; // 生命周期40-100帧 particles.add(new Particle(x, y, vx, vy, color, life)); } } // 更新烟花状态(重力影响+生命周期) public void update() { boolean hasAlive = false; for (Particle p : particles) { if (p.life > 0) { p.vy += 0.1; // 重力加速度 p.x += p.vx; p.y += p.vy; p.life--; hasAlive = true; } } alive = hasAlive; } // 绘制烟花 public void draw(Graphics g) { for (Particle p : particles) { if (p.life > 0) { // 生命周期越短,颜色越淡 float alpha = (float) p.life / 100; g.setColor(new Color( p.color.getRed() / 255f * alpha, p.color.getGreen() / 255f * alpha, p.color.getBlue() / 255f * alpha, alpha )); g.fillOval(p.x, p.y, 3, 3); // 粒子大小3x3 } } } } // 主窗口类 public class BirthdayFirework extends JFrame { ArrayList<Firework> fireworks = new ArrayList<>(); Random rand = new Random(); public BirthdayFirework() { // 窗口设置 setTitle("生日烟花"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); // 居中 setResizable(false); // 鼠标点击触发烟花 addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { fireworks.add(new Firework(e.getX(), e.getY())); } }); // 动画循环(每秒60帧) new Timer(16, e -> { // 更新所有烟花状态 for (int i = fireworks.size() - 1; i >= 0; i--) { Firework f = fireworks.get(i); f.update(); if (!f.alive) { fireworks.remove(i); // 移除消亡的烟花 } } repaint(); // 重绘窗口 }).start(); } // 绘制界面 @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 黑色背景 g.setColor(Color.BLACK); g.fillRect(0, 0, getWidth(), getHeight()); // 绘制生日文字 g.setColor(Color.WHITE); g.setFont(new Font("微软雅黑", Font.BOLD, 40)); g.drawString("生日快乐!", 280, 60); // 绘制所有烟花 for (Firework f : fireworks) { f.draw(g); } } public static void main(String[] args) { // 启动程序( Swing 需在 EDT 线程中运行) SwingUtilities.invokeLater(() -> new BirthdayFirework().setVisible(true)); } }
11-22
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值