2013-04-07 117 views
1

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

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。为什么我的代码不能正常工作......我一直在调试它......很长一段时间

+0

您正在使用递归洪水填充算法,这对于大型图像肯定会失败。 Scanline算法在这里最适合。查看*** [this](http://en.wikipedia.org/wiki/Flood_fill)***页面获取更多信息。该算法的实现可以发现*** [这里](http://stackoverflow.com/questions/14672861/how-to-add-mouseclicked-to-script/14673438#14673438)*** – 2013-04-08 02:46:54

+0

@Extreme我熟悉扫描线算法,但是,我的问题是:程序甚至没有显示任何更改,它是如果它忽略我的代码...图像会加载,但没有颜色变化发生在所有...但我没有得到任何错误... – Buras 2013-04-08 02:54:16

+0

@Extreme Coders ExcelAdapter没有问题。我把它作为一个单独的课程。它完美的作品。我已经检查了100%,系统输出等...我有这样的代码写在ExcelAdapter不同的方式... – Buras 2013-04-08 03:01:20

回答

0

你的代码有很多问题。实际上,据我所知,你想做一个洪水填充测试用例来编写其他应用程序。如果你熟悉python,你可能会发现以下内容很有趣Making a weighted USA map based on state-level data有一个现成的代码,只需复制粘贴它然后你可以修改它。

1

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

你boundaryFill4方法也看到检查边界如果当前像素是黑色的,这意味着它永远不会更新地图的任何黑色像素。另外,由于setPixel的更改被丢弃,它永远不会完成(可能)。

最后,因为你没有while (true) {这将只保留遍历所有的图像像素不休任何终止条件。另外,你的代码还有一些其他的改进,比如每次你想获得一个像素的颜色值时不创建一个新的bufferedImage,并且切换到一个不需要堆栈的算法在最坏的情况下最大宽度+高度的大小。

+0

。我试图纠正它。但仍然有错误。您能否推荐一个解决方案 – Buras 2013-04-10 01:15:14

+0

您需要修改您显示的图片。因此,而不是显示一个PNG文件,加载文件到一个图像对象,显示该图像,然后传递该图像周围进行修改(调用任何重绘或更新方法,如果需要,我不知道摆动)。 – Sysyphus 2013-04-10 01:24:16

+0

好的,我会试试。谢谢我会投票 – Buras 2013-04-10 01:33:39