Skip to content

Instantly share code, notes, and snippets.

@MinSomai
Created February 22, 2020 19:05
Show Gist options
  • Save MinSomai/98e78ef7383c274ec886fdbc9deb331d to your computer and use it in GitHub Desktop.
Save MinSomai/98e78ef7383c274ec886fdbc9deb331d to your computer and use it in GitHub Desktop.
package hw;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BoundryFillAlgorithm13 extends JPanel implements MouseListener{
private BufferedImage image,image1;
private Graphics2D g2;
public static void main(String[] args) {
JFrame frame=new JFrame("First");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoundryFillAlgorithm13 fill=new BoundryFillAlgorithm13();
frame.add(fill);
frame.pack();
frame.setVisible(true);
}
public BoundryFillAlgorithm13() {
image=new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
setPreferredSize(new Dimension(image.getWidth(),image.getHeight()));
setMinimumSize(getPreferredSize());
g2=image.createGraphics();
g2.setColor(Color.BLACK);
g2.drawRect(25,25,90,90);
addMouseListener(this);
image1=new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
setPreferredSize(new Dimension(image1.getWidth(),image1.getHeight()));
setMinimumSize(getPreferredSize());
g2=image.createGraphics();
g2.setColor(Color.WHITE);
g2.drawRect(10, 10, 120, 120);
addMouseListener(this);
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
public void boundaryFill(int x,int y,int BC,int FC) {
if(image.getRGB(x,y)!=BC&&image.getRGB(x,y)!=FC) {
update(getGraphics());
image.setRGB(x,y,Color.red.getRGB());
boundaryFill(x+1,y,FC,BC);
boundaryFill(x-1,y,FC,BC);
boundaryFill(x,y+1,FC,BC);
boundaryFill(x,y-1,FC,BC);
}
}
public void mouseClicked(MouseEvent e) {
boundaryFill(e.getX(),e.getY(),Color.WHITE.getRGB(),Color.red.getRGB());
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment