gpt4 book ai didi

java - PaintIcon 不再让我绘制图像

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

所以我只是在探索java并尝试制作一个程序 map 来娱乐,但是当我开始在数组中选择随机点以设置为某个图像时,它将不再绘制。该问题仅发生在最后一个 for 循环中,并且发生在:

if(world[c][r] == 2)

mountain.paintIcon(this, g, r*Run2.distBetweenTiles, c*Run2.distBetweenTiles);

我移除了起始的草和水,看看它是否被画在下面,但事实并非如此,它只是从未被画过。如果有人能启发我那就太好了!

使用的图片:enter image description here , enter image description here , enter image description here附: Run2.rowSizeRun2.colSizeRun2.distBetweenTiles = 20(全部静态)

import java.io.IOException;
import java.util.Properties;

import javax.swing.JFrame;

public class Run2
{
public static final int rowSize = 20;
public static final int colSize = 20;
public static final int distBetweenTiles = 20;

public static void main (String [] args) throws IOException
{
JFrame frame = new JFrame();

WorldCreation2 panel = new WorldCreation2();
frame.add(panel);

frame.setTitle("RandomScape");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1280, 720);
frame.setVisible(true);
frame.setResizable(false);

Properties properties = new Properties();
properties.setProperty("Health", "20");
System.out.println(properties.getProperty("Health"));
}
}
<小时/>
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JComponent;

public class WorldCreation2 extends JComponent
{
private static final long serialVersionUID = 1L;

int rowAmount, colAmount;
int[][] world;
int islandAmount = 3;
ArrayList<Point> islandStartingPositions = new ArrayList<Point>();
protected void paintComponent(Graphics g)
{
super.paintComponent(g);

int width = getWidth();
int height = getHeight();
int rowAmount, colAmount;
int randomRow, randomCol;

ImageIcon water = new ImageIcon("C:\\Users\\CJ Petruzzelli\\Pictures\\Water 20x20.jpg");
ImageIcon grass = new ImageIcon("C:\\Users\\CJ Petruzzelli\\Pictures\\Grass 20x20.jpg");
ImageIcon mountain = new ImageIcon("C:\\Users\\CJ Petruzzelli\\Pictures\\Mountain 20x20.jpg");

colAmount = height/Run2.colSize;
rowAmount = width/Run2.rowSize;
world = new int[colAmount][rowAmount];
//System.out.println(rowAmount + " , " + colAmount);

Random rand = new Random();

//Initialize all values to default 0
for(int r = 0; r < rowAmount; r++)
{
for(int c = 0; c < colAmount; c++)
{
//Sets tiles to 0 so full array is instantiated
world[c][r] = 0;
grass.paintIcon(this, g, r*Run2.distBetweenTiles, c*Run2.distBetweenTiles);
}
}
////Puts water on all edges of the map
for(int r = 0; r < rowAmount; r++)
{
for(int c = 0; c < colAmount; c++)
{
//Left Side, Right Side, Top Side, Bottom Side
if(r == 0 && c < colAmount || r == rowAmount-1 && c < colAmount || r < rowAmount && c == 0 || r < rowAmount && c == colAmount-1)
{
world[c][r] = 1;
water.paintIcon(this, g, r*Run2.distBetweenTiles, c*Run2.distBetweenTiles);
}
}
}

//Pick starting points
int islandStartingPointNum = 0;
for(int t = islandAmount; t > 0; t--)
{
//Pick a random square (Cannot be an edge tile)
randomRow = rand.nextInt(rowAmount-1)+1;
randomCol = rand.nextInt(colAmount-1)+1;
String rowString = String.valueOf(randomRow);
String colString = String.valueOf(randomCol);
if(world[randomCol][randomRow] == 1)
{
System.out.println(colString + " , " + rowString + " This is an edge tile");
islandAmount++;
}
else
{
System.out.println(colString + " , " + rowString + " This is a grass tile, turning to mountain");
world[randomCol][randomRow] = 2;
islandAmount--;
//Store it
islandStartingPositions.add(new Point(randomCol, randomRow));
}
System.out.println(islandStartingPositions.get(islandStartingPointNum));
islandStartingPointNum++;
}

//Turn starting points into something
for(int r = 0; r < rowAmount; r++)
{
for(int c = 0; c < colAmount; c++)
{
if(world[c][r] == 2) // Checking number of location not current locations number?
{
System.out.println("This is a mountain tile");
mountain.paintIcon(this, g, r*Run2.distBetweenTiles, c*Run2.distBetweenTiles);
}
}
}
}
}

最佳答案

这是向 JComponent 添加图 block 的快速示例。
如果您需要更多帮助,请发布您问题的 MCVE。应该是这样的:

import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Run2
{

public static void main (String [] args) throws IOException
{
JFrame frame = new JFrame();
frame.add( new WorldCreation2());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}


class WorldCreation2 extends JComponent
{

private int rows = 15, cols = 10;
private URL[] urls = { //use embedded resources for posting question
getUrl("http://static.coach.com/aapz_prd/on/demandware.static/-/Library-Sites-globalLibrary/default/dw1777ce46/201708/canada-wwcm/close_grey.png"),
getUrl("https://s7d2.scene7.com/is/image/JCPenney/DP0423201517014421S.jpg?wid=20&hei=20&op_usm=.4,.8,0,0&resmode=sharp2"),
getUrl("https://i2.wp.com/www.todaytells.com/wp-content/uploads/2018/07/new-and-ongoing-cinema-releases-uk-ire-from-thu-jul-12.jpg?fit=20%2C20&ssl=1")
};

public WorldCreation2() {

setLayout(new GridLayout(rows, cols));
for(int row = 0; row < rows; row++) {
for(int col = 0; col < cols; col++) {
if(row < 5)
{
add(getTile(urls[0]));
}else if (row < 10) {
add(getTile(urls[1]));
}else {
add(getTile(urls[2]));
}
}
}
}

private JLabel getTile(URL url){

return new JLabel(new ImageIcon(url));
}

private URL getUrl(String path) {

URL url = null;
try {
url = new URL(path);
} catch (MalformedURLException ex) { ex.printStackTrace(); }

return url;
}
}

关于java - PaintIcon 不再让我绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53127422/

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