2017-03-12 45 views
1

我對Java很新,但我嘗試使用數組創建一個房間的地圖,但是我不知道如何將其添加到我的應用程序類。由於代碼太長,我將分享我一直在嘗試使用的視圖類,以便將其添加到我的應用程序中。我刪除了添加其他組件到我的應用程序的代碼,因爲它的長度,但附加的圖片是我得到的輸出。向Java應用程序添加地圖陣列

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

你在哪裏顯示它? – user7185318

+0

@ user7185318你是什麼意思? – comp1005

+0

你在哪裏顯示你的瓷磚/ GridLayout?無論如何,我建議使用JPanel並繪製它,因爲我認爲它會容易得多... – user7185318

回答

0

如果你很高興與擺動和一個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模式之間切換。 創建牆/通過點擊鼠標刪除一些! 希望這個基地可以幫助你...