Question
Rewrite the java netbeans program below into JavaFX netbeans. import java.awt.*; import javax.swing.*; public class L4_bresenham { public static void main(String[] args) { System.out.print(Enter x1,
Rewrite the java netbeans program below into JavaFX netbeans.
import java.awt.*; import javax.swing.*; public class L4_bresenham {
public static void main(String[] args) { System.out.print("Enter x1, y1, x2, y2, respectively(from zero to 600): "); java.util.Scanner input = new java.util.Scanner(System.in); Bresenham line = new Bresenham(input.nextInt(), input.nextInt(), input.nextInt(), input.nextInt()); JFrame frame = new JFrame("Bresenham Line"); frame.setSize(800, 800); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); line.setPreferredSize(new Dimension(600,600)); frame.add(line); frame.pack(); frame.setVisible(true); }//end main }//end class
import java.awt.Graphics; import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; import javax.swing.JApplet;
public class Bresenham extends JApplet{ BufferedImage image = new BufferedImage(600, 600, BufferedImage.TYPE_INT_ARGB); WritableRaster raster = image.getRaster(); int x1, x2, y1, y2; Bresenham(int x1, int y1, int x2, int y2){ this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; init(); } public void init() { setSize(600, 600); repaint(); } float updateP(float p, float dy, float dx){ return p < 0? p + 2*dy : p + 2*(dy-dx); } public void paint(Graphics g) {float dx, dy; int color[] = {255, 0, 0, 255}; dx = Math.abs( x2 - x1); dy = Math.abs(y2 - y1); float slope = dy / dx; boolean swapped = false; // determine if a swap is needed if(slope > 1){ float temp = x1; x1 = y1; y1 = (int)temp; temp = x2; x2 = y2; y2 = (int)temp; temp = dx; dx = dy; dy = temp; swapped = true; } float p = 2*dy - dx; raster.setPixel(x1, y1, color); for(int k = 1; k <= dx; k++){ if(swapped) System.out.println("x: " + y1 + " y: " + x1); else System.out.println("x: " + x1 + " y: " + y1); if (p<0){ if(x1 < x2) x1++; else x1--; } else{ if(y1 < y2) y1++; else y1--; if(x1 < x2) x1++; else x1--; } p = updateP(p, dy, dx); if(slope>1) raster.setPixel(y1, x1, color); else raster.setPixel(x1, y1, color); } if(swapped) System.out.println("x: " + y1 + " y: " + x1); else System.out.println("x: " + x1 + " y: " + y1); g.drawImage(image, 0, 0, null); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started