mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-11 01:32:08 +10:00
use volatile image to optimize
This commit is contained in:
+42
-6
@@ -3,7 +3,10 @@ package org.diylc.swing.plugins.cloud.view.browser;
|
|||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Cursor;
|
import java.awt.Cursor;
|
||||||
import java.awt.FontMetrics;
|
import java.awt.FontMetrics;
|
||||||
|
import java.awt.Graphics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.GraphicsConfiguration;
|
||||||
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
import java.awt.RenderingHints;
|
import java.awt.RenderingHints;
|
||||||
@@ -13,6 +16,7 @@ import java.awt.event.KeyEvent;
|
|||||||
import java.awt.event.KeyListener;
|
import java.awt.event.KeyListener;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.MouseListener;
|
import java.awt.event.MouseListener;
|
||||||
|
import java.awt.image.VolatileImage;
|
||||||
|
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.Timer;
|
import javax.swing.Timer;
|
||||||
@@ -32,15 +36,21 @@ public class CloudGlassPane extends JPanel implements MouseListener, KeyListener
|
|||||||
|
|
||||||
private long startTime;
|
private long startTime;
|
||||||
private final int msPerPx = 6;
|
private final int msPerPx = 6;
|
||||||
|
private final Color bgColor = new Color(192, 222, 237);
|
||||||
|
|
||||||
private Timer animationTimer;
|
private Timer animationTimer;
|
||||||
|
|
||||||
|
private VolatileImage volatileImg;
|
||||||
|
|
||||||
public CloudGlassPane() {
|
public CloudGlassPane() {
|
||||||
addKeyListener(this);
|
addKeyListener(this);
|
||||||
addMouseListener(this);
|
addMouseListener(this);
|
||||||
setCursor(new Cursor(Cursor.WAIT_CURSOR));
|
setCursor(new Cursor(Cursor.WAIT_CURSOR));
|
||||||
setOpaque(false);
|
setOpaque(false);
|
||||||
setBackground(new Color(192, 222, 237));
|
setBackground(bgColor);
|
||||||
|
|
||||||
|
setLayout(new GridBagLayout());
|
||||||
|
|
||||||
|
|
||||||
animationTimer = new Timer(20, new ActionListener() {
|
animationTimer = new Timer(20, new ActionListener() {
|
||||||
|
|
||||||
@@ -61,14 +71,35 @@ public class CloudGlassPane extends JPanel implements MouseListener, KeyListener
|
|||||||
super.setVisible(aFlag);
|
super.setVisible(aFlag);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private void createBackBuffer() {
|
||||||
|
GraphicsConfiguration gc = getGraphicsConfiguration();
|
||||||
|
volatileImg = gc.createCompatibleVolatileImage(getWidth(), getHeight());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void paint(java.awt.Graphics g) {
|
public void paint(java.awt.Graphics g) {
|
||||||
Graphics2D g2d = (Graphics2D) g;
|
|
||||||
// g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
|
|
||||||
|
|
||||||
|
createBackBuffer();
|
||||||
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
// Validate the volatile image for the graphics configuration of this
|
||||||
|
// component. If the volatile image doesn't apply for this graphics configuration
|
||||||
|
// (in other words, the hardware acceleration doesn't apply for the new device)
|
||||||
|
// then we need to re-create it.
|
||||||
|
GraphicsConfiguration gc = this.getGraphicsConfiguration();
|
||||||
|
int valCode = volatileImg.validate(gc);
|
||||||
|
|
||||||
|
// This means the device doesn't match up to this hardware accelerated image.
|
||||||
|
if (valCode == VolatileImage.IMAGE_INCOMPATIBLE) {
|
||||||
|
createBackBuffer(); // recreate the hardware accelerated image.
|
||||||
|
}
|
||||||
|
|
||||||
|
Graphics offscreenGraphics = volatileImg.getGraphics();
|
||||||
|
Graphics2D g2d = (Graphics2D) offscreenGraphics;
|
||||||
Image cloud = IconLoader.CloudBg.getImage();
|
Image cloud = IconLoader.CloudBg.getImage();
|
||||||
|
|
||||||
g2d.setColor(new Color(192, 222, 237));
|
g2d.setColor(bgColor);
|
||||||
g2d.fillRect(0, cloud.getHeight(null), getWidth(), getHeight());
|
g2d.fillRect(0, cloud.getHeight(null), getWidth(), getHeight());
|
||||||
|
|
||||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
@@ -87,8 +118,13 @@ public class CloudGlassPane extends JPanel implements MouseListener, KeyListener
|
|||||||
Rectangle rect = new Rectangle(getSize());
|
Rectangle rect = new Rectangle(getSize());
|
||||||
int x = (rect.width - metrics.stringWidth(text)) / 2;
|
int x = (rect.width - metrics.stringWidth(text)) / 2;
|
||||||
int y = ((rect.height - metrics.getHeight()) / 2) + metrics.getAscent();
|
int y = ((rect.height - metrics.getHeight()) / 2) + metrics.getAscent();
|
||||||
g.drawString(text, x, y);
|
g2d.drawString(text, x, y);
|
||||||
};
|
|
||||||
|
// paint back buffer to main graphics
|
||||||
|
g.drawImage(volatileImg, 0, 0, this);
|
||||||
|
// Test if content is lost
|
||||||
|
} while (volatileImg.contentsLost());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(final MouseEvent pArg0) {}
|
public void mouseClicked(final MouseEvent pArg0) {}
|
||||||
|
|||||||
Reference in New Issue
Block a user