Skip to content

Instantly share code, notes, and snippets.

@PCJohn
Created September 17, 2015 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PCJohn/d09f373b8810e4cc1c62 to your computer and use it in GitHub Desktop.
Save PCJohn/d09f373b8810e4cc1c62 to your computer and use it in GitHub Desktop.
Finding closed boundaries
/*Java source to find closed boundaries an array of binary pixels.
Author: Prithvijit Chakrabarty (prithvichakra@gmail.com)
*/
package projectjava;
import java.awt.Color;
public class ClosedSurfaceFinder {
static int[][] pixels=new int[][]{{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}};
static ClosedSurfaceSelector c;
public static void findSurfaces(){
for(int i=0;i<pixels.length;i++)
for(int j=0;j<pixels[0].length;j++)
if(pixels[i][j]==1)
if(numberOfLinks(i,j)<2)
pixels[i][j]=0;
for(int i=pixels.length-1;i>=0;i--)
for(int j=pixels[0].length-1;j>=0;j--)
if(pixels[i][j]==1)
if(numberOfLinks(i,j)<2)
pixels[i][j]=0;
}
public static int numberOfLinks(int i,int j){
int linkCount=0;
int limI=pixels.length-1;
int limJ=pixels[0].length-1;
if(i!=0){
if(pixels[i-1][j]==1)
linkCount++;
}
if(i!=limI){
if(pixels[i+1][j]==1)
linkCount++;
}
if(j!=limJ&&pixels[i][j+1]==1)
linkCount++;
if(j!=0&&pixels[i][j-1]==1)
linkCount++;
return linkCount;
}
public static void main(String args[]){
findSurfaces();
for(int i=0;i<pixels.length;i++){
for(int j=0;j<pixels[0].length;j++)
System.out.print(pixels[i][j]+" ");
System.out.println();
}
}
public static void showSurfaces(){
for(int i=0;i<pixels.length;i++)
for(int j=0;j<pixels[0].length;j++)
if(pixels[i][j]==1)
c.pixels[i][j].setBackground(Color.ORANGE);
}
public static void reload(){
for(int i=0;i<pixels.length;i++)
for(int j=0;j<pixels[0].length;j++){
pixels[i][j]=0;
c.pixels[i][j].setBackground(null);
}
}
}
/*GUI frontend for closed boundary selection.
Detects closed boundaries clicked out by the user (doesn't consider diagonal elements to be adjacent).
User must click Reload before selecting a new boundary.
Author: Prithvijit Chakrabarty (prithvichakra@gmail.com)
*/
package projectjava;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ClosedSurfaceSelector extends JPanel{
JButton[][] pixels;
public ClosedSurfaceSelector() {
super();
pixels=new JButton[10][10];
JPanel panel=new JPanel();
ClosedSurfaceFinder.c=this;
panel.setLayout(new GridLayout(pixels.length,pixels[0].length));
for(int i=0;i<pixels.length;i++)
for(int j=0;j<pixels[0].length;j++){
pixels[i][j]=new JButton();
pixels[i][j].addActionListener(new ClosedPixelAction(pixels[i][j],i,j));
panel.add(pixels[i][j]);
}
setLayout(new GridLayout(2,1));
add(panel);
JPanel lower=new JPanel();
JButton find=new JButton("Find Closed Surfaces");
find.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ClosedSurfaceFinder.findSurfaces();
ClosedSurfaceFinder.findSurfaces();
ClosedSurfaceFinder.showSurfaces();
}
});
JButton reload=new JButton("Reload");
reload.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ClosedSurfaceFinder.reload();
}
});
lower.add(find);
lower.add(reload);
add(lower);
}
public static void main(String[] args){
JFrame frame=new JFrame("Closed Surface Finder");
frame.add(new ClosedSurfaceSelector());
frame.setSize(700,700);
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment