-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageDisplay.java
executable file
·98 lines (79 loc) · 2.65 KB
/
ImageDisplay.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
class ImageDisplay {
JFrame frame;
JLabel lbIm1;
JLabel lbIm2;
BufferedImage img;
int width = 512;
int height = 512;
// Draws a black line on the given buffered image from the pixel defined by (x1, y1) to (x2, y2)
public void drawLine(BufferedImage image, int x1, int y1, int x2, int y2) {
Graphics2D g = image.createGraphics();
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(1));
g.drawLine(x1, y1, x2, y2);
g.drawImage(image, 0, 0, null);
}
public void showImg(String[] args) {
// Read a parameter from command line
String param0 = args[0];
System.out.println("The first parameter was: " + param0);
// Initialize a plain white image
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
// byte a = (byte) 255;
byte r = (byte) 255;
byte g = (byte) 255;
byte b = (byte) 255;
int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
// int pix = ((a << 24) + (r << 16) + (g << 8) + b);
img.setRGB(x, y, pix);
}
}
drawLine(img, 0, 0, width-1, 0); // top edge
drawLine(img, 0, 0, 0, height-1); // left edge
drawLine(img, 0, height-1, width-1, height-1); // bottom edge
drawLine(img, width-1, height-1, width-1, 0); // right edge
drawLine(img, 0, 0, width-1, height-1); // diagonal line
// Use labels to display the images
frame = new JFrame();
GridBagLayout gLayout = new GridBagLayout();
frame.getContentPane().setLayout(gLayout);
JLabel lbText1 = new JLabel("Original image (Left)");
lbText1.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lbText2 = new JLabel("Image after modification (Right)");
lbText2.setHorizontalAlignment(SwingConstants.CENTER);
lbIm1 = new JLabel(new ImageIcon(img));
lbIm2 = new JLabel(new ImageIcon(img));
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
frame.getContentPane().add(lbText1, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
frame.getContentPane().add(lbText2, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
frame.getContentPane().add(lbIm1, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
frame.getContentPane().add(lbIm2, c);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
ImageDisplay img = new ImageDisplay();
img.showImg(args);
}
}