You are on page 1of 4

import java.math.*; import java.awt.*; import java.awt.event.*; import javax.swing.

JFrame; public class BresenhamTemplate extends JFrame { private static final long serialVersionUID = 7716286508117538157L; public static void main(String[] args) { new BresenhamTemplate(); } BresenhamTemplate() { // initialize the window super("Bresenham"); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); setSize(900, 720); getContentPane().add("Center", new CanvasBresenhamTemplate()); setVisible(true); } } class CanvasBresenhamTemplate extends Canvas { private static final long serialVersionUID = 4012295323526936729L; int centerX, centerY, pixelSize; // initialize the graphics void initgr() { pixelSize = 10; centerX = pixelSize * ((getWidth() / pixelSize) / 2); centerY = pixelSize * ((getHeight() / pixelSize) / 2); } void plotAll(Graphics g, double x1, double y1) { int x=(int)x1; int y=(int)y1; putPixel(g,x,y); putPixel(g,-x,y); putPixel(g,x,-y); putPixel(g,-x,-y); putPixel(g,y,x); putPixel(g,-y,x); putPixel(g,y,-x); putPixel(g,-y,-x); }

// draw one pixel with center (x, y) void putPixel(Graphics g, int x, int y) { g.setColor(Color.GREEN); g.fillRect((x * pixelSize) - (pixelSize / 2), (y * pixelSize) - (pixelSize / 2), pixelSize, pixelSize); } double sqrt(double x) { return Math.sqrt(x); } void drawBresenhamCircle(Graphics g, int radius) { // F Funktion double F = -radius + 0.25; double x = 0; double y = radius; plotAll(g, x, y); while (x < y) { F += 2 * x + 1; x++; // Bedingung fr y if (F >= 0) { F -= 2 * y - 1; y--; } plotAll(g, x, y); } // the circle to approximate g.setColor(Color.black); g.drawOval(-(radius * pixelSize), -(radius * pixelSize), 2 * rad ius * pixelSize, 2 * radius * pixelSize); // one pixel of the circle to be drawn putPixel(g, 0, radius); } void drawBresenhamLine(Graphics g, int x1, int y1, int x2, int y2) { // Delta y/Dx double m = ((double)y2 - (double)y1) / ((double)x2 - (double)x1); System.out.println(Double.toString(m)); double F = 0; double x = x1; double y = y1; putPixel(g, (int)x, (int)y); while (x < x2) { x++; F += m; // Bedingung fuer y if (F > 0.5) { y++; F--; } putPixel(g, (int)x, (int)y); } // the line to approximate

g.setColor(Color.black); g.drawLine(x1 * pixelSize, y1 * pixelSize, x2 * pixelSize, y2 * pixelSize); } void putSome(Graphics g, double x1, double y1) { int x = (int)x1; int y = (int)y1; putPixel(g, putPixel(g, putPixel(g, putPixel(g, } void drawBresenhamEllipse(Graphics g, int a,int b) { int x = 0; int y = b; // F Funktion double F=a * a * (b - 0.25); putSome(g, x, y); while (y > 0) { // Bedingung fuer x if(F >= 0) { x++; F = F + (b * b) * (1 - 2 * x); } // Bedingung fuer y if(F < 0) { y--; F = F - a* a * (1 - 2 * y); } putSome(g,x,y); } // the ellipse to approximate g.setColor(Color.black); g.drawOval(-(a * pixelSize), -(b * pixelSize), 2 * a * pixelSize, 2 * b* pixelSize); } // override the canvas paint method public void paint(Graphics g) { initgr(); paintCoordinateSystem(g); drawBresenhamLine(g, 0, 0, 5, 3); drawBresenhamCircle(g, 6); drawBresenhamEllipse(g, 15, 10); } private void paintCoordinateSystem(Graphics g) { g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(), getHeight()); x, y); -x, y); x, -y); -x, -y);

// coordinate grid g.setColor(Color.BLACK); for (int i = 0; i <= getWidth(); i += pixelSize) { g.drawLine(i, 0, i, getHeight()); g.drawLine(0, i, getWidth(), i); } g.setColor(Color.RED); g.drawLine(centerX, 0, centerX, getHeight()); g.drawLine(0, centerY, getWidth(), centerY); g.drawLine(centerX - 5, 10, centerX, 0); g.drawLine(centerX + 5, 10, centerX, 0); g.drawLine(getWidth() - 10, centerY - 5, getWidth(), centerY); g.drawLine(getWidth() - 10, centerY + 5, getWidth(), centerY); g.drawString("x", getWidth() - 20, centerY - 10); g.drawString("y", centerX + 10, 20); for (int i = -170; i <= 0; i += 10) { g.drawLine(centerX - 4, centerY + i, centerX + 4, center Y + i); } for (int i = -240; i <= 220; i += 10) { g.drawLine(centerX + i, centerY - 4, centerX + i, center Y + 4); } for (int i = -150; i <= 150; i += 50) { g.drawLine(centerX - 10, centerY + i, centerX + 10, centerY + i); } for (int i = -200; i <= 200; i += 50) { g.drawLine(centerX + i, centerY - 10, centerX + i, centerY + 10); } g.drawString("5", centerX - 25, centerY - 45); g.drawString("5", centerX + 45, centerY - 15); // translate origin and orientation g.translate(centerX, centerY); ((Graphics2D) g).scale(1.0, -1.0); // center g.fillOval(-pixelSize / 2, -pixelSize / 2, pixelSize, pixelSize); } }

You might also like