|
| 1 | +import fastimage.FastImage; |
| 2 | +import javax.swing.*; |
| 3 | +import java.awt.*; |
| 4 | +import java.awt.image.BufferedImage; |
| 5 | + |
| 6 | +/** |
| 7 | + * Optimierte Blur-Animation: |
| 8 | + * - Bild wird vorher kleiner resized (Performance!) |
| 9 | + * - Kein dispose/create pro Frame (wiederverwendet) |
| 10 | + * - Direkte Pixel-Manipulation statt ToBufferedImage |
| 11 | + */ |
| 12 | +public class FastBlurAnimation extends JFrame implements Runnable { |
| 13 | + |
| 14 | + private BufferedImage originalSmall; // Kleines Bild für Performance |
| 15 | + private FastImage fastImage; |
| 16 | + private JLabel imageLabel; |
| 17 | + private volatile boolean running = true; |
| 18 | + private float currentRadius = 0f; |
| 19 | + private long startTime; |
| 20 | + |
| 21 | + private static final float MIN_RADIUS = 0.0f; |
| 22 | + private static final float MAX_RADIUS = 15.0f; |
| 23 | + private static final float CYCLE_MS = 3000f; // 3 Sekunden = schneller |
| 24 | + private static final int TARGET_SIZE = 400; // Max 400px = viel schneller! |
| 25 | + |
| 26 | + public FastBlurAnimation(BufferedImage original) { |
| 27 | + super("Fast Blur Animation - Optimized"); |
| 28 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 29 | + |
| 30 | + // BILD VORHER KLEINER MACHEN (CRUCIAL!) |
| 31 | + int w = original.getWidth(); |
| 32 | + int h = original.getHeight(); |
| 33 | + int maxDim = Math.max(w, h); |
| 34 | + |
| 35 | + if (maxDim > TARGET_SIZE) { |
| 36 | + double scale = (double)TARGET_SIZE / maxDim; |
| 37 | + w = (int)(w * scale); |
| 38 | + h = (int)(h * scale); |
| 39 | + System.out.println("[OPTIMIZED] Resizing to " + w + "x" + h + " for performance"); |
| 40 | + |
| 41 | + // Schneller Resize mit FastImage |
| 42 | + FastImage temp = FastImage.fromBufferedImage(original); |
| 43 | + temp.resize(w, h); |
| 44 | + originalSmall = temp.toBufferedImage(); |
| 45 | + temp.dispose(); |
| 46 | + } else { |
| 47 | + originalSmall = original; |
| 48 | + } |
| 49 | + |
| 50 | + // Einmalig erstellen - wiederverwenden! |
| 51 | + fastImage = FastImage.fromBufferedImage(originalSmall); |
| 52 | + |
| 53 | + imageLabel = new JLabel(); |
| 54 | + imageLabel.setHorizontalAlignment(JLabel.CENTER); |
| 55 | + updateImage(originalSmall); |
| 56 | + add(imageLabel, BorderLayout.CENTER); |
| 57 | + |
| 58 | + pack(); |
| 59 | + // Links positionieren |
| 60 | + Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); |
| 61 | + int x = (screen.width / 2) - getWidth() - 10; |
| 62 | + int y = (screen.height - getHeight()) / 2; |
| 63 | + setLocation(x, y); |
| 64 | + setVisible(true); |
| 65 | + |
| 66 | + startTime = System.currentTimeMillis(); |
| 67 | + new Thread(this).start(); |
| 68 | + } |
| 69 | + |
| 70 | + private void updateImage(BufferedImage img) { |
| 71 | + imageLabel.setIcon(new ImageIcon(img)); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void run() { |
| 76 | + System.out.println("[DEBUG] Animation thread started"); |
| 77 | + long lastFpsUpdate = startTime; |
| 78 | + int frameCount = 0; |
| 79 | + int fps = 0; |
| 80 | + int blurCount = 0; |
| 81 | + long totalBlurTime = 0; |
| 82 | + |
| 83 | + while (running) { |
| 84 | + long loopStart = System.nanoTime(); |
| 85 | + long now = System.currentTimeMillis(); |
| 86 | + float elapsed = now - startTime; |
| 87 | + |
| 88 | + // Sinus-Welle |
| 89 | + float sin = (float)Math.sin(elapsed / CYCLE_MS * 2 * Math.PI); |
| 90 | + float norm = (sin + 1f) / 2f; |
| 91 | + float targetRadius = MIN_RADIUS + norm * (MAX_RADIUS - MIN_RADIUS); |
| 92 | + |
| 93 | + // Nur updaten wenn Radius sich genug ändert |
| 94 | + if (Math.abs(targetRadius - currentRadius) > 0.2f) { |
| 95 | + currentRadius = targetRadius; |
| 96 | + blurCount++; |
| 97 | + |
| 98 | + long t1 = System.nanoTime(); |
| 99 | + fastImage.dispose(); |
| 100 | + long t2 = System.nanoTime(); |
| 101 | + fastImage = FastImage.fromBufferedImage(originalSmall); |
| 102 | + long t3 = System.nanoTime(); |
| 103 | + fastImage.blur(currentRadius); |
| 104 | + long t4 = System.nanoTime(); |
| 105 | + BufferedImage blurred = fastImage.toBufferedImage(); |
| 106 | + long t5 = System.nanoTime(); |
| 107 | + |
| 108 | + long disposeMs = (t2 - t1) / 1_000_000; |
| 109 | + long createMs = (t3 - t2) / 1_000_000; |
| 110 | + long blurMs = (t4 - t3) / 1_000_000; |
| 111 | + long toBufMs = (t5 - t4) / 1_000_000; |
| 112 | + long totalMs = (t5 - t1) / 1_000_000; |
| 113 | + totalBlurTime += totalMs; |
| 114 | + |
| 115 | + // Nur alle 30 frames ausgeben (nicht zu viel spam) |
| 116 | + if (blurCount % 30 == 1) { |
| 117 | + System.out.println(String.format( |
| 118 | + "[TIMING] dispose=%dms create=%dms blur=%dms toBuf=%dms TOTAL=%dms (avg=%.1fms)", |
| 119 | + disposeMs, createMs, blurMs, toBufMs, totalMs, |
| 120 | + (double)totalBlurTime / blurCount)); |
| 121 | + } |
| 122 | + |
| 123 | + BufferedImage blurred = fastImage.toBufferedImage(); |
| 124 | + |
| 125 | + final float r = currentRadius; |
| 126 | + final int ms = (int)totalMs; |
| 127 | + final int fpsFinal = fps; |
| 128 | + |
| 129 | + SwingUtilities.invokeLater(() -> { |
| 130 | + updateImage(blurred); |
| 131 | + setTitle(String.format("Fast: r=%.1f t=%dms fps=%d", r, ms, fpsFinal)); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + frameCount++; |
| 136 | + if (now - lastFpsUpdate >= 1000) { |
| 137 | + fps = frameCount; |
| 138 | + frameCount = 0; |
| 139 | + lastFpsUpdate = now; |
| 140 | + } |
| 141 | + |
| 142 | + try { |
| 143 | + Thread.sleep(8); // ~120fps target (warten auf Blur) |
| 144 | + } catch (InterruptedException e) { |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + fastImage.dispose(); |
| 150 | + } |
| 151 | + |
| 152 | + public static void main(String[] args) { |
| 153 | + BufferedImage img = DemoUtils.createTestImage(); |
| 154 | + SwingUtilities.invokeLater(() -> new FastBlurAnimation(img)); |
| 155 | + } |
| 156 | +} |
0 commit comments