Is there a way to draw to the screen faster with StdDraw in java?
This is not the final answer, but a way to put some measurable code on the table. BufferedImage
is the way to go, but for reference on my not-too-fast system, this code takes 5 seconds to draw 40 million pixels, or 125 milliseconds per megapixel. How may pixels do you have?!? This code uses a call to line
as a proxy for drawing a single pixel because the class doesn't offer single pixel drawing, but theoretically code that draws an actual pixel like you do should be faster.
What class is YOUR map
?
How many pixels?
What amount of time is "insanely slow"?
public class SandBox {
public static void main (String [] args) {
boolean [] [] map = new boolean [1000] [1000];
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
map [i] [j] = true;
}
}
JFrame myFrame = new JFrame ();
myFrame.setSize (1100, 1100);
SPanel myPanel = new SPanel ();
myPanel.myMap = map;
myFrame.add (myPanel);
myFrame.setVisible (true);
}
} // class SandBox
class SPanel extends JPanel {
public boolean [] [] myMap;
@Override
public void paintComponent (Graphics g) {
g.drawString ("Hello", 30, 30);
Instant start = Instant.now ();
for (int i = 0; i < 10; i++) {
g.setColor (new Color (0, 0, 0));
drawMap (g);
g.setColor (new Color (255, 0, 0));
drawMap (g);
g.setColor (new Color (0, 255, 0));
drawMap (g);
g.setColor (new Color (0, 0, 255));
drawMap (g);
}
reportElapsed (start);
g.drawRect (50, 50, 1000, 1000);
}
void drawMap (Graphics g) {
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
if (myMap [i] [j] == true) {
g.drawLine (i + 50, j + 50, i + 50, j + 50);
}
}
}
}
private static void reportElapsed (Instant start) {
Duration elapsed = Duration.between (start, Instant.now ());
long millis = elapsed.toMillis ();
System.out.println ("Duration (ms): " + millis + ".");
} // reportElapsed
} // class SPanel