gpt4 book ai didi

java - 将 map 数组添加到java应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:16 25 4
gpt4 key购买 nike

我对 Java 很陌生,但我正在尝试使用数组创建房间 map ,但我不知道如何将其添加到我的应用程序类中。由于代码太长,我将分享我一直在尝试使用的 View 类,以便将其添加到我的应用程序中。由于长度原因,我删除了将其他组件添加到我的应用程序中的代码,但所附图片是我得到的输出。

output

任何帮助将不胜感激。我已经用尽了所有的选择。谢谢。

import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

public class FloorBuilderView extends GridPane {
private FloorPlan model;

//component on window

private Button buildingOverview;
private Button roomTilesColor;
private FloorPlan fp;
private Button[][] buttons;


private RadioButton exit;
private RadioButton walls;
private RadioButton roomTiles;
private RadioButton selectRoom;

private TextField floorSummary;
private Label selectEdit;
private Label floorLayoutLabel;
private Label floorSummaryLabel;


public FloorBuilderView(){
// model = fp; // Store the model so that the update() method can access it
fp = FloorPlan.floor1();
fp = new FloorPlan(20, "Main Floor");
int[][] tiles = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,1,1,1,0,1,1,1,1,1},
{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
for (int r=0; r<20; r++)
for (int c=0; c<20; c++)
fp.setWallAt(r,c,tiles[r][c]==1);






setPadding(new Insets(10,10,10,10));

//floorPlan = new Panel(new GridLayout(fp.size(), fp.size(), 0, 0));
//buttons = new Button[fp.size()][fp.size()];






update();

}



public void update() {
}
}

最佳答案

如果您对 swing 和 JPanel 感到满意,您可以这样做:

import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.Arrays;
import java.awt.EventQueue;
import javax.swing.JFrame;

public class FloorPlaner extends JFrame implements MouseListener, KeyListener {
protected BufferedImage wall;
public int type=0;
public int[][] tiles= {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,1,1,1,0,1,1,1,1,1},
{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};;

public void mousePressed(MouseEvent m) {
}

public void mouseClicked(MouseEvent m) { //Mouse Clicked
int mouseposx=(int) (m.getX()); //Mouse X
int mouseposy=(int) (m.getY()); //Mouse Y
if (m.getButton() == m.BUTTON1) {
tiles[mouseposx / 20][mouseposy / 20] = type;
}
}
public void mouseReleased(MouseEvent m) {
}
public void mouseEntered(MouseEvent m) {
}
public void mouseExited(MouseEvent m) {
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_W) { //Lets build a wall !
type=1;
}
if (e.getKeyCode()==KeyEvent.VK_E) { //Lets remove a wall !
type=0;
}

}
public void keyPressed(KeyEvent e) {
}
public FloorPlaner(){
super("FloorPlaner");
try {
wall = ImageIO.read(new File ("wall.png")); //Load a wall
} catch(IOException bug) { //Create a wall image
wall=new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
Graphics2D wg=wall.createGraphics();
wg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
wg.setColor(Color.BLACK);
wg.fillRect(0,0,20,20);
System.out.println(bug);
}

requestFocus();
addKeyListener(this);
addMouseListener(this);

setContentPane(new DrawingPane());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(400, 400);

setResizable(true);

setVisible(true);

while (true) {
repaint();
try {
Thread.sleep(40); //25 FPS
} catch(InterruptedException bug) {
Thread.currentThread().interrupt();
System.out.println(bug);
}
}
}


class DrawingPane extends JPanel { //Where you actually draw on
public void paintComponent(Graphics g) { //Drawing method
for (int x=0; x < tiles.length; x++) {
for (int y=0; y < tiles[x].length; y++) {
if (tiles[x][y]==1) {
g.drawImage(wall,x*20,y*20,null);
}
}
}
}
}
public static void main(String args[]) {
new FloorPlaner(); //Start it
}
}

The result of this code :

已经在 Ubuntu 16.04 上进行了测试,也应该适用于 Windows 和 Java。EW 键在 EmptyWall 模式之间切换。通过点击鼠标创建墙壁/删除一些墙壁!希望这个基地可以帮助你...

关于java - 将 map 数组添加到java应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42751165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com