Write a Java GUI application provides a canvas for the user

Write a Java GUI application provides a canvas for the user to draw basic shapes (such as line, circle, and rectangle), pick colors to fill the shapes. User can create a drawing on a canvas, save the result, and load saved canvasses. Hint: save a canvas (i.e., shapes on the canvas) in an object file.

Solution

package createcanvas; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; import javafx.scene.shape.ArcType; import javafx.stage.Stage; public class Testcanvas extends Application { public static void main(String[] args) { launch(args); } public void start(Stage primaryStage) { primaryStage.setTitle(\"let us start the canvas test\"); Group createroot = new Group(); Canvas createcanvas = new Canvas(300, 250); GraphicsContext gcone = createcanvas.getGraphicsContext2D(); letsdrawShapes(gcone); createroot.getChildren().add(createcanvas); primaryStage.setScene(new Scene(createroot)); primaryStage.show(); } private void letsdrawShapes(GraphicsContext gcone) { gcone.setFill(Color.RED); gcone.setStroke(Color.WHITE); gcone.setLineWidth(4); gcone.strokeLine(50, 20, 20, 50); gcone.fillOval(20, 60, 40, 40); gcone.strokeOval(60, 60, 30, 30); gcone.fillRoundRect(120, 60, 20, 20, 10, 10); gcone.strokeRoundRect(160, 60, 30, 30, 10, 10); gcone.fillArc(10, 120, 30, 30, 45, 250, ArcType.OPEN); gcone.fillArc(70, 120, 30, 30, 45, 250, ArcType.CHORD); gcone.fillArc(120, 120, 30, 30, 45, 250, ArcType.ROUND); gcone.strokeArc(20, 160, 30, 30, 45, 250, ArcType.OPEN); gcone.strokeArc(70, 160, 30, 30, 45, 250, ArcType.CHORD); gcone.strokeArc(120, 160, 30, 30, 45, 250, ArcType.ROUND); gcone.fillPolygon(new double[]{20, 40, 20, 40}, new double[]{210, 210, 250, 250}, 4); gcone.strokePolygon(new double[]{70, 90, 70, 90}, new double[]{210, 210, 250, 250}, 4); gcone.strokePolyline(new double[]{110, 140, 110, 140}, new double[]{210, 210, 240, 240}, 4); } }
Write a Java GUI application provides a canvas for the user to draw basic shapes (such as line, circle, and rectangle), pick colors to fill the shapes. User can

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site