Note that there are some explanatory texts on larger screens.

plurals
  1. POBoundary fill (flood fill) algorithm to construct an interactive map. Java
    primarykey
    data
    text
    <p>I am building a US interactive map that will respond to user's input values to the JTable. I have already done this but without flood fill algorithm (each state had its own .png image). Now I have decided to use boundary fill OR seed fill... but it does not work somehow... Here is the FULL code:</p> <pre><code>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 &lt; 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 &lt; 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 &lt; 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) &amp;&amp; (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); } } </code></pre> <p>I also use ExcelAdapter.java, which is available online just to enable copy/paste to JTable. How come my code does not work...I have been debugging it... for a long time</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload