第一个例子,基于网络
Client.java
import java.io.IOException
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import java.nio.channels.ServerSocketChannel
import java.nio.channels.SocketChannel
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 1532))
FileChannel fileChannel = FileChannel.open(Paths.get("1.png"), StandardOpenOption.READ)
ByteBuffer buf = ByteBuffer.allocate(1024)
while (fileChannel.read(buf) != -1){
buf.flip();
socketChannel.write(buf);
buf.clear();
}
fileChannel.close()
socketChannel.close()
Server.java
import java.io.IOException
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import java.nio.channels.ServerSocketChannel
import java.nio.channels.SocketChannel
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open()
FileChannel fileChannel = FileChannel.open(Paths.get("2.png"), StandardOpenOption.WRITE, StandardOpenOption.CREATE)
serverSocketChannel.bind(new InetSocketAddress(1532))
SocketChannel socketChannel = serverSocketChannel.accept()
ByteBuffer byteBuffer = ByteBuffer.allocate(1024)
while (socketChannel.read(byteBuffer) != -1){
byteBuffer.flip();
fileChannel.write(byteBuffer);
byteBuffer.clear();
}
socketChannel.close()
fileChannel.close()
serverSocketChannel.close()
先运行Server.java,然后再运行Client.java,这两个文件所在的目录下需要一个名为1.png的图片,还可以把这两段代码分别封装到一个方法中,然后执行方法,运行结果如下:
第二个例子,基于并发
ForkJoinSumCalculate.java
import java.util.concurrent.RecursiveTask
class ForkJoinSumCalculate extends RecursiveTask<Long>{
private long start;
private long end;
private static final long THURSHOLD = 10000L;
public ForkJoinSumCalculate(long start, long end){
this.start = start;
this.end = end;
}
@Override
protected Long compute() {
long length = end - start;
if(length < THURSHOLD){
long sum = 0L;
for(long i = start; i <= end; i++){
sum += i;
}
return sum;
}else {
long mid = (start + end) / 2;
ForkJoinSumCalculate left = new ForkJoinSumCalculate(start, mid);
left.fork(); //拆分并压入线程队列
ForkJoinSumCalculate right = new ForkJoinSumCalculate(mid + 1, end);
right.fork();
return left.join() + right.join();
}
}
}
MainScript.java
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.ForkJoinTask
import java.util.stream.LongStream
ForkJoinPool pool = new ForkJoinPool()
ForkJoinTask<Long> task = new ForkJoinSumCalculate(0L, 100000000L)
long sum = pool.invoke(task)
System.out.println(sum)
运行顺序必须先是ForkJoinSumCalculate.java,然后是MainScript.java,JShell还没有其他的解释型语言这么成熟,下面是运行结果:
第三个例子,基于JavaFX
MainController.java
public class MainController {}
这个java文件需要自己编译后打成Jar包,因为JavaFX fxml控制器的安全机制,需要JavaFX线程自己加载类,导致无法解释执行。
main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="MainController"
prefHeight="400.0" prefWidth="600.0" id="anchorPane">
</AnchorPane>
MainWindow.java
import javafx.fxml.FXMLLoader
import javafx.scene.Scene
import javafx.scene.layout.AnchorPane
import javafx.scene.paint.Color
import javafx.scene.shape.Line
import java.io.File
class MainWindow extends Scene {
public MainWindow() throws Exception {
super(FXMLLoader.load(new File("D:\\CAH\\Creat\\2019\\Learn\\Applicarion\\JShell\\test2\\main.fxml").toURL()));
AnchorPane anchorPane = (AnchorPane) lookup("#anchorPane");
for(int i = 0; i < 11; i++){
Line lineH = new Line();
lineH.setStartX(50);
lineH.setStartY(i * 50 + 50);
lineH.setEndX(600 - 50);
lineH.setEndY(i * 50 + 50);
lineH.setStroke(Color.RED);
lineH.setStrokeWidth(5);
Line lineV = new Line();
lineV.setStartX(i * 50 + 50);
lineV.setStartY(50);
lineV.setEndX(i * 50 + 50);
lineV.setEndY(600 - 50);
lineV.setStroke(Color.RED);
lineV.setStrokeWidth(5);
anchorPane.getChildren().add(lineH);
anchorPane.getChildren().add(lineV);
}
}
}
MainStage.java
import javafx.application.Application
import javafx.stage.Stage
class MainStage extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// 50 per
primaryStage.setMinWidth(600);
primaryStage.setMinHeight(600);
// 设置标题
primaryStage.setTitle("画格子");
// 加载显示面板到Stage容器内
primaryStage.setScene(new MainWindow());
// 设置窗口大小禁止改变
primaryStage.setResizable(false);
primaryStage.show();
}
}
MainScript.java
import javafx.application.Application
Application.launch(MainStage.class)
和之前说的一样,需要按照一定的顺序进行执行,同时还需要openJFX的jar包(用--class-path指定路径)和之前封装的控制器的jar包,运行结果如下:
第四个例子,基于Swing显示一张图片
ShowPicture.java
import javax.swing.ImageIcon
import javax.swing.JFrame
import javax.swing.JPanel
import java.awt.Image
import java.awt.Color
import java.awt.Graphics
class ShowPicture extends JFrame{
public ShowPicture(){
// 读取一个图片
ImageIcon icon = new ImageIcon("C:\\Users\\OVEA\\OneDrive\\图片\\本机照片\\Picture\\1022971.jpg");
Image img = icon.getImage();
JPanel base = new JPanel() {
// 显示和面板,同时将图片显示出来
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
super.paint(g);
}
};
// 设置背景透明
base.setBackground(null);
// 设置控件透明
base.setOpaque(false);
// 不使用任何布局
base.setLayout(null);
// 创建并设置窗体
this.setTitle("图片显示");
// 设置内容显示
this.setContentPane(base);
// 设置窗体大小
this.setSize(500, 500);
// 设置位置
this.setLocation(800, 300);
// 设置点击关闭按钮的默认动作
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 锁定窗体
this.setResizable(false);
// 设置背景色(虽然没啥用)
this.setBackground(Color.WHITE);
// 设置是否可见
this.setVisible(true);
// 设置居中
this.setLocationRelativeTo(null);
}
}
MainScript.java
new ShowPicture()
需要按照ShowPicture->MainScript的顺序运行,结果如下: