1

我正在构建一个美国交互式地图,它将响应用户对 JTable 的输入值。我已经这样做了,但没有洪水填充算法(每个州都有自己的 .png 图像)。现在我决定使用边界填充或种子填充......但它不能以某种方式工作......这是完整的代码:

import java.awt.Color;
import java.awt.Container;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;

public class MapTest extends JFrame {


private static JTable table;
private JTable tableS;
private String[] states = {"US STATES", "Alabama",  "Alaska",   "Arizona"   };
private JLabel map;

private String[][] statesPixel = { {    "alabama",  "300",  "300"   },
        {   "alaska",   "350",  "350"   },
        {   "arizona",  "400",  "400"   },
        {   "arkansas", "450",  "450"   }   };


public MapTest() throws InterruptedException
{
    createMap();
}
private void createMap() throws InterruptedException {
Container contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);

contentPane.setSize(1220,700);

tableS = new JTable(4,1);
tableS.setBounds(1000,16,120,800);
tableS.setRowHeight(12);

int i = 0;
while (i < states.length) {
    tableS.setValueAt(states[i], i, 0);
    i++;
}

contentPane.add(tableS);


table = new JTable(4,1);
table.setBounds(1120,16,50,800);
table.setRowHeight(12);
int j = 0;
while (j < states.length) {
    table.setValueAt("100", j, 0);
    j++;
}
table.setValueAt("VALUE",0,0);
contentPane.add(table);

ExcelAdapter excelTable = new ExcelAdapter(table);

map = new JLabel();
map.setIcon(new ImageIcon("map.png"));
map.setBounds(150,50,800,600);
contentPane.add(map);

setTitle("Map");
setSize(1220,700);
setVisible(true);
setLocationRelativeTo(null);

  //updates~~
   while (true) {
    for ( int k = 0; k < statesPixel.length; k++) {
        int fill = Integer.parseInt( (String) table.getValueAt(k+1, 0));
    boundaryFill4(Integer.parseInt(statesPixel[k][1]),Integer.parseInt(statesPixel[k][2]),statesPixel[k][0],fill+1,0);
    }

}


//*******************************************************************
}

    private void boundaryFill4 (int x, int y, String state, int fill, int boundary) {

       int current;

       current = getPixel (x, y);
       if ((current != boundary) && (current != fill)) {
       setPixel (x, y, fill);
       boundaryFill4 (x+1, y, state, fill, boundary);
       boundaryFill4 (x-1, y, state,fill, boundary);
       boundaryFill4 (x, y+1, state,fill, boundary);
       boundaryFill4 (x, y-1, state,fill, boundary) ;
     }
    }

    private int getPixel(int x, int y) {
    Image img = ((ImageIcon) map.getIcon()).getImage();
    BufferedImage buffered = new BufferedImage(img.getWidth(null),img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    buffered.getGraphics().drawImage(img, 0, 0, null);
    int current = buffered.getRGB(x, y);    
    return current;
    }

    private void setPixel(int x, int y, int fill) {
    Image img = ((ImageIcon) map.getIcon()).getImage();
    BufferedImage buffered = new BufferedImage(img.getWidth(null),img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    buffered.getGraphics().drawImage(img, 0, 0, null);
    int red = fill;
    int green = red;
    int blue = red;
    Color c = new Color(buffered.getRGB(x, y));
    c = new Color(red, green, blue);
    buffered.setRGB(x, y, c.getRGB());
}

    public static void main(String args[]) throws InterruptedException {
MapTest map = new MapTest();
map.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

 } 

我还使用 ExcelAdapter.java,它可以在线获得,只是为了启用复制/粘贴到 JTable。我的代码怎么不行……我调试了……好久了

4

2 回答 2

1

您的 setPixel 方法对新的 BufferedImage 进行操作,而不是您的实际图像,因此任何更改都会被丢弃。

Your boundaryFill4 method also checks for boundaries by seeing if the current pixel is black, which means it will never update any black pixels in the map. Also, since setPixel's changes are discarded, it will never finish (probably).

And lastly, because of your while (true) { without any termination condition it's going to just keep looping over all the image pixels endlessly.

Additionally, your code has a few other improvements to be made, such as not creating a new bufferedImage every time you want to get the colour value of a pixel, and indeed switching to an algorithm which doesn't need a stack size of up width+height in the worst case.

于 2013-04-10T01:10:41.197 回答
0

Your code has a lot of issues. Actually, as I understand you want to make a test case of a flood fill in order to write some other application. If you are familiar with python, you may find the following to be interesting Making a weighted USA map based on state-level data There is a ready code, just copy paste it and then you can modify it.

于 2013-04-10T04:43:26.967 回答