Skip to content
Snippets Groups Projects
Commit 959e4385 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

RemoteLoader

parent 2345a192
Branches master
No related merge requests found
Pipeline #127 failed with stages
in 0 seconds
......@@ -11,4 +11,6 @@
*.ear
*.iml
*.idea
\ No newline at end of file
*.idea
lab08.imgs
package koz01.java2.lab08;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import javafx.scene.image.Image;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class IndexReader {
private static final String URL = "http://kozusznik.cz/resources/java2/";
private static final String IMAGE_NAME = "lab08.imgs";
public Image readImage(int index) {
try (FileChannel fc = FileChannel.open(getFile()))
{
byte[] data = getData(fc, index);
return new Image(new ByteArrayInputStream(data));
}
catch (IOException exc) {
log.error("read", exc);
return null;
}
}
private Path getFile() throws IOException {
Path LocalFile = Files.createTempFile(null, null).getParent().resolve(
IMAGE_NAME);
if (!Files.exists(LocalFile)) {
try (ReadableByteChannel in = Channels.newChannel(new URL(
URL + IMAGE_NAME).openStream());
FileChannel out = FileChannel.open(LocalFile,
StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE))
{
ByteBuffer bb = ByteBuffer.allocate(1024 * 1024);
while (0 <= in.read(bb)) {
bb.flip();
out.write(bb);
bb.clear();
}
}
}
return LocalFile;
}
private byte[] getData(FileChannel fc, int index) throws IOException {
return new byte[0];
}
}
package koz01.java2.lab08;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/**
* JavaFX App
*/
public class App extends Application {
public class LocalLoader extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("LocalLoader.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
launch();
}
public static void main(String[] args) {
launch();
}
}
\ No newline at end of file
}
package koz01.java2.lab08;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
public class LocalLoaderControler {
@FXML
private ImageView imageView;
@FXML
private TextField indexTextField;
public void readButton() {
imageView.setImage(new IndexReader().readImage(Integer.parseInt(
indexTextField.getText())));
}
}
package koz01.java2.lab08;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* JavaFX App
*/
public class RemoteLoader extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource(
"RemoteLoader.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch();
}
}
package koz01.java2.lab08;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class RemoteLoaderControler {
@FXML
private ImageView imageView;
@FXML
private TextField indexTextField;
@FXML
private Button load;
@FXML
private TextField adress;
@FXML
private TextField port;
private SocketChannel socketChannel;
public void readButton() {
try {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(Integer.parseInt(indexTextField.getText()));
bb.flip();
socketChannel.write(bb);
bb.clear();
socketChannel.read(bb);
bb.flip();
int size = bb.getInt();
bb = ByteBuffer.allocate(size);
while (bb.hasRemaining()) {
socketChannel.read(bb);
}
bb.flip();
imageView.setImage(new Image(new ByteArrayInputStream(bb.array())));
}
catch (IOException exc) {
log.error("read", exc);
}
}
public void connect() {
try {
socketChannel = SocketChannel.open(new InetSocketAddress(adress.getText(),
Integer.parseInt(port.getText())));
indexTextField.setDisable(false);
load.setDisable(false);
}
catch (NumberFormatException | IOException exc) {
log.error("connect", exc);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="543.0" prefWidth="472.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="koz01.java2.lab08.LocalLoaderControler">
<children>
<ImageView fx:id="imageView" fitHeight="472.0" fitWidth="472.0" pickOnBounds="true" preserveRatio="true" />
<TextField fx:id="indexTextField" layoutX="193.0" layoutY="503.0" prefHeight="26.0" prefWidth="73.0" />
<Label layoutX="122.0" layoutY="503.0" prefHeight="26.0" prefWidth="51.0" text="Index" />
<Button layoutX="299.0" layoutY="503.0" mnemonicParsing="false" onAction="#readButton" text="Načti" />
</children>
</AnchorPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="636.0" prefWidth="472.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="koz01.java2.lab08.RemoteLoaderControler">
<children>
<ImageView fx:id="imageView" fitHeight="472.0" fitWidth="472.0" pickOnBounds="true" preserveRatio="true" />
<TextField fx:id="indexTextField" disable="true" layoutX="207.0" layoutY="592.0" prefHeight="26.0" prefWidth="73.0" />
<Label layoutX="81.0" layoutY="592.0" prefHeight="26.0" prefWidth="103.0" text="Index obrázku" />
<Button fx:id="load" disable="true" layoutX="306.0" layoutY="592.0" mnemonicParsing="false" onAction="#readButton" text="Načti" />
<Label layoutX="81.0" layoutY="531.0" text="Adresa" />
<TextField fx:id="adress" layoutX="149.0" layoutY="526.0" prefHeight="26.0" prefWidth="131.0" />
<TextField fx:id="port" layoutX="207.0" layoutY="560.0" prefHeight="26.0" prefWidth="73.0" />
<Label layoutX="81.0" layoutY="565.0" prefHeight="16.0" prefWidth="46.0" text="Port" />
<Button fx:id="connect" layoutX="306.0" layoutY="526.0" mnemonicParsing="false" onAction="#connect" text="Připojit" />
</children>
</AnchorPane>
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment