Created
September 8, 2021 09:32
-
-
Save Da9el00/fb602774156aaa7de538c55ea5e51668 to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Drawing Power and Exponential Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.beans.value.ChangeListener; | |
import javafx.beans.value.ObservableValue; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.RadioButton; | |
import javafx.scene.control.Slider; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.shape.Circle; | |
import javafx.scene.text.Text; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
@FXML | |
private Pane coordinateSystem; | |
@FXML | |
private Slider aSlider; | |
@FXML | |
private Slider bSlider; | |
@FXML | |
private Text functionText; | |
@FXML | |
private RadioButton powerFunction; | |
@FXML | |
private RadioButton exponentialFunction; | |
private int paneWidth = 300; | |
Function function; | |
@Override | |
public void initialize(URL url, ResourceBundle resourceBundle) { | |
setupSlider(aSlider); | |
setupSlider(bSlider); | |
} | |
private void setupSlider(Slider Slider) { | |
Slider.valueProperty().addListener(new ChangeListener<Number>() { | |
@Override | |
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) { | |
if(powerFunction.isSelected()){ | |
function = new PowerFunction(aSlider.getValue(),bSlider.getValue()); | |
} else if(exponentialFunction.isSelected()){ | |
function = new ExponentialFunction(aSlider.getValue(),bSlider.getValue()); | |
} | |
coordinateSystem.getChildren().clear(); | |
drawFunction(); | |
} | |
}); | |
} | |
public void drawFunction(){ | |
functionText.setText(function.toString()); | |
for (double i = 0; i < 200; i = i + 0.5) { | |
coordinateSystem.getChildren().add(drawPoint(i,function.getY(i))); | |
} | |
} | |
public Circle drawPoint(double x, double y){ | |
return new Circle(x,-(y-paneWidth),1); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
public class ExponentialFunction extends Function{ | |
public ExponentialFunction(double a, double b) { | |
super(a, b); | |
} | |
@Override | |
public double getY(double x) { | |
return getB() * Math.pow(getA(),x); | |
} | |
@Override | |
public String toString() { | |
return "f(x)=" + df.format(getB()) + "*" + df.format(getA()) + "^x"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import java.text.DecimalFormat; | |
public abstract class Function { | |
private double a; | |
private double b; | |
DecimalFormat df = new DecimalFormat("#.00"); | |
public Function(double a, double b) { | |
this.a = a; | |
this.b = b; | |
} | |
abstract public double getY(double x); | |
public double getA() { | |
return a; | |
} | |
public double getB() { | |
return b; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
primaryStage.setTitle("Hello World"); | |
primaryStage.setScene(new Scene(root)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
public class PowerFunction extends Function { | |
public PowerFunction(double a, double b) { | |
super(a, b); | |
} | |
@Override | |
public double getY(double x){ | |
return getB() * Math.pow(x,getA()); | |
} | |
@Override | |
public String toString() { | |
return "f(x)=" + df.format(getB()) + "*x^" + df.format(getA()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.RadioButton?> | |
<?import javafx.scene.control.Slider?> | |
<?import javafx.scene.control.ToggleGroup?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<?import javafx.scene.layout.Pane?> | |
<?import javafx.scene.text.Font?> | |
<?import javafx.scene.text.Text?> | |
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> | |
<children> | |
<Pane fx:id="coordinateSystem" layoutX="251.0" layoutY="50.0" prefHeight="300.0" prefWidth="300.0" /> | |
<Slider fx:id="aSlider" layoutX="31.0" layoutY="155.0" majorTickUnit="0.5" max="3.0" minorTickCount="1" showTickLabels="true" showTickMarks="true" value="1.1" /> | |
<Slider fx:id="bSlider" layoutX="31.0" layoutY="220.0" showTickLabels="true" showTickMarks="true" value="1.0" /> | |
<Text fx:id="functionText" layoutX="31.0" layoutY="297.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Pick a function"> | |
<font> | |
<Font size="20.0" /> | |
</font> | |
</Text> | |
<RadioButton fx:id="powerFunction" layoutX="38.0" layoutY="42.0" mnemonicParsing="false" text="f(x)=b*x^a"> | |
<toggleGroup> | |
<ToggleGroup fx:id="functions" /> | |
</toggleGroup> | |
</RadioButton> | |
<RadioButton fx:id="exponentialFunction" layoutX="38.0" layoutY="61.0" mnemonicParsing="false" text="f(x)=b*a^x" toggleGroup="$functions" /> | |
<Text layoutX="190.0" layoutY="27.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Power Function and Exponential Function" /> | |
</children> | |
</AnchorPane> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment