0% found this document useful (0 votes)
7 views2 pages

Clip

The document contains a Java program that implements a graphical application for clipping and filling shapes using boundary fill algorithm. It creates a window where users can click to draw a border and fill an area within defined clipping boundaries. The program uses mouse events to handle user interactions and manage the drawing on a canvas.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Clip

The document contains a Java program that implements a graphical application for clipping and filling shapes using boundary fill algorithm. It creates a window where users can click to draw a border and fill an area within defined clipping boundaries. The program uses mouse events to handle user interactions and manage the drawing on a canvas.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;
import [Link].*;
import [Link].*;
class ClippingWithFill extends JFrame implements MouseListener{
int Xmin = 50, Ymin = 50, Xmax = 450, Ymax = 450;
int x1, y1, x2, y2;
Color pixel[][]=new Color[500][500];
Graphics g;
public void paint(Graphics g) {
[Link]([Link]);
[Link](Xmin, Ymin, (Xmax - Xmin), (Ymax - Ymin));
}
ClippingWithFill() {
super("ClippingWithFill");
setSize(500, 500);
addMouseListener(this);
setVisible(true);
g = getGraphics();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void mouseClicked(MouseEvent e) {
int x=[Link](),y=[Link]();
if(Xmax-50>=x && Xmin<=x && Ymax-50>=y && Ymin<=y){
drawBorder(x,y,50,[Link]);
boundaryfill4(x+1,y+1,[Link],[Link]);
}
else{[Link]("Break the border");}
}
public void mousePressed(MouseEvent e) {
x1 = [Link]();
y1 = [Link]();
}
public void mouseReleased(MouseEvent e) {
x2 = [Link]();
y2 = [Link]();
Clipping();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public static void main(String[] args) {
new ClippingWithFill();
}
public Color getPixel(int x,int y){
return pixel[x][y];
}
public void setPixel(int x,int y,Color c){
[Link](c);
pixel[x][y]=[Link]();
[Link](x,y,1,1);
}

public void boundaryfill4(int x,int y,Color bc,Color ic){


Color pc=getPixel(x,y);
if(pc != ic && pc != bc){
setPixel(x,y,ic);
boundaryfill4(x+1,y,bc,ic);
boundaryfill4(x-1,y,bc,ic);
boundaryfill4(x,y-1,bc,ic);
boundaryfill4(x,y+1,bc,ic);
}
}

public void drawBorder(int x,int y,int d,Color c){


for(int i=1;i<=d;i++){
setPixel(x,y+i,c);
setPixel(x+d,y+i,c);
setPixel(x+i,y,c);
setPixel(x+i,y+d,c);
}
}
public void Clipping() {
int dx, dy, d, x, y;
dx = x2 - x1;
dy = y2 - y1;
int[] p = { -dx, dx, -dy, dy };
int[] q = { x1 - Xmin, Xmax - x1, y1 - Ymin, Ymax - y1 };
double t1 = 0, t2 = 1;
for (int i = 0; i < 4; i++) {
if (p[i] == 0 && q[i] < 0) {
[Link]("Line is outside the Boundry. It is not a clipping candidate.");
} else {
double t = (double) q[i] / p[i];
if (p[i] < 0)
t1 = [Link](t, t1);
else
t2 = [Link](t, t2);
}
}
if (t1 <= t2){
int nx1, ny1, nx2, ny2;
nx1 = (int) (x1 + t1 * dx);
ny1 = (int) (y1 + t1 * dy);
nx2 = (int) (x1 + t2 * dx);
ny2 = (int) (y1 + t2 * dy);
[Link]([Link]);
[Link](nx1, ny1, nx2, ny2);
}
}
}

You might also like