Java Reference
In-Depth Information
File:
its/Images/ImagePanel.java
1.
package
its.Images;
2.
3.
import
java.awt.*;
4.
import
javax.swing.JPanel;
5.
import
java.util.Vector;
6.
7.
public class
ImagePanel
extends
JPanel
8.
{
9.
10.
private
Vector images;
11.
private int
gap = 10;
12.
private
MediaTracker mediTracker;
13.
private int
imageID;
14.
15.
public
ImagePanel()
16.
{
17.
this
.setLayout(
new
BorderLayout());
18.
this
.setBackground(Color.yellow);
19.
images =
new
Vector();
20.
mediTracker =
new
MediaTracker(
this
);
21.
imageID = 0;
22.
this.
setPreferredSize(
new
Dimension(1000,1400));
23.
}
24.
25.
public void
paintComponent(Graphics g)
26.
{
27.
super.
paintComponent(g);
28.
int
currentYPosition = gap;
29.
int
currentXPosition = gap;
30.
int
imageWidth, imageHeight;
31.
int
maxHeight = -1;
32.
int
panelWidth =
this
.getWidth();
33.
for (int
i=0;i<images.size(); i++) {
34.
Image currentImage = (Image)(images.get(i));
35.
imageWidth = currentImage.getWidth(
this
);
36.
imageHeight = currentImage.getHeight(
this
);
37.
//
Check whether to start a new row
.
38.
if
((gap+imageWidth+currentXPosition) > panelWidth)
39.
{
40.
currentYPosition += maxHeight + gap;
41.
maxHeight = -1;
42.
currentXPosition = gap;
43.
}
44.
if
(imageHeight > maxHeight)
Search WWH ::
Custom Search