有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将图像插入单元格

我正在创建一个迷宫,想在其中一个单元格中插入一个图像。我可以使用画笔插入和更改单元格的颜色,但我不能插入图像甚至文本。细胞似乎只接受颜料。我想在单元格中插入图像或文本,以显示起始位置或结束位置。迷宫是随机的,所以末端细胞可以改变

这是我正在使用的java文件

package com.example.mazegame;
import 安卓.content.Context;
import 安卓.graphics.BitmapFactory;
import 安卓.graphics.Canvas;
import 安卓.graphics.Color;
import 安卓.graphics.Paint;
import 安卓.graphics.Bitmap;
import 安卓.util.AttributeSet;
import 安卓.view.View;

import 安卓x.annotation.Nullable;

import java.util.ArrayList;
import java.util.Random;
import java.util.Stack;


public class GameView extends View{

    private Cell [] [] cells;
    private Cell player, exit;
    private static final int COLS=15, ROWS=15;
    private static final float WALL_THICKNESS =4;
    private float cellSize, hMargin, vMargin;
    private Paint wallPaint, startPaint, endPaint;
    public Bitmap canvasBitmap;
    private Canvas canvas;
    private Random random;

    public GameView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        wallPaint = new Paint();
        wallPaint.setColor(Color.BLACK);
        wallPaint.setStrokeWidth(WALL_THICKNESS);
        startPaint = new Paint();
        startPaint.setColor(Color.BLACK);


        endPaint = new Paint();
        endPaint.setColor(Color.BLACK);
        random = new Random();
        canvasBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.wh);

        createMaze();
    }

    private Cell getNeighbor (Cell cell) {
        ArrayList <Cell> neighbors =new ArrayList<>();

        //left neighbor
        if (cell.col >0)
            if(!cells[cell.col-1][cell.row].visited)
                neighbors.add(cells[cell.col-1][cell.row]);
        //right neighbor
        if (cell.col < COLS-1)
            if(!cells[cell.col+1][cell.row].visited)
                neighbors.add(cells[cell.col+1][cell.row]);
        //top neighbor
        if (cell.row >0)
            if(!cells[cell.col][cell.row-1].visited)
                neighbors.add(cells[cell.col][cell.row-1]);
         //bottom neighbor
        if (cell.row <ROWS-1)
            if(!cells[cell.col][cell.row+1].visited)
                neighbors.add(cells[cell.col][cell.row+1]);

        if(neighbors.size()>0 ){
        int index = random.nextInt(neighbors.size());
        return neighbors.get(index);
        }
        return null;
    }
    private void removeWall(Cell current, Cell next){
        if (current.col == next.col && current.row == next.row+1){
            current.topWall=false;
            next.bottomWall = false;
        }

        if (current.col == next.col && current.row == next.row-1){
            current.bottomWall=false;
            next.topWall = false;
        }

        if (current.col == next.col+1 && current.row == next.row){
            current.leftWall=false;
            next.rightWall = false;
        }

        if (current.col == next.col-1 && current.row == next.row){
            current.rightWall=false;
            next.leftWall = false;
        }
    }

    void createMaze() {
        Stack<Cell>stack = new Stack<>();
        Cell current, next;

        cells = new Cell [COLS] [ROWS];
        for (int x=0;x<COLS; x++) {
            for (int y=0;y<ROWS; y++){
                cells [x][y]=new Cell (x,y);
            }
        }
        player =cells [0][0];

        exit = cells [COLS-1][ROWS-1];


        current = cells [0][0];
        current.visited = true;
        do {
        next=getNeighbor (current);

        if (next !=null) {
            removeWall(current, next);
            stack.push(current);
            current = next;
            current.visited = true;
        }
        else
            current=stack.pop();
        }while (!stack.empty());

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);

        int width=getWidth();
        int height = getHeight();

        if (width/height < COLS/ROWS)
            cellSize = width/(COLS +1);
        else
            cellSize=height/(ROWS+1);

        hMargin=(width-COLS*cellSize)/2;
        vMargin=(height-ROWS*cellSize)/2;

        canvas.translate(hMargin, vMargin);

        for (int x=0;x<COLS; x++) {
            for (int y = 0; y < ROWS; y++) {
                if (cells[x][y].topWall)
                    canvas.drawLine(
                            x * cellSize,
                            y * cellSize,
                            (x + 1) * cellSize,
                            y * cellSize,
                            wallPaint);

                if (cells[x][y].leftWall)
                    canvas.drawLine(
                            x * cellSize,
                            y * cellSize,
                            x * cellSize,
                            (y + 1) * cellSize,
                            wallPaint);

                if (cells[x][y].rightWall)
                    canvas.drawLine(
                            (x + 1) * cellSize,
                            y * cellSize,
                            (x + 1) * cellSize,
                            (y + 1) * cellSize,
                            wallPaint);

                if (cells[x][y].bottomWall)
                    canvas.drawLine(
                            x * cellSize,
                            (y + 1) * cellSize,
                            (x + 1) * cellSize,
                            (y + 1) * cellSize,
                            wallPaint);


            }
        }
        float margin =cellSize/10;

    canvas.drawRect(
    player.col*cellSize+margin,
    player.row*cellSize+margin,
     (player.col+1)*cellSize-margin,
            (player.row+1)*cellSize-margin,
            startPaint);


        canvas.drawRect(

                exit.col*cellSize+margin,
                exit.row*cellSize+margin,
                (exit.col+1)*cellSize-margin,
                (exit.row+1)*cellSize-margin,
                endPaint);

    }

    private class Cell {
        boolean
        topWall = true,
        leftWall = true,
        bottomWall = true,
        rightWall = true,
        visited = false;

        int col, row;

        public Cell (int col, int row){
            this.col=col;
            this.row=row;

        }

    }

}

共 (0) 个答案