Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions lab-03/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>lab-03</artifactId>
<version>1.0-SNAPSHOT</version>
<name>lab-03</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.9.2</junit.version> </properties>

<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>22-ea+16</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>21-ea+24</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>21-ea+24</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency> </dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>com.example.lab03/by.fact0rial.paint.HelloApplication</mainClass>
<launcher>app</launcher>
<jlinkZipName>app</jlinkZipName>
<jlinkImageName>app</jlinkImageName>
<noManPages>true</noManPages>
<stripDebug>true</stripDebug>
<noHeaderFiles>true</noHeaderFiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
24 changes: 24 additions & 0 deletions lab-03/src/main/java/by/fact0rial/paint/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package by.fact0rial.paint;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 1800, 1000);
stage.setTitle("Paint");
stage.setMaximized(true);
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch();
}
}
5 changes: 5 additions & 0 deletions lab-03/src/main/java/by/fact0rial/paint/Mode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package by.fact0rial.paint;

public enum Mode {
Line, Circle, Rectangle, Draw, FilledCircle, FilledRectangle
}
150 changes: 150 additions & 0 deletions lab-03/src/main/java/by/fact0rial/paint/MyController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package by.fact0rial.paint;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.awt.image.BufferedImage;
import javafx.embed.swing.SwingFXUtils;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

public class MyController {
@FXML
private Canvas canvas;
@FXML
private ColorPicker lineColor;
@FXML
private ColorPicker fillColor;
private GraphicsContext g = null;
@FXML
private Slider thickness;
Mode m = Mode.Draw;
double startX = 0;
double startY = 0;
public void initialize() {
g = canvas.getGraphicsContext2D();
lineColor.setValue(Color.BLACK);
fillColor.setValue(Color.AQUA);
canvas.setOnMousePressed(this::mousePress);
canvas.setOnMouseDragged(this::mouseDrag);
canvas.setOnMouseReleased(this::mouseDragExited);
}

public void onDrawButtonPress() {
m = Mode.Draw;
}
public void onCircleButtonPress() {
m = Mode.Circle;
}
public void onRectangleButtonPress() {
m = Mode.Rectangle;
}
public void onLineButtonPress() {
m = Mode.Line;
}
private void mousePress(MouseEvent e) {
g.setLineWidth(thickness.getValue());
g.setStroke(lineColor.getValue());
g.setFill(fillColor.getValue());
switch (m) {
case Draw -> {
g.beginPath();
g.moveTo(e.getX(), e.getY());
g.stroke();
}
case Circle, Rectangle, Line, FilledRectangle, FilledCircle -> {
startX = e.getX();
startY = e.getY();
}
}
}
private void mouseDrag(MouseEvent e) {
switch (m) {
case Draw -> {
g.lineTo(e.getX(), e.getY());
g.stroke();
}
}
}
private void mouseDragExited(MouseEvent e) {
double x1 = Math.min(startX, e.getX());
double x2 = Math.abs(startX - e.getX());
double y1 = Math.min(startY, e.getY());
double y2 = Math.abs(startY - e.getY());
switch (m) {
case Circle -> {
g.strokeOval(x1, y1, x2, y2);
}
case Rectangle -> {
g.strokeRect(x1, y1, x2, y2);
}
case FilledCircle -> {
g.fillOval(x1,y1,x2,y2);
g.strokeOval(x1, y1, x2, y2);
}
case FilledRectangle -> {
g.fillRect(x1, y1, x2, y2);
g.strokeRect(x1, y1, x2, y2);
}
case Line -> {
g.strokeLine(startX, startY, e.getX(), e.getY());
}
}
}

public void onFilledCircleButtonPress() {
m = Mode.FilledCircle;
}

public void onFilledRectangleButtonPress() {
m = Mode.FilledRectangle;
}

public void onSaveButtonPress(ActionEvent actionEvent) {
FileChooser chooser = new FileChooser();
Stage stage = (Stage)((Button)actionEvent.getSource()).getScene().getWindow();
File file = chooser.showSaveDialog(stage);
WritableImage im = new WritableImage((int)canvas.getWidth(), (int)canvas.getHeight());
canvas.snapshot(null, im);
BufferedImage buff = SwingFXUtils.fromFXImage(im, null);
try {
ImageIO.write(buff, "png", file);
} catch (IOException e) {
System.out.println("Ошибка при сохранении рисунка: " + e.getMessage());
}
}

public void onOpenButtonPress(ActionEvent actionEvent) {
FileChooser chooser = new FileChooser();
chooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("PNG Files", "*.png"));
Stage stage = (Stage)((Button)actionEvent.getSource()).getScene().getWindow();
File file = chooser.showOpenDialog(stage);
BufferedImage buff = null;
try {
buff = ImageIO.read(file);
} catch (IOException e) {
System.out.println("Ошибка при открытии рисунка: " + e.getMessage());
}
Image im = SwingFXUtils.toFXImage(buff, null);
g.fillRect(0,0, canvas.getWidth(), canvas.getHeight());
g.drawImage(im, 0, 0);
}
}
10 changes: 10 additions & 0 deletions lab-03/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module com.example.lab03 {
requires javafx.controls;
requires javafx.fxml;
requires java.desktop;
requires javafx.swing;


opens by.fact0rial.paint to javafx.fxml;
exports by.fact0rial.paint;
}
28 changes: 28 additions & 0 deletions lab-03/src/main/resources/by/fact0rial/paint/view.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.ColorPicker?>
<?import javafx.scene.control.Slider?>
<VBox alignment="TOP_CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml"
fx:controller="by.fact0rial.paint.MyController">
<HBox alignment="BASELINE_LEFT" maxHeight="10">
<Button text="save" onAction="#onSaveButtonPress"/>
<Button text="open" onAction="#onOpenButtonPress"/>
<Button text="draw" onAction="#onDrawButtonPress"/>
<Button text="circle" onAction="#onCircleButtonPress"/>
<Button text="rectangle" onAction="#onRectangleButtonPress"/>
<Button text="line" onAction="#onLineButtonPress"/>
<Button text="filled circle" onAction="#onFilledCircleButtonPress"/>
<Button text="filled rectangle" onAction="#onFilledRectangleButtonPress"/>
<ColorPicker fx:id="lineColor"/>
<ColorPicker fx:id="fillColor"/>
<Slider fx:id="thickness" min="1" max="10"/>
</HBox>
<Canvas fx:id="canvas" height="900" width="1600"/>
</VBox>