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
|
package test;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.imageio.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class TestInterface {
private static final String[] DIRECTIONS = {BorderLayout.NORTH, BorderLayout.WEST, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.CENTER};
private static final Color[] COLORS = {Color.GREEN, Color.ORANGE, Color.BLUE, Color.CYAN, Color.RED};
public static void main(String ...args) {
try {
BufferedImage image = ImageIO.read(new File("test.jpg"));
for (int j = 0; j < 2; j++) {
boolean opaque = (j % 2 != 0);
JFrame frame = new JFrame();
frame.setTitle(String.valueOf(j) + " " + opaque);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImagePanel imagePanel = new ImagePanel(image);
frame.setContentPane(imagePanel);
frame.setLayout(new BorderLayout());
for (int i = 0; i < DIRECTIONS.length; i++) {
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(Color.WHITE, 2));
panel.setBackground(COLORS[i]);
panel.setOpaque(opaque);
frame.add(panel, DIRECTIONS[i]);
}
frame.setSize(400, 400);
frame.setVisible(true);
}
} catch (Throwable t) {
t.printStackTrace();
}
}
private static class ImagePanel extends JPanel {
private Image image = null;
public ImagePanel(Image image) {
this.image = image;
}
/** {@inheritDoc}
*/
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
if (image != null) {
Insets insets = getInsets();
Dimension size = getSize();
int width = size.width - (insets.left + insets.right);
int height = size.height - (insets.top + insets.bottom);
Graphics2D g2d = (Graphics2D) graphics.create(insets.left, insets.top, width, height);
try {
g2d.drawImage(image, 0, 0, width, height, null);
}
finally {
g2d.dispose();
}
}
}
}
} |
Partager