【发布时间】:2016-10-01 06:21:44
【问题描述】:
我一直在玩 Java GUI,但遇到了一个我不知道原因的错误。
我的程序基于洪水填充(线条内的填充颜色),但是当我尝试填充时,我得到了这个错误:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.util.Hashtable.get(Unknown Source)
at javax.swing.UIDefaults.getFromHashtable(Unknown Source)
at javax.swing.UIDefaults.get(Unknown Source)
at javax.swing.MultiUIDefaults.get(Unknown Source)
at javax.swing.UIDefaults.getColor(Unknown Source)
at javax.swing.UIManager.getColor(Unknown Source)
at javax.swing.LookAndFeel.installColors(Unknown Source)
at javax.swing.LookAndFeel.installColorsAndFont(Unknown Source)
at javax.swing.plaf.basic.BasicLabelUI.installDefaults(Unknown Source)
at javax.swing.plaf.basic.BasicLabelUI.installUI(Unknown Source)
at javax.swing.JComponent.setUI(Unknown Source)
at javax.swing.JLabel.setUI(Unknown Source)
at javax.swing.JLabel.updateUI(Unknown Source)
at javax.swing.JLabel.<init>(Unknown Source)
at javax.swing.JLabel.<init>(Unknown Source)
at MapPanel.refreshImage(MapPanel.java:167)
at MapPanel.setImage(MapPanel.java:162)
at MapPanel.floodFill(MapPanel.java:224)
[这些重复了很多次]
at MapPanel.floodFill(MapPanel.java:231)
at MapPanel.floodFill(MapPanel.java:230)
[第 230 和 231 行的代码]
floodFill(newImage, mark, row - 1, col, srcColor, tgtColor);
floodFill(newImage, mark, row + 1, col, srcColor, tgtColor);
图像已更新颜色但不是全部...
我希望你明白我想说什么,我已经坚持了一段时间,任何帮助都会感谢。
代码
// Mouse Listener
public class MouseCapture implements MouseListener
{
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked");
System.out.println(e.getX() + "," + e.getY());
boolean[][] mark = new boolean[image.getHeight()][image.getWidth()];
for (int row = e.getY() + 23; row < image.getHeight(); row++) {
for (int col = e.getX() - 200; col < image.getWidth(); col++) {
floodFill(image, mark, row, col, Color.WHITE, getColor());
}
}
image.setRGB(e.getX(), e.getY() + 23, Color.BLACK.getRGB());
refreshImage();
}
}
private void floodFill(BufferedImage newImage, boolean[][] mark, int row, int col, Color srcColor, Color tgtColor)
{
// make sure row and col are inside the image
if (row < 0) return;
if (col < 0) return;
if (row >= newImage.getHeight()) return;
if (col >= newImage.getWidth()) return;
// make sure this pixel hasn't been visited yet
if (mark[row][col]) return;
// make sure this pixel is the right color to fill
if (!(newImage.getRGB(col, row) == (srcColor.getRGB()))) return;
// fill pixel with target color and mark it as visited
image.setRGB(col, row, tgtColor.getRGB());
mark[row][col] = true;
// set drawn image
setImage(newImage);
// recursively fill surrounding pixels
// (this is equivelant to depth-first search)
floodFill(newImage, mark, row - 1, col, srcColor, tgtColor);
floodFill(newImage, mark, row + 1, col, srcColor, tgtColor);
floodFill(newImage, mark, row, col - 1, srcColor, tgtColor);
floodFill(newImage, mark, row, col + 1, srcColor, tgtColor);
}
【问题讨论】:
-
你试过我的解决方案了吗?干杯!
-
很好的例子@Wojciech Kazior,明天会添加到我的程序中,因为我今天脑子很乱,需要睡觉(程序员需要睡觉:D)谢谢!
标签: java user-interface flood-fill