Created
October 30, 2018 22:15
-
-
Save feehe21/a30c41221ccefe67547ee945056e2cde 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; | |
import java.awt.*; | |
public class Puzzle { | |
private JFrame frame; | |
public Puzzle() { | |
frame = new JFrame("Photoshop Filter"); | |
frame.setSize(620, 400); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
frame.setPreferredSize(frame.getSize()); | |
frame.add(new PhotoPuz(frame.getSize())); | |
frame.pack(); | |
frame.setVisible(true); | |
System.out.println("Can you guess who it is?"); | |
} | |
public static void main(String... argv) { | |
new Puzzle(); | |
} | |
public static class PhotoPuz extends JPanel implements MouseListener { | |
int[][] red; | |
int[][] green ; | |
int[][] blue ; | |
//Amount of Rows and Columns | |
int pS = 5; | |
//Array of images | |
BufferedImage[][] puz = new BufferedImage[pS][pS]; | |
//Reveal original image | |
boolean showFace = false; | |
BufferedImage img; | |
BufferedImage img2; | |
//width and height of the image | |
int width = 0; | |
int height= 0; | |
int firstX = -1; | |
int firstY = -1; | |
int[ ] spot = new int[25]; | |
public PhotoPuz(Dimension dimension) {//constructor | |
setSize(dimension); | |
setPreferredSize(dimension); | |
addMouseListener(this); | |
try { | |
img = ImageIO.read(this.getClass().getResource("face.jpg")); | |
} catch (IOException e) { | |
System.out.println("Image could not be read"); | |
System.exit(1); | |
} | |
width = img.getWidth(); | |
height = img.getHeight(); | |
//get RGB of original image | |
setArrays(width, height); | |
//setup the puz array | |
setImgArray(width, height); | |
} | |
public void setImgArray(int w, int h){ | |
int s = w/pS;//width / 5 | |
int xStart = 0; | |
int yStart = 0; | |
//complete the method | |
/*1) | |
* you will want to make use | |
* of the variables above and call below | |
* What else will you need? | |
*/ | |
img2 = imageChange(xStart, yStart, s,img ); | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
Graphics2D g2 = (Graphics2D)g; | |
Dimension d = getSize(); | |
if(showFace){ | |
g2.drawImage(img,300,0,210,210 ,null); | |
} | |
g2.setColor(Color.black); | |
g2.fillRect(10,250,100,50); | |
g2.setColor(Color.red); | |
g2.setFont (new Font("TimesRoman", Font.PLAIN, 20)); | |
g2.drawString("Hide" , 15,275); | |
int s = width/pS; | |
int yspace = 0; | |
int xspace = 0; | |
/*3) | |
* You will want to display the puzzle here | |
*/ | |
//g2.drawImage(puz[x][y],x ,y,s,s ,null); | |
//(x+1)*(s), width*spot[spotNumber]/5 | |
//spot[spotNumber] = corresponds to which number spot it should be placed | |
int spotNumber = 0; | |
for(int x = 0; x < 5; x++){ | |
for(int y = 0; y < 5; y++){ | |
spotNumber = x + y * 5; | |
g2.drawImage(puz[y][x], (spot[spotNumber]/5)*s, (spot[spotNumber]%5)*s, width/5, height/5, null); | |
//width*rand/5; height*rand/5, height*spot[spotNumber]/5 | |
} | |
} | |
//g2.drawImage(img); | |
} | |
public void setArrays(int width, int height){ | |
red = new int[width][height]; | |
green = new int[width][height]; | |
blue = new int[width][height]; | |
int pCount=0; | |
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(); | |
int xPlace = x/(width/5); | |
int yPlace = y/(width/5); | |
showFace=true; | |
/*if(x <= width && y <= height){ | |
swap(xPlace,yPlace); | |
}*///g2.fillRect(10,250,100,50); | |
if(x >= 10 && x <= 110 && y >= 250 && y <= 300){ | |
showFace = false; | |
} | |
repaint(); | |
} | |
/*public void swap(int x, int y){ | |
if(firstX == -1){ | |
firstX = x; | |
firstY = y; | |
}else{ | |
int save = spot[firstX]; | |
spot[firstX] = spot[x]; | |
spot[x] = save; | |
save = spot[firstY]; | |
spot[firstY] = spot[y]; | |
spot[y] = save; | |
} | |
}*/ | |
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 BufferedImage imageChange(int xStart, int yStart, int len,BufferedImage img4 ) | |
{ | |
//len = width / 5 | |
//make changes below | |
//System.out.println(xStart); | |
//create new image using new values | |
BufferedImage img3 = new BufferedImage(len, len, BufferedImage.TYPE_INT_RGB); | |
int tx = 0; | |
int ty = 0; | |
for(int x = 0; x < 5; x++){ | |
for(int y = 0; y < 5; y++){ | |
puz[y][x] = img.getSubimage(tx, ty, len, len); | |
tx += len; | |
} | |
tx = 0; | |
ty += len; | |
} | |
//2) tweak for loop below | |
//the actual headers need to by updated | |
//make use of tx and ty variables? | |
int rand; | |
int max = 24; | |
int min = 0; | |
ArrayList<Integer> blocksNotUsed = new ArrayList<Integer>(); | |
for(int i = 0; i < 25; i++){ | |
blocksNotUsed.add(i); | |
} | |
int count = 0;; | |
for(int x = 0; x < 5; x++){ | |
for(int y = 0; y < 5; y++){ | |
rand = (int)(Math.random() * ((max - min) )) + min; | |
max --; | |
spot[count] = blocksNotUsed.remove(rand); | |
count++; | |
//puz[y][x] = | |
//width*rand/5; height*rand/5 | |
} | |
} | |
for (int x = xStart; x< len; x++){ | |
for (int y = yStart; y< len; y++){ | |
int r = red[x][y]; | |
int g = green[x][y]; | |
int b = blue[x][y]; | |
int col = (r << 16) | (g << 8) | b; | |
//want to tweak line below | |
img3.setRGB(x, y, col); | |
} | |
} | |
return img3; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment