mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
painting optimization
This commit is contained in:
+35
-17
@@ -6,6 +6,7 @@ import java.awt.Graphics2D;
|
||||
import java.awt.GraphicsConfiguration;
|
||||
import java.awt.GraphicsDevice;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.Image;
|
||||
import java.awt.Insets;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
@@ -46,9 +47,11 @@ class CanvasPanel extends JComponent implements Autoscroll {
|
||||
|
||||
private IPlugInPort plugInPort;
|
||||
|
||||
private VolatileImage bufferImage;
|
||||
private Image bufferImage;
|
||||
private GraphicsConfiguration screenGraphicsConfiguration;
|
||||
|
||||
private static boolean USE_HARDWARE_ACCELLERATION = false;
|
||||
|
||||
private static final EnumSet<DrawOption> drawOptions = EnumSet.of(DrawOption.GRID,
|
||||
DrawOption.SELECTION, DrawOption.ZOOM, DrawOption.ANTIALIASING,
|
||||
DrawOption.CONTROL_POINTS);
|
||||
@@ -95,9 +98,13 @@ class CanvasPanel extends JComponent implements Autoscroll {
|
||||
}
|
||||
|
||||
protected void createBufferImage() {
|
||||
bufferImage = screenGraphicsConfiguration.createCompatibleVolatileImage(getWidth(),
|
||||
getHeight());
|
||||
bufferImage.validate(screenGraphicsConfiguration);
|
||||
if (USE_HARDWARE_ACCELLERATION) {
|
||||
bufferImage = screenGraphicsConfiguration.createCompatibleVolatileImage(getWidth(),
|
||||
getHeight());
|
||||
((VolatileImage) bufferImage).validate(screenGraphicsConfiguration);
|
||||
} else {
|
||||
bufferImage = createImage(getWidth(), getHeight());
|
||||
}
|
||||
Graphics2D g2d = (Graphics2D) bufferImage.getGraphics();
|
||||
plugInPort.draw(g2d, drawOptions);
|
||||
}
|
||||
@@ -109,21 +116,32 @@ class CanvasPanel extends JComponent implements Autoscroll {
|
||||
}
|
||||
if (bufferImage == null) {
|
||||
createBufferImage();
|
||||
} else {
|
||||
Graphics2D g2d = (Graphics2D) bufferImage.getGraphics();
|
||||
plugInPort.draw(g2d, drawOptions);
|
||||
}
|
||||
do {
|
||||
try {
|
||||
if (bufferImage.contentsLost()) {
|
||||
if (USE_HARDWARE_ACCELLERATION) {
|
||||
VolatileImage volatileImage = (VolatileImage) bufferImage;
|
||||
do {
|
||||
try {
|
||||
if (volatileImage.contentsLost()) {
|
||||
createBufferImage();
|
||||
}
|
||||
int validation = volatileImage.validate(screenGraphicsConfiguration);
|
||||
if (validation == VolatileImage.IMAGE_INCOMPATIBLE) {
|
||||
createBufferImage();
|
||||
}
|
||||
g.drawImage(bufferImage, 0, 0, this);
|
||||
} catch (NullPointerException e) {
|
||||
createBufferImage();
|
||||
}
|
||||
int validation = bufferImage.validate(screenGraphicsConfiguration);
|
||||
if (validation == VolatileImage.IMAGE_INCOMPATIBLE) {
|
||||
createBufferImage();
|
||||
}
|
||||
g.drawImage(bufferImage, 0, 0, this);
|
||||
} catch (NullPointerException e) {
|
||||
createBufferImage();
|
||||
}
|
||||
} while (bufferImage == null || bufferImage.contentsLost());
|
||||
} while (volatileImage == null || volatileImage.contentsLost());
|
||||
} else {
|
||||
Graphics2D g2d = (Graphics2D) bufferImage.getGraphics();
|
||||
plugInPort.draw(g2d, drawOptions);
|
||||
g.drawImage(bufferImage, 0, 0, this);
|
||||
bufferImage.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -141,7 +159,7 @@ class CanvasPanel extends JComponent implements Autoscroll {
|
||||
}
|
||||
});
|
||||
addKeyListener(new KeyAdapter() {
|
||||
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
plugInPort.mouseMoved(getMousePosition(), e.isControlDown(), e.isShiftDown(), e
|
||||
|
||||
-1
@@ -92,7 +92,6 @@ public class CanvasPlugin implements IPlugIn {
|
||||
refreshSize();
|
||||
break;
|
||||
case REPAINT:
|
||||
canvasPanel.invalidateCache();
|
||||
canvasPanel.repaint();
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user