Created
October 29, 2018 11:46
-
-
Save feehe21/67b5a63dbe34c649206457ebe92910db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.WindowConstants; | |
import java.awt.Dimension; | |
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Point; | |
import java.awt.event.MouseListener; | |
import java.awt.event.MouseEvent; | |
import javax.imageio.*; | |
import java.awt.image.*; | |
import java.io.*; | |
import java.util.*; | |
import java.awt.Font; | |
import java.awt.FontMetrics; | |
public class Photoshop { | |
private JFrame frame; | |
public Photoshop() { | |
frame = new JFrame("Photoshop Filter"); | |
frame.setSize(620, 400); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
frame.setPreferredSize(frame.getSize()); | |
frame.add(new PhotoDraw(frame.getSize())); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
public static void main(String... argv) { | |
new Photoshop(); | |
} | |
public static class PhotoDraw extends JPanel implements MouseListener { | |
//int[][] alpha ; | |
int[][] red; | |
int[][] green ; | |
int[][] blue ; | |
BufferedImage img; | |
BufferedImage img2; | |
public PhotoDraw(Dimension dimension) {//constructor | |
setSize(dimension); | |
setPreferredSize(dimension); | |
addMouseListener(this); | |
try { | |
img = ImageIO.read(this.getClass().getResource("img.jpg")); | |
img2 = ImageIO.read(this.getClass().getResource("img.jpg")); | |
} catch (IOException e) { | |
System.out.println("Image could not be read"); | |
System.exit(1); | |
} | |
int width = img.getWidth(); | |
int height = img.getHeight(); | |
// System.out.println(width + " | " + height); | |
setArrays(width, height); | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
Graphics2D g2 = (Graphics2D)g; | |
Dimension d = getSize(); | |
g2.drawImage(img,0,0,300,150 ,null); | |
g2.drawImage(img2,310,0,300,150 ,null); | |
g2.setColor(Color.black); | |
g2.fillRect(10,200,100,100); | |
g2.fillRect(120,200,100,100); | |
g2.fillRect(230,200,100,100); | |
g2.fillRect(340,200,100,100); | |
g2.setColor(Color.red); | |
g2.setFont (new Font("TimesRoman", Font.PLAIN, 20)); | |
g2.drawString("Red" , 15,250); | |
g2.drawString("Threashold" , 125,250); | |
g2.drawString("Inverted" , 235,250); | |
g2.drawString("Flip" , 345,250); | |
} | |
public void setArrays(int width, int height){ | |
red = new int[width][height]; | |
green = new int[width][height]; | |
blue = new int[width][height]; | |
for(int x=0; x< width; x++){ | |
for (int y=0;y< height;y++){ | |
Color mycolor = new Color(img.getRGB(x, y)); | |
int r = mycolor.getRed(); | |
int g = mycolor.getGreen(); | |
int b = mycolor.getBlue(); | |
/* */ | |
red[x][y] = r; | |
green[x][y] = g; | |
blue[x][y] = b; | |
}} | |
} | |
public void mousePressed(MouseEvent e) { | |
int x = e.getX(); | |
int y = e.getY(); | |
if(x > 10 && x < 110 && y > 200 && y < 300){ | |
imageChange("red"); | |
}else if(x > 120 && x < 220 && y > 200 && y < 300){ | |
imageChange("threashold"); | |
}else if(x > 230 && x < 330 && y > 200 && y < 300){ | |
imageChange("inverted"); | |
}else if(x > 340 && x < 440 && y > 200 && y < 300){ | |
imageChange("flip"); | |
} | |
repaint(); | |
} | |
public boolean checkForWinner(int x,int y, Color c){ | |
return false; | |
} | |
public void mouseReleased(MouseEvent e) { | |
} | |
public void mouseEntered(MouseEvent e) { | |
} | |
public void mouseExited(MouseEvent e) { | |
} | |
public void mouseClicked(MouseEvent e) { | |
} | |
//this creates a new image | |
public void imageChange(String filter) | |
{ | |
int save = 0; | |
//make some changes to the pixels | |
for(int x=0; x< red.length-1; x++){ | |
for (int y=0;y < red[0].length-1;y++){ | |
//luminance of the pixel (0.2126*r) + (0.7152*g) + (0.0722*b); | |
double lum =(0.2126*red[x][y]) + (0.7152*green[x][y]) + (0.0722 * blue[x][y]); | |
if(filter.equals("red")){ | |
red[x][y] = 255; | |
} | |
if(filter.equals("threashold")){ | |
if(red[x][y] > 255/2){ | |
red[x][y] = 255; | |
}else{ | |
red[x][y] = 0; | |
} | |
if(blue[x][y] > 255/2){ | |
blue[x][y] = 255; | |
}else{ | |
blue[x][y] = 0; | |
} | |
if(green[x][y] > 255/2){ | |
green[x][y] = 255; | |
}else{ | |
green[x][y] = 0; | |
} | |
} | |
if(filter.equals("inverted")){ | |
red[x][y] = 255 - red[x][y]; | |
blue[x][y] = 255 - blue[x][y]; | |
green[x][y] = 255 - green[x][y]; | |
} | |
if(filter.equals("flip") && y <= (red[0].length - 1)/2){ | |
save = red[x][y]; | |
red[x][y] = red[x][red[0].length - 1 - y]; | |
red[x][red[0].length - 1 - y] = save; | |
save = blue[x][y]; | |
blue[x][y] = blue[x][blue[0].length - 1 - y]; | |
blue[x][blue[0].length - 1 - y] = save; | |
save = green[x][y]; | |
green[x][y] = green[x][green[0].length - 1 - y]; | |
green[x][green[0].length - 1 - y] = save; | |
} | |
}} | |
//make changes above | |
//create new image using new values | |
BufferedImage img3 = new BufferedImage(red.length, red[0].length, BufferedImage.TYPE_INT_RGB); | |
for (int x = 0; x< red.length; x++){ | |
for (int y = 0; y< red[0].length; y++){ | |
int r = red[x][y]; | |
int g = green[x][y]; | |
int b = blue[x][y]; | |
int col = (r << 16) | (g << 8) | b; | |
//255, 255, 255 | |
img3.setRGB(x, y, col); | |
}} | |
String fullName = "inverted"; | |
File f = new File(fullName + ".jpg"); | |
try{ | |
ImageIO.write(img3, "JPEG", f); | |
img2 = img3; | |
repaint(); | |
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& | |
// iImgName = "inverted.jpg"; | |
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& | |
} catch(Exception e){ | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment