简体   繁体   中英

How to create obstacle in swing

I want to create this obstacle in red color in Java

在此处输入图片说明

class Grid extends JFrame{
    public Grid(){
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(250,300);
        // Set the width,height,number of rows and columns
        final GridPanel panel = new GridPanel(300, 300, 10, 10);
        add(panel);
    }
}
class GridPanel22 extends JPanel{
    int width, height, rows, columns;
    int gridWidth, gridHeight;
    public GridPanel22(int width, int height, int rows, int columns){
        gridHeight = height / rows;
        gridWidth = width / columns;
        setPreferredSize(new Dimension(width, height));
    }
    public void paint(Graphics g){

        g.setColor(Color.red);
        for (int i = 1; i <= rows; i++) {
            g.drawLine(0, i * gridHeight, width, i * gridHeight);
        }
        for (int j = 1; j <= columns; j++) {
            g.drawLine(j * gridWidth, 0, j * gridWidth, height);
        }
    }
}

This code creates a grid but I'm unable to create an obstacles. In the picture those Red cells are the obstacle cell.To distinguish between normal grid and main grid I paint it with red. . I know that there is a function called drawRect() in java but I'm not sure how it works.As per the picture there is a obstacle starting from grid (1,1) and ending it (5,4). Now to make those cell as a obstacle cell , I want to assign 1(one) to all those cells means (1,1)->1,then (2,1)->1 ......(5,4)->1 etc. So that when my Robot move from one cell to another it can avoid those cell and found it's own path to go to destination.

Please note the comments in the code :

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class Grid extends JFrame{

    public Grid(){
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        final GridPanel panel = new GridPanel(300, 300, 4, 4);
        panel.setColor(1, 1, Color.RED); //change cell colors 
        panel.setColor(2, 3, Color.RED);
        add(panel);
        pack();
        setVisible(true);
    }
}

class GridPanel extends JPanel{

    //store reference to each cells
    Component[][] cells;

    public GridPanel (int width, int height, int rows, int columns){

        cells = new Component[rows][columns];
        //use grid layout to get the layout you want 
        setLayout(new GridLayout(rows, columns));
        setPreferredSize(new Dimension(width, height));
        for(int row =0; row < rows; row ++){
            for(int col =0; col < columns; col ++){
                Component cell = getCell();
                cells[row][col] = cell;
                add(cell);
            }
        }
    }

    //represent each cell as a label 
    Component getCell() {
        JLabel label = new JLabel();
        label.setBackground(Color.WHITE);
        label.setOpaque(true);
        label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        return label;
    }

    //control color of each cell 
    void setColor(int row, int col, Color color) {
        cells[row][col].setBackground(color);
    }

    public static void main(String[] args) { new Grid();    }
}

Don't hesitate to ask for clarifications as needed.

Your only problem is that you are not assigning your fields the values passed to your constructor:

public GridPanel22(int width, int height, int rows, int columns) {
    this.width = width;
    this.height = height;
    this.rows = rows;
    this.columns = columns;
    gridHeight = height / rows;
    gridWidth = width / columns;
    setPreferredSize(new Dimension(width, height));
}

Also, change

final GridPanel panel = new GridPanel(300, 300, 10, 10);

to

final GridPanel22 panel = new GridPanel22(300, 300, 10, 10);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM