Created
November 2, 2012 10:08
-
-
Save andytill/3999914 to your computer and use it in GitHub Desktop.
A mini benchmark to test if Java compatible images are rendered faster than images loaded in the default manner, results printed on the command line.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.awt.GraphicsConfiguration; | |
import java.awt.GraphicsDevice; | |
import java.awt.GraphicsEnvironment; | |
import java.awt.Image; | |
import java.awt.Transparency; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.imageio.ImageIO; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.SwingUtilities; | |
public class CompatibleImageBenchMark extends JPanel implements Runnable | |
{ | |
public static void main(String[] args) | |
{ | |
SwingUtilities.invokeLater(new CompatibleImageBenchMark()); | |
} | |
Image optimal, suboptimal; | |
public void run() | |
{ | |
try | |
{ | |
optimal = createCompatibeImage(); | |
suboptimal = createSuboptimalImage(); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
JFrame frame; | |
frame = new JFrame("Compatible image benchmark"); | |
frame.setSize(800, 600); | |
frame.setContentPane(this); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setVisible(true); | |
} | |
@Override | |
protected void paintComponent(Graphics g) | |
{ | |
super.paintComponent(g); | |
long start, elapsed; | |
start = System.currentTimeMillis(); | |
for (int i = 0; i < 10; i++) | |
{ | |
g.drawImage(suboptimal, 10, 10, null); | |
} | |
elapsed = System.currentTimeMillis() - start; | |
System.out.println("elapsed for suboptimal image is " + elapsed + "ms"); | |
start = System.currentTimeMillis(); | |
for (int i = 0; i < 10; i++) | |
{ | |
g.drawImage(optimal, 10, 10, null); | |
} | |
elapsed = System.currentTimeMillis() - start; | |
System.out.println("elapsed for optimal image is " + elapsed + "ms"); | |
} | |
private BufferedImage createCompatibeImage() throws IOException | |
{ | |
BufferedImage suboptimalImage = createSuboptimalImage(Color.GREEN); | |
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); | |
GraphicsDevice gs = ge.getDefaultScreenDevice(); | |
GraphicsConfiguration gc = gs.getDefaultConfiguration(); | |
// Create an image that does not support transparency | |
BufferedImage image = gc.createCompatibleImage( | |
suboptimalImage.getWidth(), suboptimalImage.getHeight(), Transparency.TRANSLUCENT | |
); | |
Graphics g; | |
g = image.getGraphics(); | |
g.drawImage(suboptimalImage, 0, 0, null); | |
return image; | |
} | |
private BufferedImage createSuboptimalImage() throws IOException | |
{ | |
BufferedImage suboptimalImage = ImageIO.read(new File("my image.png")); | |
return suboptimalImage; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment