我的代码是带鼠标控制的球运动
ballFrame:
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class BallFrame extends Application {
private Pane canvas;
@Override
public void start(final Stage primaryStage) {
canvas = new Pane();
final Scene scene = new Scene(canvas, 800, 600);
primaryStage.setTitle("Game");
primaryStage.setScene(scene);
primaryStage.show();
addCircle(100, 100, Color.BLUE);
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.getButton() == MouseButton.PRIMARY) {
if (!(canvas.getChildren().isEmpty())) {
canvas.getChildren().remove(0);
}
}
else {
int red = (int)(Math.random()*256);
int green = (int)(Math.random()*256);
int blue = (int)(Math.random()*256);
int x = (int)(Math.random()*801);
int y = (int)(Math.random()*601);
addCircle(x, y, Color.rgb(red, green, blue));
}
}
});
}
private void addCircle(double x, double y, Color color) {
Circle circle = new Circle(15, color);
circle.relocate(x, y);
canvas.getChildren().addAll(circle);
final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {
double deltaX = 3;
double deltaY = 3;
@Override
public void handle(final ActionEvent t) {
circle.setLayoutX(circle.getLayoutX() + deltaX);
circle.setLayoutY(circle.getLayoutY() + deltaY);
final Bounds bounds = canvas.getBoundsInLocal();
final boolean atRightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius());
final boolean atLeftBorder = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius());
final boolean atBottomBorder = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius());
final boolean atTopBorder = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius());
if (atRightBorder || atLeftBorder) {
deltaX *= -1;
}
if (atBottomBorder || atTopBorder) {
deltaY *= -1;
}
}
}));
loop.setCycleCount(Timeline.INDEFINITE);
loop.play();
}
public static void main(final String[] args) {
launch(args);
launch(args);
}
}
这扩展到了应用程序。 我想要创建2个球框的窗口,一个是服务器套接字,另一个是客户端套接字。 我想从一个窗口发送球,如果它从右侧窗口右侧边界到另一个窗口,并且双向工作。
我不知道这样做
我尝试用这样的服务器套接字和客户端编写代码,但不知道该做什么。
public class ServerThread extends Thread {
public ServerSocket servsock;
ServerThread(int port) throws IOException {
servsock = new ServerSocket(port);
servsock.setSoTimeout(30000);
}
public void gui() {
}
public void run() {
gui();
while(true) {
try {
System.out.println("SERVER: waiting for connection on " + servsock.getLocalPort());
Socket server = servsock.accept();
System.out.println("SERVER: Client accepted: " + server);
System.out.println("SERVER: connect to "+server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
String text = in.readUTF();
System.out.println("SERVER: get \"" + text + "\"");
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF(text);
server.close();
} catch (Exception e) {
System.out.println(e.toString());
try {
servsock.close();
System.out.println("Bye bye");
break;
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
客户端:
public class ClientThread extends Thread {
Socket client;
final String url;
final int port;
public ClientThread(String ip, int port) {
this.url = ip;
this.port = port;
}
public void gui() {
}
public void run() {
gui();
display.append("Connecting to " + url + " on port " + port +"\n");
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent o) {
try {
/* CONNECT */
client = new Socket(url, port);
display.append("Just connected to " + client.getRemoteSocketAddress() +"\n");
/* SEND */
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF(input.getText());
/* READ */
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
display.append("Server says: " + in.readUTF() +"\n");
client.close();
} catch (IOException e) {
e.printStackTrace();
}
input.setText("");
}
});
}
public static void main(String[] args) throws IOException {
String serverName = "127.0.0.1";
int port = 15788;
ServerThread t = new ServerThread(port);
t.start();
Thread client = new ClientThread(serverName,port);
client.start();
}
}