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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import org.jdesktop.swingx.JXImagePanel;
/**
*
* @author Fabrice
*/
public class Main extends JComponent {
public enum Variant {
SWING,
SWING_X;
}
private Variant variant;
public Main(Variant variant) throws IOException {
variant = (variant == null) ? Variant.SWING : variant;
this.variant = variant;
JEditorPane jEditorPane = new JEditorPane();
jEditorPane.setForeground(Color.WHITE);
jEditorPane.setOpaque(false);
StringBuilder text = new StringBuilder();
for (int i = 0; i < 30; i++) {
text.append("BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla\n");
text.append("BlaBlaBlaBlaBlaBlaBlaBlaBlaBla\n");
text.append("\n");
}
jEditorPane.setText(text.toString());
jEditorPane.setCaretPosition(0);
JScrollPane scrollPane = new JScrollPane(jEditorPane);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
JComponent container = null;
final BufferedImage image = ImageIO.read(new File("C:\\Users\\Fabrice\\Pictures\\DAOrigins 2010-03-22 21-44-27-45.jpg"));
switch (variant) {
case SWING_X: {
JXImagePanel imagePanel = new JXImagePanel();
imagePanel.setImage(image);
container = imagePanel;
}
break;
case SWING:
default: {
JComponent panel = new JComponent() {
/**
* {@inheritDoc}
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
};
panel.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
container = panel;
}
}
container.setLayout(new BorderLayout());
container.add(scrollPane, BorderLayout.CENTER);
setLayout(new BorderLayout());
add(container, BorderLayout.CENTER);
}
/**
* {@inheritDoc}
*/
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Dimension size = getSize();
String label = variant.toString();
Font font = new Font("Dialog", Font.BOLD, 70);
Rectangle2D stringBounds = font.getStringBounds(label, g2d.getFontRenderContext());
LineMetrics lineMetrics = font.getLineMetrics(label, g2d.getFontRenderContext());
g2d.setColor(Color.RED);
g2d.setFont(font);
g2d.drawString(label, (int) ((size.width - stringBounds.getWidth()) / 2.0), (int) (lineMetrics.getAscent() + (size.height - stringBounds.getHeight()) / 2.0));
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
try {
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new Main(Variant.SWING), new Main(Variant.SWING_X));
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1300, 1000);
f.setLayout(new BorderLayout());
f.add(splitPane);
f.setLocationRelativeTo(null);
f.setVisible(true);
splitPane.setDividerLocation(0.5);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
});
}
} |
Partager