-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawArray2D.java
More file actions
105 lines (95 loc) · 3.12 KB
/
Copy pathDrawArray2D.java
File metadata and controls
105 lines (95 loc) · 3.12 KB
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
99
100
101
102
103
104
105
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JComponent;
/**
* This program repeatedly draws the image specified in a URL,
* calling Array2dDraw.shiftArray() before each redraw.
*
* @author {user}
* Course: COMP B11
* Created: Mar 25, 2017
* Source File: DrawArray2D.java
*/
public class DrawArray2D {
static int [][] image = null;
/**
* Initialize image, using input as source
* @param input a URL that specifies that source of the image
*/
public static void initializeImage(String input) {
BufferedImage imageIn;
try {
URL url = new URL(input);
imageIn = ImageIO.read(url);
int width = imageIn.getWidth();
int height = imageIn.getHeight();
image = new int[height][width];
System.out.printf("width=%d height=%d\n", width, height);
for (int row = 0; row < image.length; row++) {
for (int col = 0; col < image[row].length; col++) {
image[row][col] = imageIn.getRGB(col, row);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Draws image
* @param g Graphics object
*/
public static void draw(Graphics g) {
int width = image[0].length;
int height = image.length;
BufferedImage bufferedImage =
new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int row = 0; row < height; row++) {
bufferedImage.setRGB(0, row, image[row].length, 1, image[row], 0, width);
// for (int col = 1; col < image[row].length; col++) {
// bufferedImage.setRGB(col, row, image[row][col]);
// }
}
g.drawImage(bufferedImage, 0, 0, null);
}
public static void main(String[] args) {
String url;
if (args.length > 0)
url = args[0];
else
url = "http://cassavafilms.com/wp-content/uploads/2013/02/dinosaur.jpg";
if (image == null) {
initializeImage(url);
}
JFrame frame = new JFrame();
int width = image[0].length;
int height = image.length;
frame.setSize(width, height);
frame.setResizable(false);
frame.setTitle("Hal Mendoza"); // Change to your name
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent component = new JComponent() {
private static final long serialVersionUID = 5384931283626878754L;
public void paintComponent(Graphics graph) {
draw(graph);
}
};
frame.add(component);
frame.setVisible(true);
for (int i = 0; i < 10000; ++i) {
Array2dShift.shiftArray(image);
frame.repaint();
try {
Thread.sleep(12);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}