功能

1.天气预报
2.局域网对战

展示

 java学习群669823128
Java局域网对战游戏、天气预报项目-LMLPHP
 

部分源码

package game.weather;

import java.util.HashMap;

public class Weather {
/**
* @Fields 今天的天气数据,整体
*/
private JSONObject today;
/**
* @Fields 今天的天气指数 index第一个String是指数中文名,index的Map键值为"ivalue" "detail";
*/
private JSONArray index;
/**
* @Fields 一周的天气,JSONObject:"night" "day";
*/
private JSONArray daily; private JSONArray hourly; private JSONObject aqi; /**
*
* @Title: getSubJSONObject @Description: TODO(这里用一句话描述这个方法的作用) @param
* JSONObject obj ,String key @return JSONObject 返回类型 @throws
*/
public JSONObject getToday(JSONObject obj) {
return today = (JSONObject) obj;
}
public JSONArray getIndex(JSONObject obj){
return index = (JSONArray) obj.get("index");
} public JSONArray getDaily(JSONObject obj) {
return daily = (JSONArray) obj.get("daily");
} public JSONArray getHourly(JSONObject obj){
return hourly = (JSONArray) obj.get("hourly");
}
public JSONObject getAqi(JSONObject obj){
return aqi = obj.getJSONObject("aqi");
} public static JSONObject getSubJSONObject(JSONObject obj, String key) {
Object obj2 = obj.get(key);
if (obj2 instanceof JSONObject) {
return (JSONObject) obj2;
} else {
System.out.println(key + "不是一个JSONObject");
}
return null;
} public static JSONObject ipToWeather(String ip) {
String host = "http://jisutqybmf.market.alicloudapi.com";
String path = "/weather/query";
String method = "GET";
String appcode = "8c97675e6493491d8153093a1facdc0e";
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + appcode);
Map<String, String> querys = new HashMap<String, String>();
// System.out.println(ip);
querys.put("ip", ip); // Map<String, String> res = new HashMap<>(); try {
HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
// 获取response的body
System.out.println("111");
String json = EntityUtils.toString(response.getEntity());
// json = (json == null)? "{}":json;
System.out.println("222");
if (json.equals("")) {
System.out.println("333");
return null;
}
System.out.println(json); // 解析json
JSONObject obj = JSONObject.fromString(json);
String ret = (String) obj.get("msg");
if (ret.equals("ok")) {
// 获取result里的json数据
String ret2 = obj.getString("result");
JSONObject obj2 = JSONObject.fromString(ret2); // // 直接提取数据,城市
// String value = obj2.getString("city");
// res.put("city", value);
// // 日期
// value = obj2.getString("date");
// res.put("date", value);
// // 周次
// value = obj2.getString("week");
// res.put("week", value);
// // 天气
// value = obj2.getString("weather");
// res.put("weather", value);
// // 温度
// value = obj2.getString("temp");
// res.put("temp", value);
//
// analysisJson(obj2); return obj2;
}
} catch (Exception e) {
e.printStackTrace();
} return null;
} // 递归解析JSON
private static void analysisJson(JSONObject obj) {
Iterator keys = obj.keys();
while (keys.hasNext()) {
String keyName = keys.next().toString();
Object obj2 = obj.get(keyName);
if (obj2 instanceof JSONObject) {
analysisJson((JSONObject) obj2);
} else if (obj2 instanceof JSONArray) {
for (int i = 0; i < ((JSONArray) obj2).length(); ++i) {
JSONObject obj3 = ((JSONArray) obj2).getJSONObject(i);
System.out.println("[");
analysisJson(obj3);
System.out.println("]");
}
} else {
System.out.println(keyName + ": " + (String) obj2);
}
}
} public static void main(String[] args) {
Weather.ipToWeather("119.75.217.109");
}
}
package game.ui;

import java.awt.geom.Rectangle2D;

/**
*
* @ClassName: Main
* @Description: 游戏启动界面
* @author: Administrator
* @date: 2017年7月5日 下午6:37:53
*/
public class Main extends Application {
/**
* 滚动信息的总条数
*/
private static final int countLabel = 23;
/**
* 背景的WebView
*/
private WebView w;
/**
* 按下鼠标时的x
*/
private double xOffset;
/**
* 按下鼠标开始拖动窗体时的x
*/
private double startX;
/**
* 按下鼠标开始拖动窗体时的y
*/
private double startY;
/**
* 按下鼠标时的y
*/
private double yOffset;
/**
* 川师一区
*/
private ToggleButton cs1;
/**
* 川师二区
*/
private ToggleButton cs2;
/**
* 屏幕宽度
*/
private static final int width = 1000;
/**
* 屏幕高度
*/
private static final int hight = 655;
/**
* 按钮互斥组
*/
ToggleGroup csGroup = new ToggleGroup();
/**
* Web引擎
*/
WebEngine webEngine;
/**
* 滚动信息的Label存储List
*/
List<Label> rollInfor = new ArrayList<>();
/**
* 加载fxml的root
*/
Parent root;
/**
* 存放滚动和信息的VBox
*/
private VBox rollBox;
/**
* 主舞台
*/
private Stage primaryStage;
/**
* 开始游戏按钮
*/
private Button startG;
/**
* 播放声音的线程
*/
private Thread thradePlay;
/**
* 旋转
*/
private RotateTransition rotateTransition;
/**
* 声音
*/
private String[] soundPath = { "sound//start1.wav", "sound//start2.wav", "sound//start3.wav" };
/**
* 退出游戏
*/
private Button exitButton;
/**
* 健康按钮
*/
private Button healthButton;
/**
* 设置按钮
*/
private Button setButton;
private GridPane mainPane;
private LineChart<Number, Number> lineChart;
XYChart.Series series;
private Axis<Number> xAxis;
private Axis<Number> yAxis;
private WebView bgWeb;
private WebEngine bgEngine;
private GridPane gp; /*
* (non Javadoc)
*
* @Title: start
*
* @Description: 舞台初始化
*
* @param primaryStage
*
* @see javafx.application.Application#start(javafx.stage.Stage)
*/
@Override
public void start(Stage primaryStage) {
try { primaryStage.getIcons().add(new Image(new File("img//icon.png").toURL().toString()));
primaryStage.setTitle("血战川师实验楼");
this.primaryStage = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("/game/ui/main.fxml"));
this.root = root;
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene); // 加载组件
loadModule(root);
// 加载网页
loadHTML("start//index.html",webEngine);
// 加载提示信息
loadRollInfor(); // 窗口关闭
closeWindow();
// 进入游戏点击事件
clickedStartG();
// 播放声音
CSTool.playSound("sound//introduce.wav");
thradePlay = new Thread(new CSTool.PlaySound(soundPath));
thradePlay.start();
// 关闭窗口
clickedExit();
// 健康按钮
clickedHealth();
// 设置按钮
clickedSet();
// 天气旋转
// transWeather();
// 创建图标
createChat();
// 滚动控制
rollLabel();
// Stage设置
prepareStage(primaryStage); } catch (Exception e) {
e.printStackTrace();
}
} private void createChat() { // 定义序列对<x,y>
series = new XYChart.Series(); // 把序列对添加到坐标系
lineChart.getData().add(series); lineChart.setLayoutY(100); }
//
// private void transWeather() {
//
// try {
// weaherIV.setImage(new Image(new File("img//head.png").toURL().toString()));
// } catch (MalformedURLException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }
//
// weaherIV.setFitWidth(200);
// weaherIV.setFitHeight(200);
// rotateTransition = RotateTransitionBuilder.create().node(weaherIV).duration(Duration.seconds(4))
// .axis(new Point3D(0, 100, 0)).fromAngle(0).toAngle(180).cycleCount(Timeline.INDEFINITE)
// .autoReverse(true).build();
// rotateTransition.play();
// } /**
*
* @Title: loadModule
* @Description: 从root寻找组件
* @param root
* @return: void
*/
private void loadModule(Parent root) { gp = (GridPane) root.lookup("#gp");
// 背景webView
bgWeb = (WebView) root.lookup("#bgWeb");
bgEngine = bgWeb.getEngine();
// 加载WebView
w = (WebView) root.lookup("#w");
// 加载WebViewEngine
webEngine = w.getEngine();
// 川师一区
cs1 = (ToggleButton) root.lookup("#cs1");
// 川师二区
cs2 = (ToggleButton) root.lookup("#cs2");
// 互斥按钮组
cs1.setToggleGroup(csGroup);
cs2.setToggleGroup(csGroup);
// 提示消息
rollBox = (VBox) root.lookup("#rollBox");
// 进入游戏
startG = (Button) root.lookup("#startG");
// 退出游戏
exitButton = (Button) root.lookup("#exitButton");
// 健康按钮
healthButton = (Button) root.lookup("#healthButton");
// 设置按钮
setButton = (Button) root.lookup("#setButton");
// 天气View
// weaherIV = (ImageView) root.lookup("#weaherIV");
// 折线图
lineChart = (LineChart<Number, Number>) root.lookup("#lineChart");
// y轴
yAxis = (NumberAxis) root.lookup("#yAxis");
// x轴
xAxis = (NumberAxis) root.lookup("#xAxis"); } /**
*
* @Title: clickedStartG
* @Description: 点击进入游戏事件
* @return: void
*/
private void clickedStartG() {
// 登录
if (startG != null) {
startG.setOnMouseClicked((MouseEvent event) -> {
// 启动客户端
new TankClient().launchFrame();
// primaryStage.close();
// new TankClient().lauchFrame();
// primaryStage.hide();
if (thradePlay.isAlive()) {
thradePlay.stop();
}
});
}
} /**
*
* @Title: clickedExit
* @Description: 退出按钮
* @return: void
*/
private void clickedExit() {
// 退出
if (exitButton != null) {
exitButton.setOnMouseClicked((MouseEvent event) -> {
System.exit(0);
});
}
} /**
*
* @Title: closeWindow
* @Description: 关闭窗口
* @return: void
*/
private void closeWindow() {
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) { System.exit(0);
}
});
} /**
*
* @Title: clickedExit
* @Description: 退出按钮
* @return: void
*/
private void clickedHealth() {
// 健康
if (healthButton != null) {
healthButton.setOnMouseClicked((MouseEvent event) -> {
new Health().start(new Stage()); });
}
} /**
*
* @Title: clickedSet
* @Description: 设置按钮
* @return: void
*/
private void clickedSet() {
// 设置
if (setButton != null) {
setButton.setOnMouseClicked((MouseEvent event) -> {
System.out.println("设置按钮");
loadHTML("bg//index.html",bgEngine);
rotateTransition = RotateTransitionBuilder.create().node(gp).duration(Duration.seconds(4))
.axis(new Point3D(0, 100, 0)).fromAngle(0).toAngle(180).cycleCount(2)
.autoReverse(true).build();
rotateTransition.play();
});
}
} /**
*
* @Title: rollLabel
* @Description: 滚动信息控制
* @return: void
*/
private void rollLabel() { Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int countChat = 1;
int countWeb = 0;
int i = 1; @Override
public void run() {
while (true) {
Platform.runLater(() -> {
rollBox.getChildren().clear();
if (countWeb == 20) {
countWeb = 1;
series.getData().clear();
countChat = 0;
loadHTML("start//index.html",webEngine);
}
++countWeb;
}); Platform.runLater(() -> {
XYChart.Data xy = new XYChart.Data(i, Math.random() * 1000); series.getData().add(xy);
}); ++countChat; int j = 1;
while (j <= countLabel) {
if (i >= countLabel) {
i = i % countLabel;
}
Label l = rollInfor.get(i);
Platform.runLater(() -> {
rollBox.getChildren().add(l);
}); ++i;
++j;
}
++i; // next作为current
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}, 1000, 5000);
} /**
*
* @Title: loadRollInfor
* @Description: 滚动信息设置
* @return: void
*/
private void loadRollInfor() {
// 1
rollInfor.add(new Label("欢迎来到血战川师实验楼!"));
// 2
rollInfor.add(new Label("您是第1024位玩家!"));
// 3
rollInfor.add(new Label("玩家枸杞子加入游戏!"));
// 4
rollInfor.add(new Label("玩家孤独求败加入游戏!"));
// 5
rollInfor.add(new Label("玩家晓风寒月加入游戏!"));
// 6
rollInfor.add(new Label("川师第一实验楼失守!"));
// 7
rollInfor.add(new Label("川师第二实验楼受到第一轮进攻!"));
// 8
rollInfor.add(new Label("抵制盗版游戏!"));
// 9
rollInfor.add(new Label("游戏有害健康"));
// 10
rollInfor.add(new Label("每日限制登录4小时!"));
// 11
rollInfor.add(new Label("川师第二实验楼受到第二轮进攻!"));
// 12
rollInfor.add(new Label("玩家枸杞子取得狗头一个!"));
// 13
rollInfor.add(new Label("新枪支黄金战枪上线"));
// 14
rollInfor.add(new Label("活动期间,购买商品价格减半"));
// 15
rollInfor.add(new Label("玩家阡陌上线!"));
// 16
rollInfor.add(new Label("您已登录5小时,请注意合理游戏!"));
// 17
rollInfor.add(new Label("有版本可更新,请下载!"));
// 18
rollInfor.add(new Label("由于苹果商店限制,已取消热更新功能!"));
// 19
rollInfor.add(new Label("川师第一实验楼已被修复!"));
// 20
rollInfor.add(new Label("川师第二实验楼失守!"));
// 21
rollInfor.add(new Label("川师第三实验楼受到第一轮进攻!"));
// 22
rollInfor.add(new Label("您的账号近期出现异地登录,请尽快修改密码!"));
// 23
rollInfor.add(new Label("川师一区开服!")); for (int i = 0; i < countLabel; ++i) {
rollBox.getChildren().add(rollInfor.get(i));
}
} /**
*
* @Title: prepareStage
* @Description: 舞台准备
* @param primaryStage
* @return: void
*/
private void prepareStage(Stage primaryStage) {
primaryStage.setResizable(false);
primaryStage.setWidth(width);
primaryStage.setHeight(hight);
primaryStage.show();
} /**
*
* @Title: loadHTML
* @Description: 加载HTML网页
* @param filePath
* @return: void
*/
private void loadHTML(String filePath,WebEngine webEngine) { try {
File file = new File(filePath);
if (file.exists()) {
System.out.println("00");
}
webEngine.load(file.toURL().toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
w.setMinWidth(width);
w.setMinHeight(hight);
} /**
*
* @Title: snapshot
* @Description: 网页截图
* @param view
* @return: void
*/
public void snapshot(Node view) {
Image image = view.snapshot(null, null);
try {
System.out.println("sdsds");
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png",
new File("f:\\" + System.currentTimeMillis() + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
} /**
*
* @Title: noBorderMove
* @Description: 无边框,可移动
* @param primaryStage
* @param root
* @return: void
*/
private void noBorderMove(Stage primaryStage, Parent root) {
primaryStage.initStyle(StageStyle.UNDECORATED);// 设定窗口无边框 // 可拖动
root.setOnMousePressed((MouseEvent event) -> {
event.consume();
xOffset = event.getScreenX();
startX = primaryStage.getX();
startY = primaryStage.getY();
yOffset = event.getScreenY();
// System.out.println(xOffset + ":" + yOffset);
}); // root.setOnMouseDragEntered
root.setOnMouseDragged((MouseEvent event) -> {
// System.out.println(event.getSceneX() + "-" + event.getSceneY()); double xx = event.getScreenX();
double yy = event.getScreenY();
double x = xx - xOffset + startX;
double y = yy - yOffset + startY;
primaryStage.setX(x);
primaryStage.setY(y);
}); } // 自己实现多线程调用截屏,较麻烦
private class Snap implements Runnable {
public void run() {
while (true) {
try {
Thread.sleep(1000);// 该命令不可直接在Fx用户线程执行,否则会导致前台的渲染线程暂停,页面不会被加载
Platform.runLater(new Runnable() { @Override
public void run() {
snapshot(w);// 在后台线程中不可以直接操作UI,需要借助runLater
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} /**
*
* @Title: main
* @Description: 主函数
* @param args
* @return: void
*/
public static void main(String[] args) {
launch(args);
}
}
java学习群669823128
05-07 15:30