1

I am pretty new to Java but I am trying to create a map of a room using an array however I do not know how to add it to my app class. Since the code is too long I will share the view class I have been trying to work with in order to add it to my application. I removed the code adding the other components to my application because of the length but the picture attached is the output I am getting.

output

Any help would be GREATLY appreciated. I have exhausted every option. Thanks.

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() {
    }
}
comp1005
  • 25
  • 6

1 Answers1

0

If you are happy with swing and a JPanel, you could do something like that :

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 :

Already tested with Ubuntu 16.04, should work with Windows and Java, too. E and W keys toggle between Empty and Wall mode. Create walls/delete some by clicking with the mouse ! Hope this base helps you...

Luatic
  • 8,513
  • 2
  • 13
  • 34