0% found this document useful (0 votes)
56 views9 pages

Tugas 2 Di Susun Oleh Mohamad Reza 43A87006180052: Ilmu Managemen Dan Ilmu Komputer Stmik Bani Saleh 2021

This document contains the source code for a flood fill algorithm in Java. It creates a JFrame with a panel to draw on. When the user clicks on the panel, it calls the floodFill method, which recursively replaces neighboring pixels of the same color with a new color. The source code implements mouse clicking to trigger the flood fill and graphics drawing to display the results.

Uploaded by

MamangBiel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views9 pages

Tugas 2 Di Susun Oleh Mohamad Reza 43A87006180052: Ilmu Managemen Dan Ilmu Komputer Stmik Bani Saleh 2021

This document contains the source code for a flood fill algorithm in Java. It creates a JFrame with a panel to draw on. When the user clicks on the panel, it calls the floodFill method, which recursively replaces neighboring pixels of the same color with a new color. The source code implements mouse clicking to trigger the flood fill and graphics drawing to display the results.

Uploaded by

MamangBiel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Tugas 2

Di susun oleh
Mohamad Reza
43A87006180052

ILMU MANAGEMEN DAN ILMU KOMPUTER


STMIK BANI SALEH
2021
/*
* To change this license header, choose License Headers in
Project Properties.
* To change this template file, choose Tools | Templates *
and open the template in the editor.
*/ package
floodfill;

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;

/**
*
* @author
*/
// Extends ke JPanel untuk membuat Panel // dan Implements
dengan Aksi klik dari Mouse public class FloodFill extends JPanel
implements MouseListener{

// Membuat Objek private


BufferedImage image; private
Graphics2D g2;
public static void main(String[] args) {
// Membuat Frame untuk Tampilan
JFrame frame = new JFrame("Tugas 2 FloodFill");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Membuat fill dari Class


FloodFill fill=new FloodFill();
frame.add(fill); frame.pack();
frame.setVisible(true);
}

// Define Costruct Class


public FloodFill() {
// Pendefinisian Image dengan RGB image= new
BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
setPreferredSize(new
Dimension(image.getWidth(),image.getHeight()));
setMinimumSize(getPreferredSize()); // Define Image Graphics
g2=image.createGraphics(); // Set warna menjadi Biru
g2.setColor(Color.blue);

g2.drawRect(100, 100, 100, 100);


addMouseListener(this);

@Override public void


paintComponent(Graphics g)
{
// Menggambar image di lokasi 0,0
g.drawImage(image, 0, 0, null);
}

// Membuat Class untuk Memberi warna di dalam objek


public void floodFill(int seedX,int seedY,int rgb)
{
//if warna old ditemukan
if(image.getRGB(seedX, seedY)==rgb)
{
// Replace object dengan warna baru
image.setRGB(seedX,seedY,Color.red.getRGB());
// Memanggil paintComponent
update(getGraphics());

//8 connected neighbours


floodFill(seedX-1,seedY-1,rgb);
floodFill(seedX-1,seedY+1,rgb);
floodFill(seedX+1,seedY-1,rgb);
floodFill(seedX+1,seedY+1,rgb);
floodFill(seedX,seedY-1,rgb);
floodFill(seedX,seedY+1,rgb);
floodFill(seedX-1,seedY,rgb);
floodFill(seedX+1,seedY,rgb);
}

}
@Override
// Membuat Event setelah Mouse di Click
public void mouseClicked(MouseEvent e)
{
floodFill(e.getX(), e.getY(), image.getRGB(e.getX(), e.getY()));
}

@Override public void


mousePressed(MouseEvent e) {
}

@Override public void


mouseReleased(MouseEvent e) {
}

@Override public void


mouseEntered(MouseEvent e) {
}

@Override public void


mouseExited(MouseEvent e) {
}

}
HASIL SOURCE KODE

You might also like