功能 1.天氣預報 2.區域網對戰 展示 java學習群669823128 部分源碼 package game.weather; import java.util.HashMap; public class Weather { /** * @Fields 今天的天氣數據,整體 */ private ...
功能
1.天氣預報
2.區域網對戰
展示
java學習群669823128部分源碼
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