gpt4 book ai didi

java - 这段代码应该创建一个 JFrame 并在其上绘制......但它没有

转载 作者:行者123 更新时间:2023-11-30 11:18:45 25 4
gpt4 key购买 nike

我已经盯着这个看好几天了...

我认为 Imageicon 部分是正确的,并且引用了正确的对象。它与创建看板、 map 和其他东西有关,我不太擅长这些。

public class Maze {
public static final int SCREEN_SIZE = 1024;
public static void main(String[] args){
new Maze();
}

// help from: http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
public Maze(){
JFrame f = new JFrame();
f.setTitle("Maze");
f.add(new Board());
f.setSize(SCREEN_SIZE,SCREEN_SIZE);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

所以你创建屏幕,制作板,获取图像,然后根据数组的内容进行绘制。

public class Board extends JPanel implements ActionListener {
public static final int TILE_SIZE = 32;
private Timer timer;
public Map m;

public Board(){
m = new Map();
timer = new Timer(25, this);
timer.start();
}

@Override
public void actionPerformed(ActionEvent e) {
repaint(); // TODO Auto-generated method stub
}

public void paint(Graphics g){
super.paint(g);
for (int y = 0; y < TILE_SIZE; y++) {
for (int x = 0; x < TILE_SIZE; x++) {
if(m.Map[x][y] == 0) {
g.drawImage(m.getFloor(), x * 32, y * 32, null);
}
if (m.Map[x][y] == 1) {
g.drawImage(m.getWall(), x * 32, y * 32, null);
}
}
}
}
}

public class Map {
public static Image floor, wall;
public int Map[][] = new int[Maze.SCREEN_SIZE / Board.TILE_SIZE][Maze.SCREEN_SIZE / Board.TILE_SIZE];

public Map() {
ImageIcon img = new ImageIcon("C://Users/Brian2/Pictures/backgrounds/FLOOR.png");
floor = img.getImage();
img = new ImageIcon("C://Users/Brian2/Pictures/backgrounds/WALL");
wall = img.getImage();
GenMap();
}

public void GenMap() {
for(int i = 0; i < Maze.SCREEN_SIZE / Board.TILE_SIZE; i++){
for(int j = 0; j < Maze.SCREEN_SIZE / Board.TILE_SIZE; j++){
Map[i][j] = 1;
}
}
}

public Image getWall() {
return wall;
}

public Image getFloor() {
return floor;
}

public void printMap() {}
}

它不会在我运行它时打开的 JPanel 上显示任何内容。我不知道出了什么问题。没有错误,我已经卡住了大约 4 天。

最佳答案

我试图重现您的问题,但它对我有用。看你用的是哪个JDK,我用的是1.6.0_45。

不过我假设你想画一张“世界”的 map 。

我重新组织了你的代码,因为我认为你也试图做一些不必要的事情,比如保留一个整数数组来为世界建模,但实际上你将它转换为图像。

董事会类:

package org.myself.mazetest;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel implements ActionListener
{
private static final int BOARD_SIZE = 1024;

private static final int FRAME_VERTICAL_BORDERS = 40;

private static final int FRAME_HORIZONTAL_BORDERS = 20;

private static final int TILE_SIZE = 32;

private static final int TIMER_DELAY = 25;

private final Timer timer;

private final int tileCount;

public Map map;


public Board( )
{
tileCount = Board.BOARD_SIZE / Board.TILE_SIZE;
map = new Map( tileCount );
timer = new Timer( Board.TIMER_DELAY, this );
timer.start();
setSize( Board.BOARD_SIZE, Board.BOARD_SIZE );
}


@Override
public void actionPerformed( final ActionEvent e )
{
repaint(); // TODO Auto-generated method stub
}


@Override
public void paintComponent( final Graphics g )
{
super.paintComponent( g );
Image img;
for ( int y = 0; y < tileCount; y++ )
{
for ( int x = 0; x < tileCount; x++ )
{
img = map.getImageFor( x, y );
if ( img != null )
{
g.drawImage( img, x * Board.TILE_SIZE, y * Board.TILE_SIZE, null );
}
}
}

}


public static void main( final String[] args )
{
final JFrame f = new JFrame();
f.setTitle( "Maze" );
f.setLayout( null );
f.add( new Board() );
f.setSize( Board.BOARD_SIZE + Board.FRAME_HORIZONTAL_BORDERS,
Board.BOARD_SIZE + Board.FRAME_VERTICAL_BORDERS );
f.setLocationRelativeTo( null );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.setVisible( true );
}
}

还有 map 类:

package org.myself.mazetest;

import java.awt.Image;

import javax.swing.ImageIcon;

public class Map
{
private final Image floor, wall;

private final Image mapArray[][];

private final int tileCount;


public Map( final int tileCount )
{
mapArray = new Image[tileCount][tileCount];
this.tileCount = tileCount;
ImageIcon img = new ImageIcon( "C:\\Users\\Brian2\\Pictures\\backgrounds\\FLOOR.png" );
floor = img.getImage();
//TODO: sure without extension?
img = new ImageIcon( "C:\\Users\\Brian2\\Pictures\\backgrounds\\WALL" );
wall = img.getImage();
//now build up the world's map :)
genMap();
}


private void genMap()
{
//we build the inner area
//that's why we start from 1, and go until tileCount - 1
for ( int row = 1; row < (tileCount - 1); row++ )
{
for ( int col = 1; col < (tileCount - 1); col++ )
{
mapArray[row][col] = floor;
}
}

//we build the wall itself
final int firstRow = 0;
final int lastRow = tileCount - 1;
final int firstCol = 0;
final int lastCol = tileCount - 1;
for ( int col = 0; col < tileCount; col++ )
{
mapArray[firstRow][col] = wall;
mapArray[lastRow][col] = wall;
}
for ( int row = 0; row < tileCount; row++ )
{
mapArray[row][firstCol] = wall;
mapArray[row][lastCol] = wall;
}

//this also can be done as follows, but I don't want do make confusion
/*
for ( int colOrRow = 0; colOrRow < tileCount; colOrRow++ )
{
mapArray[firstRow][colOrRow] = wall;
mapArray[lastRow][colOrRow] = wall;

mapArray[colOrRow][firstCol] = wall;
mapArray[colOrRow][lastCol] = wall;
}
*/
}


public Image getImageFor( final int col, final int row )
{
return mapArray[row][col];
}

}

附件是输出结果(墙壁和地板图像来自谷歌)。 The output of the main method

请检查您的墙图,在您的示例中它没有扩展名...这可能会导致某些图像读取 API 出现问题。

我将 Maze 类中的代码移到了 board 上,因为实际上它只是一个框架创建,我认为不需要为此使用单独的类。

我试图给出详细的名称和评论,但如果有什么不清楚的地方,请随时回来找我。

关于java - 这段代码应该创建一个 JFrame 并在其上绘制......但它没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23645729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com