-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageTest.java
64 lines (52 loc) · 1.5 KB
/
ImageTest.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
import java.awt.*;
import javax.swing.*;
public class ImageTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() ->
{
JFrame frame = new ImageFrame();
frame.setTitle("Bruce dice test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
/**
* A frame with an image component
*/
class ImageFrame extends JFrame
{
public ImageFrame()
{
add(new ImageComponent());
pack();
}
}
/**
* A component that displays a tiled image
*/
class ImageComponent extends JComponent
{
private static final int DEFAULT_WIDTH = 1100;
private static final int DEFAULT_HEIGHT = 500;
private Image image1, image2, image3;
public ImageComponent()
{
image1 = new ImageIcon("3up.jpg").getImage();
image2 = new ImageIcon("4up.jpg").getImage();
image3 = new ImageIcon("5up.jpg").getImage();
}
public void paintComponent(Graphics g)
{
if (image1 == null) return;
int image1Width = image1.getWidth(null);
int image1Height = image1.getHeight(null);
// draw the image in the upper-left corner
g.drawImage(image1, 50, 100, null);
g.drawImage(image2, 70 + image1Width, 100, null);
g.drawImage(image3, 90 + image1Width * 2, 100, null);
}
public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
}