HOWTO · Java
JavaFX setFill() 메서드
이 튜토리얼은 JavaFX에서 setFill() 메소드를 사용하는 방법을 보여줍니다.
이 페이지의 내용
setFill() 메소드는 JavaFX에서 모양 및 기타 요소의 색상을 채우는 데 사용됩니다. 이 튜토리얼은 JavaFX에서 setFill() 메소드를 사용하는 방법을 보여줍니다.
JavaFX setFill() 메서드
setFill() 메소드는 JavaFX의 모양에 균일한 이미지 패턴과 그라디언트 패턴을 채울 수 있습니다. setFill() 메소드를 사용하려면 JavaFX.scene.paint 패키지가 필요합니다.
setFill()은 Shape, Text 등과 같은 클래스에 색상을 채우는 데 사용할 수 있습니다.
통사론:
//Setting color to the text
Color color = new Color.Red
text.setFill(color);
위의 구문은 페인트 패키지의 Color 클래스를 사용하여 색상을 지정하고 setFill() 메서드를 사용하여 텍스트를 채웁니다. 다음은 setFill 방법을 사용하여 장면에 색상을 채우는 단계입니다.
- Application 클래스를 확장하고
start()메서드를 구현하는 클래스를 만듭니다. Group클래스를 인스턴스화하여 그룹을 생성합니다.Scene클래스를 인스턴스화하고group을 전달하여 장면을 만듭니다.setFill방법을 사용하여 장면에 색상을 채웁니다.- 모양, 원, 직사각형 등을 만들고
그룹에 모양을 추가합니다. scene을 무대에 전달하고Show방법으로 무대를 표시합니다.main방법으로 애플리케이션을 시작합니다.
위의 단계를 기반으로 한 예를 들어 보겠습니다.
예제 코드:
package delftstack;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
public class JavaFX_SetFill extends Application {
@Override
public void start(Stage DemoStage) {
Group DemoGroup = new Group();
Scene DemoScene = new Scene(DemoGroup, 200, 150);
DemoScene.setFill(Color.LIGHTBLUE);
Circle DemoCircle = new Circle(100, 100, 80, Color.RED);
DemoGroup.getChildren().add(DemoCircle);
DemoStage.setScene(DemoScene);
DemoStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
위의 코드는 원 모양의 장면을 만듭니다. setFill 메서드를 사용하여 장면에 색상을 채웁니다.
출력:
setFill() 메서드를 사용하여 모양과 텍스트에 색상을 채우도록 합시다.
예제 코드:
package delftstack;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class JavaFX_SetFill extends Application {
@Override
public void start(Stage DemoStage) {
// Draw a Square
Rectangle Square = new Rectangle();
// Set the properties of the Square
Square.setX(200.0f);
Square.setY(200.0f);
Square.setWidth(300.0f);
Square.setHeight(300.0f);
// Set color to the Square
Square.setFill(Color.LIGHTBLUE);
// Set the stroke width
Square.setStrokeWidth(3);
// Set color to the stroke
Square.setStroke(Color.LIGHTGREEN);
// Draw a text
Text DemoText = new Text("This is a colored Square");
// Set the font of the text
DemoText.setFont(Font.font("Edwardian Script ITC", 60));
// Set the position of the text
DemoText.setX(155);
DemoText.setY(50);
// Set color to the text
DemoText.setFill(Color.BEIGE);
DemoText.setStrokeWidth(2);
DemoText.setStroke(Color.LIGHTBLUE);
// Create a Group object
Group Group_Root = new Group(Square, DemoText);
// Create a scene object
Scene DemoScene = new Scene(Group_Root, 600, 300);
// Set title to the Stage
DemoStage.setTitle("SetFill Example");
// Add scene to the stage
DemoStage.setScene(DemoScene);
// Display the contents of the stage
DemoStage.show();
}
public static void main(String args[]) {
launch(args);
}
}
위의 코드는 정사각형과 텍스트를 만든 다음 setfill 메서드를 사용하여 정사각형을 색상으로 채웁니다. 또한 테두리에 setStroke 메서드를 사용합니다.
출력:
setFill 메서드는 모양이나 텍스트에 대한 이미지 그라디언트를 채울 수도 있습니다.
예제 코드:
package delftstack;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class JavaFX_SetFill extends Application {
@Override
public void start(Stage DemoStage) throws FileNotFoundException {
// Draw a Square
Rectangle Square = new Rectangle();
// Set the properties of the Square
Square.setX(200.0f);
Square.setY(200.0f);
Square.setWidth(300.0f);
Square.setHeight(300.0f);
// Draw a text
Text DemoText = new Text("This is a Gradient Square");
// Set the font of the text
DemoText.setFont(Font.font("Edwardian Script ITC", 60));
// Set the position of the text
DemoText.setX(155);
DemoText.setY(50);
// Set the image pattern
Image DemoImage = new Image(new FileInputStream("Delftstack.png"));
ImagePattern Image_Gradient = new ImagePattern(DemoImage, 80, 80, 160, 160, false);
// Set the linear gradient to the Square
Square.setFill(Image_Gradient);
// Create a Group object
Group Group_Root = new Group(Square, DemoText);
// Create a scene object
Scene DemoScene = new Scene(Group_Root, 600, 300);
// Set title to the Stage
DemoStage.setTitle("SetFill Example");
// Add scene to the stage
DemoStage.setScene(DemoScene);
// Display the contents of the stage
DemoStage.show();
}
public static void main(String args[]) {
launch(args);
}
}
위의 코드는 이미지 그라디언트를 정사각형 모양으로 채웁니다.
출력: