Skip to content

Instantly share code, notes, and snippets.

@AlexFrazer
Created January 9, 2013 17:44
Show Gist options
  • Save AlexFrazer/4495162 to your computer and use it in GitHub Desktop.
Save AlexFrazer/4495162 to your computer and use it in GitHub Desktop.
package Grid;
/**
* for artists, makes a super easy grid to follow.
* @Alex "Crowz" Frazer
*/
import java.awt.image.BufferedImage;
import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.imageio.ImageIO;
public class GridFriend extends JFrame
{
/*variables*/
private double h_division=0;
private double v_division=0;
private BufferedImage finalImage=null;
private ImageIcon img;
private JButton make;
private JButton browse;
private JButton color;
private JButton grid;
private JTextField filename;
private JTextField h_divide;
private JTextField v_divide;
final JFileChooser filec = new JFileChooser();
//constructor
public GridFriend() {
super("Grid Maker"); //title of the application
setLayout(new FlowLayout());
/** instantiating the variables */
h_divide=new JTextField("enter horizontal divisions");
v_divide=new JTextField("enter vertical divisions");
browse=new JButton("browse");
color=new JButton("Choose Color");
filename= new JTextField("file name");
make=new JButton("make");
//output=new JTextField("picture height: " + height + " " +"picture width: "+width+ " "+"Horizontal divisions: " +h_division+" "+"Vertical Divisions: "+v_division);
/** adding to the panel */
add(h_divide);
add(v_divide);
add(filename);
add(color);
add(browse);
add(make);
//add(output);
/** action listener */
Actionhandler handler= new Actionhandler();
browse.addActionListener(handler);
color.addActionListener(handler);
}
private class Actionhandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
//browse for file
if(e.getSource()==browse) {
try {
int val = filec.showOpenDialog(GridFriend.this);
/** convert the item to a BufferedImage */
if(val==JFileChooser.APPROVE_OPTION) {
File unfiltered_picture = filec.getSelectedFile();
finalImage = ImageIO.read(unfiltered_picture);
Graphics g = getGraphics();
g.drawImage(finalImage,0,150,null);
} else {
}
} catch(IOException exception) {
exception.printStackTrace();
}
}
//open the color browser to change the lines
if(e.getSource()==color) {
}
if(e.getSource()==make) {
/** write the file to the specified directory */
try {
ImageIO.write(finalImage, "jpg", new File("C:\\" + filename.getText() + ".jpg"));
} catch (IOException exception) {
exception.printStackTrace();
}
}
if(e.getSource()==h_divide) {
double h=Double.valueOf(h_divide.getText());
h_division=finalImage.getWidth()/h;
}
if(e.getSource()==v_divide) {
double v=Double.valueOf(v_divide.getText());
v_division=finalImage.getHeight()/v;
}
if(e.getSource()==grid) {
BufferedImage modImage = finalImage; //creating a copy to display to the screen
if(v_division!=0 && h_division!=0) {
for(int i=0; i<finalImage.getWidth(); i++) {
for(int j=0; j<finalImage.getHeight(); j++) {
if(j%v_division==0) {
//modImage.setRGB(i,j,);
}
}
}
for(int i=0; i<finalImage.getHeight(); i++) {
for(int j=0; j<finalImage.getWidth(); j++) {
if(j%v_division==0) {
//finalImage.setRGB();
}
}
}
}
Graphics g=getGraphics();
g.drawImage(modImage,0,150,null);
}
}
}
/**public BufferedImage grid(BufferedImage img) {
for(int i=0; i<height; i++) {
}
for(int j=0; j<width; j++) {
}
}*/
}
package Grid;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class GUI
{
public static void main(String [] args) {
GridFriend grid = new GridFriend();
grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
grid.setSize(700, 1000);
grid.setVisible(true);
}
}
package Grid;
/**
* An imagereader that doesn't look at all like the professors + an edge detector. >.>
* (in my defense, it's mostly reading the API).
* I will use the Sobel's Operator, because I think it's the prettiest and I'm an annoying, pretentious, artist.
* Sobel's operator works by using a 3x3 matrix kinda operation to find the abruptness of change in rgb values.
* Therefore, my strategy will be to use a for loop to iterate through all the pixels and use this operator on all of them.
* Hopefully it works out.
*/
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
public class ImageReader
{
private BufferedImage Gx, Gy;
public BufferedImage detect(BufferedImage img)
{
double constant=21326564.99;
// My matrices for the multiplication.
float[] x1 = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
float[] y1 = {-1,-2,-1,0,0,0,1,2,1};
//2x2 matrix, with those two float arrays.
Kernel MatrixA = new Kernel(3, 3, x1);
Kernel MatrixB = new Kernel(3, 3, y1);
// Convolving the matrices. I hate math.
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.white.getRGB());
} else {
img.setRGB(i,j,Color.black.getRGB());
}
}
}
return img;
}
public BufferedImage greyscale(BufferedImage img)
{
//maximum number of colors.
double max = 23777215;
// My matrices for the multiplication.
float[] x1 = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
float[] y1 = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
//2x2 matrix, with those two float arrays.
Kernel MatrixA = new Kernel(3, 3, x1);
Kernel MatrixB = new Kernel(3, 3, y1);
// Convolving the matrices. I hate math.
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;
// System.out.println("Result: " + result + " max double: " + max + " Grayscale value: " + greyscaleValue);
// System.out.println("Gray -- R: " + Color.gray.getRed() + " G: " + Color.gray.getGreen() + " B: " + Color.gray.getBlue() );
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;
}
//formula for making things work.
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;
}
//I'm starting to get a bit insulted by this "artistic expression". What is that
public static void main(String args[])
{
File file = new File(arg0);
BufferedImage img = null;
try
{
ImageReader edgeDetector = new ImageReader();
img = ImageIO.read(file);
//the colorspace for the file is wrong and will not pass to convolve. Thus this stupid line of code.
BufferedImage rgbImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
rgbImg.getGraphics().drawImage(img, 0, 0, null);
BufferedImage result = edgeDetector.detect(rgbImg);
//greyscale
BufferedImage greyImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
greyImg.getGraphics().drawImage(img, 0, 0, null);
BufferedImage result2 = edgeDetector.greyscale(greyImg);
//writing
ImageIO.write(result, "jpg", new File("C:\\Test\\boxxy2.jpg"));
ImageIO.write(result2, "jpg", new File("C:\\Test\\boxxy3.jpg"));
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment