Created
February 22, 2020 18:55
-
-
Save MinSomai/c581fc84200e9db45f39ce073766a516 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hw; | |
import java.awt.*; | |
import java.awt.event.*; | |
import java.awt.image.BufferedImage; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
public class FloodFill13 extends JPanel { | |
private BufferedImage image; | |
private Graphics2D g2; | |
public static void main(String[] args) { | |
JFrame frame = new JFrame("Flood Fill Algorithm: "); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
FloodFill13 fill=new FloodFill13(); | |
frame.add(fill); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
public FloodFill13() { | |
image= new BufferedImage(1000,1000,BufferedImage.TYPE_INT_RGB); | |
setPreferredSize(new Dimension(image.getWidth(), image.getHeight())); | |
setMinimumSize(getPreferredSize()); | |
g2=image.createGraphics(); | |
g2.setColor(Color.WHITE); | |
g2.drawRect(150,200,250,100); | |
addMouseListener(new MouseAdapter() { | |
public void mouseClicked(MouseEvent e) { | |
floodFill(e.getX(), e.getY(), image.getRGB(e.getX(), e.getY())); | |
} | |
}); | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
g.drawImage(image, 0, 0, null); | |
} | |
public void floodFill(int X, int Y, int rgb) { | |
if(image.getRGB(X,Y)==rgb) | |
{ | |
image.setRGB(X,Y,Color.yellow.getRGB()); | |
update(getGraphics()); | |
floodFill(X-1, Y-1, rgb); | |
floodFill(X-1,Y+1,rgb); | |
floodFill(X+1,Y-1,rgb); | |
floodFill(X+1,Y+1,rgb); | |
floodFill(X,Y-1,rgb); | |
floodFill(X,Y+1,rgb); | |
floodFill(X-1,Y,rgb); | |
floodFill(X+1,Y,rgb); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment