Skip to content

Commit d8358b0

Browse files
authored
Add files via upload
1 parent 11c5de4 commit d8358b0

File tree

10 files changed

+257
-0
lines changed

10 files changed

+257
-0
lines changed

imageEditor/bin/GraphicsMain.class

1.39 KB
Binary file not shown.
5.13 KB
Binary file not shown.

imageEditor/bin/MichaelScott.jpg

31.1 KB
Loading

imageEditor/bin/source.jpg

113 KB
Loading

imageEditor/bin/sukuna.jpg

7.22 KB
Loading

imageEditor/src/GraphicsMain.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// GraphicsMain.java
2+
3+
// Written by: Mason Zhu
4+
// Date: May 17, 2024
5+
// Main Class of imageEditor
6+
7+
import java.io.IOException;
8+
9+
import java.util.Scanner;
10+
import javax.swing.JFrame;
11+
import javax.swing.JPanel;
12+
13+
public class GraphicsMain extends JFrame{
14+
15+
public static void main(String[] args) throws IOException
16+
{
17+
18+
Scanner sc = new Scanner(System.in);
19+
System.out.println("Enter file name, do not include any spaces nor .jpg; Example: 'MichaelScott'");
20+
21+
String fileName = sc.nextLine();
22+
GraphicsMain window = new GraphicsMain();
23+
JPanel p = new JPanel();
24+
p.add(new GraphicsPanel(fileName)); // add a class that extends JPanel
25+
window.setTitle("imageEditor");
26+
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27+
28+
window.setContentPane(p);
29+
30+
window.pack();
31+
window.setLocationRelativeTo(null);
32+
window.setVisible(true);
33+
}
34+
35+
}

imageEditor/src/GraphicsPanel.java

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
// Graphics Panel
2+
3+
// Written by: Mason Zhu
4+
// Date: May 17, 2024
5+
// Description: Graphic Panel of imageEditor
6+
import java.awt.Color;
7+
import java.awt.Dimension;
8+
import java.awt.Graphics;
9+
import java.awt.Graphics2D;
10+
import java.awt.event.KeyEvent;
11+
import java.awt.image.BufferedImage;
12+
import java.awt.image.Raster;
13+
import java.awt.image.WritableRaster;
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.util.Scanner;
17+
18+
import javax.imageio.ImageIO;
19+
import javax.swing.ImageIcon;
20+
import javax.swing.JPanel;
21+
import java.awt.event.KeyEvent;
22+
import java.awt.event.KeyListener;
23+
24+
public class GraphicsPanel extends JPanel implements KeyListener{
25+
26+
private BufferedImage image;
27+
private BufferedImage modifiedImage;
28+
private String imageName;
29+
30+
final int PIX_SIZE = 10;
31+
32+
33+
// method: GraphicsPanel Constructor
34+
// description: This 'method' runs when a new instance of this class in instantiated. It sets default values
35+
// that are necessary to run this project. You do not need to edit this method.
36+
public GraphicsPanel(String imageName) throws IOException{
37+
this.imageName = imageName;
38+
39+
//establish File
40+
File file = new File("src/"+imageName+".jpg"); //enter your picture here
41+
42+
//read into BufferedImage
43+
image = ImageIO.read(file);
44+
setPreferredSize(new Dimension(image.getWidth() * 2, image.getHeight()));
45+
46+
modifiedImage = ImageIO.read(file);
47+
48+
//investigate how pixels are represented numerically
49+
System.out.println(image.getRGB(50,50)); //prints the decimal representation of the pixel at position 50,50
50+
System.out.println(Integer.toHexString(image.getRGB(50,50))); //prints the hexadecimal representation of the pixel at position 50,50
51+
System.out.printf("%x \n",image.getRGB(50,50)); //another way to print hexadecimal represenation
52+
53+
54+
// modifyPicture();
55+
56+
//write to file
57+
ImageIO.write(image, "jpg",new File("new.jpg"));
58+
this.setFocusable(true); // for keylistener
59+
this.addKeyListener(this);
60+
61+
}
62+
63+
public void modifyPicture() {
64+
for(int i = 0; i < modifiedImage.getHeight(); i++)
65+
for(int j = 0; j < modifiedImage.getWidth(); j++)
66+
if (modifiedImage.getRGB(j, i) < 20) {
67+
modifiedImage.setRGB(j, i, ~modifiedImage.getRGB(j, i) + 200);
68+
}
69+
else {
70+
modifiedImage.setRGB(j, i, modifiedImage.getRGB(j, i) + 10);
71+
}
72+
// this line set RGB
73+
}
74+
75+
public void invertImage() {
76+
for(int i = 0; i < modifiedImage.getHeight(); i++)
77+
for(int j = 0; j < modifiedImage.getWidth(); j++)
78+
modifiedImage.setRGB(j, i, ~modifiedImage.getRGB(j, i));
79+
// this line set RGB
80+
}
81+
82+
public void makeGray() {
83+
for(int i = 0; i < modifiedImage.getHeight(); i++) {
84+
for(int j = 0; j < modifiedImage.getWidth(); j++) {
85+
Color c = new Color(modifiedImage.getRGB(j, i));
86+
System.out.println(modifiedImage.getRGB(j, i) + "");
87+
int green = c.getGreen();
88+
int red = c.getRed();
89+
int blue = c.getBlue();
90+
int gray = (green + blue + red)/3;
91+
Color g = new Color( gray, gray, gray);
92+
modifiedImage.setRGB(j, i, g.getRGB());
93+
}
94+
}
95+
}
96+
97+
public void invertRed() {
98+
for(int i = 0; i < modifiedImage.getHeight(); i++) {
99+
for(int j = 0; j < modifiedImage.getWidth(); j++) {
100+
Color c = new Color(image.getRGB(j, i));
101+
int green = c.getGreen();
102+
int red = c.getRed();
103+
int blue = c.getBlue();
104+
int rgb = ((green << 8) + blue + (~red<<16));
105+
106+
modifiedImage.setRGB(j, i, rgb);
107+
}
108+
}
109+
}
110+
public void makePixelated(int pixelSize) { // Use pixelSize for clarity
111+
112+
for (int i = 0; i < modifiedImage.getHeight(); i += pixelSize) {
113+
for (int j = 0; j < modifiedImage.getWidth(); j += pixelSize) {
114+
Color c = new Color(image.getRGB(j, i));
115+
116+
117+
int red = 0;
118+
int green = 0;
119+
int blue = 0;
120+
121+
for (int a = i; a < i + pixelSize && a < modifiedImage.getHeight(); a++) {
122+
for (int b = j; b < j + pixelSize && b < modifiedImage.getWidth(); b++) {
123+
Color innerColor = new Color(image.getRGB(b, a));
124+
red += innerColor.getRed();
125+
green += innerColor.getGreen();
126+
blue += innerColor.getBlue();
127+
}
128+
}
129+
130+
// calculate average rgb stuff
131+
int numPixels = Math.min(pixelSize * pixelSize, (modifiedImage.getWidth() - j) * (modifiedImage.getHeight() - i));
132+
red /= numPixels;
133+
green /= numPixels;
134+
blue /= numPixels;
135+
136+
// set everything in to that color
137+
for (int a = i; a < i + pixelSize && a < modifiedImage.getHeight(); a++) {
138+
for (int b = j; b < j + pixelSize && b < modifiedImage.getWidth(); b++) {
139+
modifiedImage.setRGB(b, a, new Color(red, green, blue).getRGB());
140+
}
141+
}
142+
}
143+
}
144+
}
145+
146+
public void reset() throws IOException {
147+
File file = new File("src/"+imageName+".jpg");
148+
modifiedImage = ImageIO.read(file);
149+
150+
}
151+
152+
153+
public void keyPressed(KeyEvent e) {
154+
if (e.getKeyCode() == KeyEvent.VK_1) { // invert red
155+
invertRed();
156+
repaint();
157+
}
158+
159+
else if(e.getKeyCode() == KeyEvent.VK_2) { // monochrome
160+
makeGray();
161+
repaint();
162+
}
163+
164+
else if (e.getKeyCode() == KeyEvent.VK_3) { // invert image
165+
invertImage();
166+
repaint();
167+
}
168+
169+
else if (e.getKeyCode() == KeyEvent.VK_4) { // pixelated
170+
makePixelated(10);
171+
repaint();
172+
}
173+
174+
else if (e.getKeyCode() == KeyEvent.VK_0) { // reset
175+
try {
176+
reset();
177+
} catch (IOException e1) {
178+
// TODO Auto-generated catch block
179+
e1.printStackTrace();
180+
}
181+
repaint();
182+
}
183+
}
184+
185+
186+
187+
188+
189+
190+
191+
192+
193+
194+
195+
196+
// method: paintComponent
197+
// description: This method is called when the Panel is painted. It contains code that draws shapes onto the panel.
198+
// parameters: Graphics g - this object is used to draw shapes onto the JPanel.
199+
// return: void
200+
public void paintComponent(Graphics g){
201+
Graphics2D g2 = (Graphics2D) g;
202+
203+
//ImageIcon imageIcon = new ImageIcon(image);
204+
205+
new ImageIcon(image).paintIcon(this, g2, 0, 0);
206+
new ImageIcon(modifiedImage).paintIcon(this, g2, image.getWidth() + 1, 0);
207+
208+
}
209+
210+
@Override
211+
public void keyTyped(KeyEvent e) {
212+
// TODO Auto-generated method stub
213+
214+
}
215+
216+
@Override
217+
public void keyReleased(KeyEvent e) {
218+
// TODO Auto-generated method stub
219+
220+
}
221+
222+
}

imageEditor/src/MichaelScott.jpg

31.1 KB
Loading

imageEditor/src/source.jpg

113 KB
Loading

imageEditor/src/sukuna.jpg

7.22 KB
Loading

0 commit comments

Comments
 (0)