Question: TASK #1 Implement the Line-drawing When you first run the application, the graph will not be plotted, although the prompts will appear and be handled.
TASK #1 Implement the Line-drawing
When you first run the application, the graph will not be plotted, although the prompts will appear and be handled. In the method drawGraph(double, double, GraphicsContext), add a call to gc.strokeLine(x0Draw, y1Draw, x1Draw, y0Draw) to plot each line segment of the graph.
Experiment with all three functions provided, varying left and right extents. Notice the gray horizontal line that is plotted at y=0 for reference.
TASK #1 Implement the Line-drawing
When you first run the application, the graph will not be plotted, although the prompts will appear and be handled. In the method drawGraph(double, double, GraphicsContext), add a call to gc.strokeLine(x0Draw, y1Draw, x1Draw, y0Draw) to plot each line segment of the graph.
Experiment with all three functions provided, varying left and right extents. Notice the gray horizontal line that is plotted at y=0 for reference.
I don't understand what the question is asking me to do. I tried doing it as followed and it didn't work. Can someone please help me?
GraphDriverFX.java
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class GraphDriverFX extends Application { public final double CANVAS_WIDTH = 400; public final double CANVAS_HEIGHT = 300; public final double WINDOW_HEIGHT = 400; public static void main(String[] args){ launch(args); } /** * start is required by the class Application and is called by launch * It initializes MainPaneFX, which returns the main panel */ public void start(Stage stage) throws Exception { MainPaneFX mainPane = new MainPaneFX(CANVAS_WIDTH, CANVAS_HEIGHT); BorderPane root = mainPane.getMainPane(); Scene scene = new Scene(root, CANVAS_WIDTH, WINDOW_HEIGHT); stage.setScene(scene); stage.setTitle("Function Grapher"); stage.show(); } }
---------------------------------------------------
MainPaneFX.java
import javax.swing.JOptionPane; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Group; import javafx.scene.Parent; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; /** * This panel is the basic pane, inside which other panes are placed. * @author ralexander */ public class MainPaneFX { private BorderPane mainPane; private HBox buttonPanel; private GraphPanelFX graphPanel; private Canvas graphCanvas; private Button graphFunctionButton, exitButton; //The manager is the way the GUI communicates with the worker code private GraphManager graphManager; /** * The MainPanel constructor sets up the GUI with two buttons and a display area. */ MainPaneFX(double CANVAS_WIDTH, double CANVAS_HEIGHT) { mainPane = new BorderPane(); //create the dataManager instance graphManager = new GraphManager(); //create the exitButton exitButton = new Button("Exit"); //exitButton.setToolTipText("Exit the program"); exitButton.setOnAction(event -> System.exit(0) ); //create the button to start graphing graphFunctionButton = new Button("Graph a Function"); //graphFunctionButton.setToolTipText("Select a function and graph it"); /* * When the button pushed was the graph function button, user is prompted to select one of the functions, * and is asked for the left and right limits (extents) to plot the function. */ graphFunctionButton.setOnAction(event -> { graphCanvas.setVisible(false); int choice = 0; double left=0, right=0; choice = askForFunction("Select one of the following functions to graph (by number): "+graphManager.toString()); if(choice != 0) { graphManager.setFunctionChoice(choice); try { left = askForExtent("Enter the left extent of the function domain"); right = askForExtent("Enter the right extent of the function domain"); graphManager.setExtents(left, right, graphCanvas.getWidth()); graphPanel.drawGraph(); } catch (NullPointerException e) { JOptionPane.showMessageDialog(null, "No entry: exiting"); } graphCanvas.setVisible(true); } }); //create the buttonPanel buttonPanel = new HBox(); //buttonPanel.setPreferredSize(new Dimension(600,50)); buttonPanel.setVisible(true); buttonPanel.setAlignment(Pos.CENTER); HBox.setMargin(exitButton, new Insets(10,10,10,10)); HBox.setMargin(graphFunctionButton, new Insets(10,10,10,10)); buttonPanel.getChildren().addAll(exitButton, graphFunctionButton); //buttonPanel.add(graphFunctionButton); //add the panel to the bottom section of the main panel mainPane.setBottom(buttonPanel); //panel to hold the graph graphPanel = new GraphPanelFX(graphManager, CANVAS_WIDTH, CANVAS_HEIGHT); graphCanvas = graphPanel.getGraphCanvas(graphPanel); graphCanvas.setVisible(true); mainPane.setCenter(graphCanvas); } public GraphManager getGraphManager() { return graphManager; } private int askForFunction(String str) { boolean error=false; int returnVal=0; do { try { returnVal = Integer.parseInt(JOptionPane.showInputDialog (null, str)); if (returnVal<1 || returnval>5) { error = true; JOptionPane.showMessageDialog(null, "Choice of function must be an integer between 1 and 5"); } else error = false; } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Input Error: "+e); error = true; } } while(error); return returnVal; } private double askForExtent(String str) throws NullPointerException { boolean error=false; double returnVal=0; do { try { returnVal = Double.parseDouble(JOptionPane.showInputDialog(null, str)); error = false; } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Input Error - "+e); error = true; } } while(error); return returnVal; } public BorderPane getMainPane() { // TODO Auto-generated method stub return mainPane; } } --------------------------------------------------------
GraphPanelFX
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Paint; /** * GraphPanelFX creates the Canvas and draws the graph * @author ralexander * */ public class GraphPanelFX { double gridWidth; double gridHeight; double xLeft, xRight, yTop, yBottom; GraphManager graphMgr; Canvas graphCanvas; GraphicsContext gc; GraphPanelFX(GraphManager graphManager, double CANVAS_WIDTH, double CANVAS_HEIGHT) { graphMgr = graphManager; graphCanvas = new Canvas(CANVAS_WIDTH, CANVAS_HEIGHT); gc = graphCanvas.getGraphicsContext2D(); } public Canvas getGraphCanvas(GraphPanelFX graphPanel2) { return graphCanvas; } /** * drawGraph is called when the "Graph a Function" button is selected */ public void drawGraph() { gridWidth = gc.getCanvas().getWidth(); gridHeight = gc.getCanvas().getHeight(); gc.clearRect(0,0,gridWidth,gridHeight); System.out.println("in paintComponent(); width="+gridWidth+"; height="+gridHeight); drawGraph(gridWidth, gridHeight-10, gc); } /** * Draws line segments from left extent to right extent, pixel by pixel, transforming points * to the coordinate system of the panel. * @param gridWidth2 the width of the panel in pixels * @param gridHeight the height of the panel in pixels * @param g2 the Graphics2D instance generated by Java library classes and sent as a argument of paintComponent */ public void drawGraph(double gridWidth2, double gridHeight, GraphicsContext gc) { double x0=xLeft, y0, x1=0; double x1Draw, x0Draw, y1Draw, y0Draw; int functionChoice = graphMgr.getFunctionChoice(); Function fn = graphMgr.getFunction(functionChoice); //check to make sure a function choice has been made before drawing if(functionChoice>0) { xLeft = graphMgr.getLeftExtent(); xRight = graphMgr.getRightExtent(); graphMgr.setExtents(xLeft, xRight, gridWidth2); yTop = graphMgr.getTopExtent(); yBottom = graphMgr.getBottomExtent(); //draw a gray horizontal line at y=0 gc.setStroke(Paint.valueOf("Gray")); y1Draw = fn.originToPlot(gridHeight, yBottom, yTop); gc.strokeLine(0,y1Draw,gridWidth2,y1Draw); //set the graphing color and width gc.setStroke(Paint.valueOf("BLUE")); gc.setLineWidth(2); x0=xLeft; y0 = graphMgr.getFnValue(functionChoice, x0); //loop pixel by pixel, drawing the function between each value of x for (int i=1; i
--------------------------------------
Function.Java
public abstract class Function { /** * Calculates the value f(x) of the function at x * @param x The x-value at which the function will be evaluated * @return a double, the value of the function at x */ public abstract double fnValue(double x); /** * Translates f(x) to the display coordinate system. Note that Java graphics * places (0,0) in the upper left, and (xmax, ymax) in the lower right of the * display. A buffer of 5 pixels is kept at top and bottom. * @param x the value at which f(x) will be evaluated * @param d the height in pixels of the display * @param minY the minimum f(x) to be displayed, over the extent of the x's * @param maxY the maximum f(x) to be displayed, over the extent of the x's * @return the value of f(x) translated to the display coordinate system */ public double fnValueToPlot(double x, double d, double minY, double maxY) { double y = fnValue(x); double yDraw = (d+5)-((y-minY)*(d/(maxY-minY))); return yDraw; } /** * Determines the display value where y=0 * @param height Height of the Canvas * @param minY th=0e minimum height of the function within the chosen extents * @param maxY the maximum height of the function within the chosen extents * @return the value of y to display corresponding to y */ public double originToPlot(double height, double minY, double maxY) { double yDraw = (height+5)-((0-minY)*(height/(maxY-minY))); return yDraw; } }
--------------------------------------------
Function1.java
public class Function1 extends Function { @Override public double fnValue(double x) { if (x==0.0) return Double.MAX_VALUE; else return 1/x; } public String toString() { return "1/x"; } }
--------------------------------------------
Function2.java
public class Function2 extends Function { @Override public double fnValue(double x) { return Math.sin(x); } public String toString() { return "sin(x)"; } }
-----------------------------------
Function3.java
public class Function3 extends Function {
@Override public double fnValue(double x) { return (8*x-Math.sqrt(x))/(Math.pow(x,3) - (7*Math.pow(x,2)) + 15*x - 9); } public String toString () { return "(8*x-sqrt(x))/x^3 - 7*x^2 + 15*x - 9"; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
