PaintIcon不再允许我绘制图像

时间:2018-11-03 00:45:48

标签: java swing arraylist paint

因此,我只是在探索Java并尝试制作一个过程图,以便娱乐,但是当我开始从数组中随机选择斑点以设置为特定图像时,它将不再绘制。该问题仅发生在最后一个for循环中,并且发生在:

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

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

我移走了开始的草和水,看是否在下面涂了油漆,但事实并非如此,从来没有被涂过。如果有人能启发我,那就太好了!

使用的图片:enter image description hereenter image description hereenter 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);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

以下是将切片添加到JComponent的快速示例。
如果您需要更多帮助,请发布有关您的问题的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;
    }
}