如何在秋千上制造障碍 [英] How to create obstacle in swing

查看:102
本文介绍了如何在秋千上制造障碍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Java用红色创建这个障碍

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);
        }
    }
}

此代码创建了一个网格,但是我无法创建障碍.在图中,那些红色单元格是障碍单元格.要区分法线网格和主网格,我用红色绘制它. .我知道在Java中有一个叫做drawRect()的函数,但是我不确定它是如何工作的.根据图片,从网格(1,1)到结束(5,4)都有一个障碍.现在要将这些单元格设置为障碍单元格,我想为所有这些单元格分配1(one)表示(1,1)-> 1,然后(2,1)-> 1 ......(5, 4)-> 1等.这样,当我的机器人从一个单元移动到另一个单元时,它可以避开那些单元,并找到到达目的地的自己的路径.

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.

推荐答案

请注意代码中的注释:

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.

这篇关于如何在秋千上制造障碍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆