Jump to content

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.*Color;
import javaxjava.swingawt.*Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
 
public class Bresenham extends JFrame {
 
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(Bresenham::run) {;
}
@Override
 
private publicstatic void run() {
JFrame f = new BresenhamJFrame();
f.setDefaultCloseOperation(JFrameWindowConstants.EXIT_ON_CLOSEDISPOSE_ON_CLOSE);
f.setVisible(true);
f.setTitle("Bresenham");
f.add(new BresenhamPanel(), BorderLayout.CENTER);
 
f.setTitle("Bresenham");
f.getContentPane().add(new BresenhamPanel(), BorderLayout.CENTER);
f.setResizable(false);
f.pack();
 
f.setLocationRelativeTo(null);
}f.setLocationRelativeTo(null);
}f.setVisible(true);
}
}
 
class BresenhamPanel extends JPanel {
final int centerX, centerY;
 
private final int pixelSize = 10;
public BresenhamPanel() {
 
int w = 600;
public BresenhamPanel() {
int h = 500;
centerXsetPreferredSize(new =Dimension(600, w / 2500));
centerY = h / 2setBackground(Color.WHITE);
setPreferredSize(new Dimension(w, h));
setBackground(Color.white);
}
 
Line 1,837 ⟶ 1,839:
super.paintComponent(g);
 
drawLine(g,int 0,w 0,= 8,(getWidth() 19- 1); // NNEpixelSize;
drawLine(g,int 0,h 0,= 19,(getHeight() 8- 1); // ENEpixelSize;
drawLine(g,int 0,maxX 0,= 19,(w -8 1); // ESE2;
drawLine(g,int 0,maxY 0,= 8,(h -19 1); // SSE2;
drawLine(g,int 0,x1 0,= -8maxX, x2 = maxX * -19);2 / 3, x3 = maxX * 2 / SSW3, x4 = maxX;
drawLine(g,int 0,y1 0,= -19maxY, y2 = maxY * -8);2 / 3, y3 = maxY * 2 / WSW3, y4 = maxY;
 
drawLine(g, 0, 0, -19, 8); // WNW
drawLine(g, 0, 0, -8x3, 19y1); // NNWNNE
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, -19x1, 8y2); // WNW
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 wmaxX = 600(w - 1) / 2;
int hmaxY = 500(h - 1) / 2;
 
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(centerX + (x * 10)left, centerY + (-y * 10)top, 10pixelSize, 10pixelSize);
}
 
private void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
// delta of exact value and rounded value of the dependantdependent variable
int d = 0;
 
int dy = Math.abs(y2 - y1);
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
 
int dy2dx2 = (dy2 <<* 1)dx; // slope scaling factors to avoid floating
int dx2dy2 = (dx2 <<* 1)dy; // avoid floating point
 
int ix = x1 < x2 ? 1 : -1; // increment direction
int iy = y1 < y2 ? 1 : -1;
 
ifint (dyx <= dx) {x1;
int y = for (y1;;) {
 
plot(g, x1, y1);
if (x1dx =>= x2dy) {
@Overridewhile (true) {
f.setVisibleplot(trueg, x, y);
f.setResizableif (falsex == x2);
break;
x1x += ix;
d += dy2;
if (d > dx) {
y1y += iy;
d -= dx2;
}
}
} else {
forwhile (;;true) {
plot(g, x1x, y1y);
if (y1y == y2)
break;
y1y += iy;
d += dx2;
if (d > dy) {
x1x += ix;
d -= dy2;
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.