Skip to content

Instantly share code, notes, and snippets.

@varren
Last active August 29, 2015 14:23
Show Gist options
  • Save varren/f139a7b4d93154b7ff8e to your computer and use it in GitHub Desktop.
Save varren/f139a7b4d93154b7ff8e to your computer and use it in GitHub Desktop.
/*
I dont really have this problem: here is results from your version
Start of drawLines(): 82086490370339
Done drawing thick lines!
Time taken (ns): 76994, Time taken(ms): 0
Done drawing vertical lines!
Time taken (ns): 54858, Time taken(ms): 0
Done drawing horizontal lines!
Time taken (ns): 57424, Time taken(ms): 0
And here is results from this version:
Start of drawLines(): 82268401323649
Done drawing thick lines!
Time taken (ns): 113886, Time taken(ms): 0
Done drawing vertical lines!
Time taken (ns): 53896, Time taken(ms): 0
Done drawing horizontal lines!
Time taken (ns): 187993, Time taken(ms): 0
This version works even slower but has no glitching on my pc
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.GeneralPath;
import javax.swing.*;
@SuppressWarnings("serial")
public class SO_MCVE extends JPanel {
private DrawingCanvas _drawingCanvas = null;
private JButton repaintBtn;
public SO_MCVE() {
super(new BorderLayout());
_drawingCanvas = new DrawingCanvas();
_drawingCanvas.setPreferredSize(new Dimension(600, 600)); //CHANGED THIS LINE
JLabel repaintLabel = new JLabel(
"<html><div style=\"text-align: center;\">" +
"REPAINT</html>");
repaintLabel.setHorizontalAlignment(
SwingConstants.CENTER);
repaintBtn = new JButton();
repaintBtn.add(repaintLabel);
repaintBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_drawingCanvas.triggerRepaint();
}
});
add(_drawingCanvas, BorderLayout.CENTER);
add(repaintBtn, BorderLayout.PAGE_END);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("StackOverflow MCVE for drawLine");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SO_MCVE());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
@SuppressWarnings("serial")
class DrawingCanvas extends JComponent { //CHANGED THIS LINE TO JComponent
public static final Color lightGreen = new Color(0, 255, 0, 180);
public static final BasicStroke STROKE1PX = new BasicStroke(1.0f);
public static final BasicStroke STROKE3PX = new BasicStroke(3.0f);
private static final int LEFT = 50;
private static final int RIGHT = 550;
private static final int TOP = 50;
private static final int BOTTOM = 550;
private static final double WIDTH = 500.00d;
private static final double HEIGHT = 500.00d;
public DrawingCanvas() {
setBackground(Color.BLACK);
}
public void triggerRepaint() {
repaint();
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Dimension dim = getSize();
int w = (int) dim.getWidth();
int h = (int) dim.getHeight();
// Clears the rectangle that was previously drawn
g2.setPaint(Color.BLACK);
g2.fillRect(0, 0, w, h);
drawLines(g2, w, h);
}
/**
* Draw the lines marking the x-y limits
**/
private void drawLines(Graphics2D g2, int w, int h) {
long start = System.nanoTime();
System.out.println("Start of drawLines(): " + start);
// Thick lines
g2.setPaint(Color.GREEN);
g2.setStroke(STROKE3PX);
g2.drawLine(LEFT, 0, LEFT, h);
g2.drawLine(RIGHT, 0, RIGHT, h);
g2.drawLine(0, TOP, w, TOP);
g2.drawLine(0, BOTTOM, w, BOTTOM);
System.out.println("Done drawing thick lines!");
long end = System.nanoTime();
System.out.println("Time taken (ns): " +
(end - start) + ", Time taken(ms): " +
((end - start) / 1000 / 1000));
start = end;
//CHANGED THIS LINE
path = calculatePath(LEFT, TOP, RIGHT, BOTTOM);
System.out.println("Done drawing vertical lines!");
end = System.nanoTime();
System.out.println("Time taken (ns): " +
(end - start) + ", Time taken(ms): " +
((end - start) / 1000 / 1000));
start = end;
// Thin horizontal lines
g2.setPaint(lightGreen);
g2.setStroke(STROKE1PX);
g2.draw(path); //CHANGED THIS LINE
System.out.println("Done drawing horizontal lines!");
end = System.nanoTime();
System.out.println("Time taken (ns): " +
(end - start) + ", Time taken(ms): " +
((end - start) / 1000 / 1000));
System.out.println();
}
private GeneralPath path = new GeneralPath();
public GeneralPath calculatePath(int LEFT, int TOP, int RIGHT, int BOTTOM) {
path.reset();
// Thin vertical lines
for (int i = LEFT, wInc = ((int) WIDTH) / 50; i <= RIGHT; i += wInc) {
path.moveTo(i, TOP);
path.lineTo(i, BOTTOM);
}
// Thin horizontal lines
for (int i = TOP, hInc = ((int) HEIGHT) / 50; i <= BOTTOM; i += hInc) {
path.moveTo(LEFT, i);
path.lineTo(RIGHT, i);
}
return path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment