Skip to content

Instantly share code, notes, and snippets.

@AlexFrazer
Last active December 11, 2015 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AlexFrazer/fca33e9ffa8bf508f4fb to your computer and use it in GitHub Desktop.
Save AlexFrazer/fca33e9ffa8bf508f4fb to your computer and use it in GitHub Desktop.
package Grid;
/** color selector for the lines of the grid */
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class ColorSelector extends JPanel implements ChangeListener
{
21private static final long serialVersionUID = 1L;
public Color color = null;
JColorChooser chooser;
public ColorSelector() {
super(new BorderLayout());
chooser = new JColorChooser();
chooser.getSelectionModel().addChangeListener(this);
chooser.setBorder(BorderFactory.createTitledBorder("Choose Line Color"));
add(chooser, BorderLayout.PAGE_END);
}
public void stateChanged(ChangeEvent e) {
color = chooser.getColor();
}
public Color getColor() {
return color;
}
private static void createGUI() {
JFrame frame = new JFrame("Palette");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
JComponent newContentPane = new ColorSelector();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch(InstantiationException e) {
e.printStackTrace();
}catch(IllegalAccessException e) {
e.printStackTrace();
}catch(UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
}
package Grid;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
public class EdgeFinder {
21private BufferedImage Gx, Gy;
public BufferedImage detect(BufferedImage img, double constant)
{
//21326564.99;
float[] x1 = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
float[] y1 = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
Kernel MatrixA = new Kernel(3, 3, x1);
Kernel MatrixB = new Kernel(3, 3, y1);
ConvolveOp convolve1 = new ConvolveOp(MatrixA);
ConvolveOp convolve2 = new ConvolveOp(MatrixB);
this.Gx = convolve1.filter(img, null);
this.Gy = convolve2.filter(img, null);
for (int i=0; i<img.getWidth(); i++) {
for (int j=0; j<img.getHeight(); j++) {
double result = G(i,j);
if(result < constant) {
img.setRGB(i,j,Color.black.getRGB());
} else {
img.setRGB(i,j,Color.white.getRGB());
}
}
}
return img;
}
public BufferedImage greyscale(BufferedImage img)
{
//double max = 23777215;
float[] x1 = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
float[] y1 = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
Kernel MatrixA = new Kernel(3, 3, x1);
Kernel MatrixB = new Kernel(3, 3, y1);
ConvolveOp convolve1 = new ConvolveOp(MatrixA);
ConvolveOp convolve2 = new ConvolveOp(MatrixB);
this.Gx = convolve1.filter(img, null);
this.Gy = convolve2.filter(img, null);
for (int i=0; i<img.getWidth(); i++) {
for (int j=0; j<img.getHeight(); j++) {
double result = G(i,j);
//using a floating point to change everything to the right values.
float greyscaleValue = (float)(result/23777215);
greyscaleValue = 1-greyscaleValue;
float red = 255 * greyscaleValue;
float blue = 255 * greyscaleValue;
float green = 255 * greyscaleValue;
Color gray2 = new Color((int)red,(int)green,(int)blue);
img.setRGB(i,j,gray2.getRGB());
}
}
return img;
}
private double G(int x, int y)
{
//the minimum value has to be 0, and the maximum must be 16777215 (hexidecimal of black is 000000 and white is ffffff. I just used the calculator to find it out)
int derp = this.Gx.getRGB(x,y);
int herp = this.Gy.getRGB(x,y);
//maximum possible for result: 23726565. Minimum == 0.
double result = Math.sqrt(Math.pow(derp, 2.0) + Math.pow(herp, 2.0));
return result;
}
public BufferedImage edgeDetect(BufferedImage img)
{
EdgeFinder edgeDetector = new EdgeFinder();
BufferedImage rgbImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
rgbImg.getGraphics().drawImage(img, 0, 0, null);
BufferedImage result = edgeDetector.detect(rgbImg, 21326564);
return result;
}
}
package Grid;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.imageio.ImageIO;
public class GridCreator extends JFrame implements ChangeListener{
private static final long serialVersionUID = 1L;
/**variables*/
public int hd;
public int vd;
private static double height=600;
private static double width=800;
static final int MIN_DIV=0;
static final int MAX_DIV=25; //the maximum is 25. That's a lot
static final int INITIAL_DIV=0;
private Color myColor=Color.WHITE;
private BufferedImage finalImage=null;
private JButton browse;
private JButton color;
private JButton save;
private JLabel img;
private JSlider h_divide;
private JSlider v_divide;
final JFileChooser filec = new JFileChooser();
//constructor
public GridCreator() {
super("Grid Maker"); //title of the application
setLayout(new FlowLayout());
//handling the JSliders
h_divide=new JSlider(JSlider.HORIZONTAL, MIN_DIV, MAX_DIV, INITIAL_DIV);
v_divide=new JSlider(JSlider.HORIZONTAL, MIN_DIV, MAX_DIV, INITIAL_DIV);
h_divide.setMajorTickSpacing(5);
h_divide.setMinorTickSpacing(1);
h_divide.setPaintTicks(true);
h_divide.setPaintLabels(true);
v_divide.setMajorTickSpacing(5);
v_divide.setMinorTickSpacing(1);
v_divide.setPaintTicks(true);
v_divide.setPaintLabels(true);
browse=new JButton("browse");
color=new JButton("Choose Color");
save=new JButton("save");
img=new JLabel();
/** adding to the panel */
add(h_divide);
add(v_divide);
add(color);
add(browse);
add(save);
add(img);
/** action listener */
Actionhandler handler= new Actionhandler();
browse.addActionListener(handler);
color.addActionListener(handler);
save.addActionListener(handler);
h_divide.addChangeListener(this);
v_divide.addChangeListener(this);
}
private class Actionhandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
//browse for file
if(e.getSource()==browse) {
try {
int val = filec.showOpenDialog(GridCreator.this);
/** convert the item to a BufferedImage */
if(val==JFileChooser.APPROVE_OPTION) {
File unfiltered_picture = filec.getSelectedFile();
finalImage = ImageIO.read(unfiltered_picture);
/**if(finalImage.getHeight()>height || finalImage.getWidth()>width) {
BufferedImage anImage = resize(finalImage, getWidth(finalImage), getHeight(finalImage));
ImageIcon imgIcon = new ImageIcon();
imgIcon.setImage(anImage);
img.setIcon(imgIcon);
} else {*/
ImageIcon imgIcon = new ImageIcon();
imgIcon.setImage(finalImage);
img.setIcon(imgIcon);
//}
img.invalidate();
} else {
}
} catch(IOException exception) {
exception.printStackTrace();
}
}
if(e.getSource()==color) {
ColorSelector select = new ColorSelector();
select.main(null);
}
}
}
public double getWidth(BufferedImage img) {
double ratio = Math.min(width / img.getWidth(), height / img.getHeight());
return ratio * width;
}
public double getHeight(BufferedImage img) {
double ratio = Math.min(width / img.getWidth(), height / img.getHeight());
return ratio * height;
}
public BufferedImage resize(BufferedImage origin, double width, double height) {
Image img2 = origin.getScaledInstance((int)width, (int)height, Image.SCALE_AREA_AVERAGING);
BufferedImage img = new BufferedImage((int)width, (int)height, origin.getType());
Graphics2D g = img.createGraphics();
try {
g.drawImage(img2, 0, 0, (int)width, (int)height, null);
} finally {
g.dispose();
}
return img;
}
/** All working! Do not touch! */
public void redrawPic(ChangeEvent e) {
BufferedImage myImg = new BufferedImage(finalImage.getWidth(), finalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = myImg.createGraphics();
g.drawImage(finalImage, 0, 0, null);
drawHorizontalLines(myImg, e);
drawVerticalLines(myImg, e);
ImageIcon imgIcon2 = new ImageIcon();
imgIcon2.setImage(myImg);
img.setIcon(imgIcon2);
img.invalidate();
}
public void drawHorizontalLines(BufferedImage gridImage, ChangeEvent e) {
//horizontal divisions
hd=h_divide.getValue();
if(hd>0) {
int distanceBetweenLines= gridImage.getHeight() / hd;
JSlider source = (JSlider)e.getSource();
if(!source.getValueIsAdjusting()) {
for(int i=0; i<gridImage.getHeight(); i+=distanceBetweenLines) {
for(int j=0; j<gridImage.getWidth(); j++) {
gridImage.setRGB(j,i,myColor.getRGB());
}
}
}
}
}
public void drawVerticalLines(BufferedImage gridImage, ChangeEvent e) {
//vertical divisions
vd=v_divide.getValue();
if(vd>0) {
int distanceBetweenLines= gridImage.getWidth() / vd;
JSlider source = (JSlider)e.getSource();
if(!source.getValueIsAdjusting()) {
for(int i=0; i<gridImage.getWidth(); i+=distanceBetweenLines) {
for(int j=0; j<gridImage.getHeight(); j++) {
gridImage.setRGB(i,j,myColor.getRGB());
}
}
}
}
}
public void stateChanged(ChangeEvent e) {
if(finalImage!=null) {
redrawPic(e);
}
}
public static void createGUI(GridCreator grid) {
grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
grid.setSize((int)width, (int)height);
grid.setVisible(true);
//grid.pack();
}
public static void main(String [] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch( InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch(UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
final GridCreator grid = new GridCreator();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI(grid);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment