将数组添加到java应用程序

时间:2017-03-12 18:09:32

标签: java gridpane

我是Java的新手,但我正在尝试使用数组创建一个房间的地图但是我不知道如何将它添加到我的app类。由于代码太长,我将分享我一直在尝试使用的视图类,以便将其添加到我的应用程序中。我删除了代码,将其他组件添加到我的应用程序,因为长度,但附加的图片是我得到的输出。

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() {
    }
}

1 个答案:

答案 0 :(得分:0)

如果你对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模式之间切换。 通过单击鼠标创建墙/删除一些! 希望这个基地可以帮助你...