Question

I started this but need to convert it using JavaFX import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage;...

I started this but need to convert it using JavaFX

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class ArtProject {

public static void main(String[] args) throws IOException {
/*this will be the size of the rectangle background with
circles inside the image*/
int width = 400;
int height = 250;
int circleWidth = height;

//create an image object
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();

//this sets the background color to white
g2d.setColor(Color.white);
g2d.fillRect(0, 0, width, height);

//this will be the colors of three different circles
g2d.setColor(Color.pink);
g2d.fillOval(150, 0, circleWidth, height);
g2d.setColor(Color.darkGray);
g2d.fillOval(75, 0, circleWidth, height);
g2d.setColor(Color.gray);
g2d.fillOval(0, 0, circleWidth, height);


/*this will set the color, font and size of the text and place it at the
lower right corner*/
Font stringFont = new Font("Century Gothic", Font.PLAIN, 16);
g2d.setFont(stringFont);
g2d.setColor(Color.black);
g2d.drawString("text here", 275, 240);
g2d.dispose();

//this will create the file the image will be saved to.
File file = new File("javaimage.jpg");
ImageIO.write(bufferedImage, "jpg", file);


}
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Here is the code and related screenshots. All you need to do is extend the class called Application for Javafx. Next, you got to use canvas to create the images that's all. Please drop me a comment if you have any doubts, I'll be glad to help you. Make sure you go through the comments for better understanding fo the code.

O/P Screenshot:

Code

import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.WritableImage; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.stage.Stage; import javax.imageio.ImageIO; import java.awt.image.RenderedImage; import java.io.File; import java.io.IOException; public class ArtProject extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) throws IOException { /*this will be the size of the rectangle background with circles inside the image*/ int width = 400, height = 250, circleWidth = height; // Create the Canvas, set height & width Canvas canvas = new Canvas(width, height); canvas.setWidth(width); canvas.setHeight(height); // Here is the graphics for canvas GraphicsContext gc = canvas.getGraphicsContext2D(); // All your specifications gc.setFill(Color.WHITE); gc.fillRect(0, 0, width,height); //this will be the colors of three different circles gc.setFill(Color.PINK); gc.fillOval(150, 0, circleWidth, height); gc.setFill(Color.DARKGREY); gc.fillOval(75, 0, circleWidth, height); gc.setFill(Color.GRAY); gc.fillOval(0, 0, circleWidth, height); // font Font font = Font.font("Century Gothic", FontWeight.NORMAL, 25); gc.setFont(font); gc.setFill(Color.BLACK); gc.strokeText("text here", 275, 240); // Create the Pane Pane root = new Pane(); // Set the Style-properties of the Pane root.setStyle("-fx-padding: 10;" + "-fx-border-style: solid inside;" + "-fx-border-width: 2;" + "-fx-border-insets: 5;" + "-fx-border-radius: 5;" + "-fx-border-color: blue;"); // Add the Canvas to the Pane root.getChildren().add(canvas); // Create the Scene, add it to the stage Scene scene = new Scene(root); stage.setScene(scene); stage.setTitle("Creation of a Circles"); // writing it to a file WritableImage writableImage = new WritableImage(width, height); canvas.snapshot(null, writableImage); RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null); File file = new File("javaimage.jpg"); ImageIO.write(renderedImage, "jpg", file); // Simply display the canvas stage.show(); } }

Code screenshots

Add a comment
Know the answer?
Add Answer to:
I started this but need to convert it using JavaFX import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT