Anonymous user
Bitmap/Bresenham's line algorithm: Difference between revisions
→{{header|Java}}: resizable demonstration window, more idiomatic Java code, centered image on the screen
(reworded task) |
(→{{header|Java}}: resizable demonstration window, more idiomatic Java code, centered image on the screen) |
||
Line 1,799:
=={{header|Java}}==
<lang java>import java.awt.
import
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class Bresenham
public static void main(String[] args) {
SwingUtilities.invokeLater(
}
@Override▼
private
f.setVisible(true);▼
f.add(new BresenhamPanel(), BorderLayout.CENTER);▼
▲ f.setTitle("Bresenham");
f.setResizable(false);▼
}
}
class BresenhamPanel extends JPanel {
private final int pixelSize = 10;
public BresenhamPanel() {▼
int w = 600;▼
int h = 500;▼
}
Line 1,837 ⟶ 1,839:
super.paintComponent(g);
drawLine(g, 0, 0, -19, 8); // WNW▼
drawLine(g, 0, 0,
drawLine(g, 0, 0, x4, y2); // ENE
drawLine(g, 0, 0, x4, y3); // ESE
drawLine(g, 0, 0, x3, y4); // SSE
drawLine(g, 0, 0, x2, y4); // SSW
drawLine(g, 0, 0, x1, y3); // WSW
drawLine(g, 0, 0, x2, y1); // NNW
}
private void plot(Graphics g, int x, int y) {
int w = (getWidth() - 1) / pixelSize;
int h = (getHeight() - 1) / pixelSize;
int borderX = getWidth() - ((2 * maxX + 1) * pixelSize + 1);
int borderY = getHeight() - ((2 * maxY + 1) * pixelSize + 1);
int left = (x + maxX) * pixelSize + borderX / 2;
int top = (y + maxY) * pixelSize + borderY / 2;
g.setColor(Color.black);
g.drawOval(
}
private void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
// delta of exact value and rounded value of the
int d = 0;
int dy = Math.abs(y2 - y1);▼
int dx = Math.abs(x2 - x1);
▲ int dy = Math.abs(y2 - y1);
int
int
int ix = x1 < x2 ? 1 : -1; // increment direction
int iy = y1 < y2 ? 1 : -1;
int y =
break;
d += dy2;
if (d > dx) {
d -= dx2;
}
}
} else {
plot(g,
if (
break;
d += dx2;
if (d > dy) {
d -= dy2;
}
|