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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.LinearGradientPaint;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.lang.ref.WeakReference;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
/**
* @param T The type of the targeted component.
* @author fabriceb
*/
public class WaitingPane<T extends Component> extends JComponent implements ComponentListener {
private static final long serialVersionUID = 1L;
private static final Color DEFAULT_BACKGROUND = new Color(0, 0, 0, 128);
private BufferedImage image;
private Stroke stroke = new BasicStroke(2f);
private RoundRectangle2D progressBar = new RoundRectangle2D.Float();
private LinearGradientPaint gradient;
private WeakReference<T> target;
/** Creates a new instance.
* @param component The target component.
*/
public WaitingPane(T component) {
target = new WeakReference<T>(component);
addComponentListener(this);
setBackground(DEFAULT_BACKGROUND);
setForeground(Color.WHITE);
}
/** Recalculate size and stuff; recreate images.
*/
private void recalculate() {
Dimension size = getSize();
Insets insets = getInsets();
int width = size.width - (insets.left + insets.right);
int height = size.height - (insets.top + insets.bottom);
float rectX = width / 3f;
float rectY = 2 * height / 3f;
float rectWidth = rectX;
float rectHeight = Math.min(height / 3f, 20);
float rectArcWidth = rectHeight / 2f;
float rectArcHeight = rectArcWidth;
progressBar.setRoundRect(rectX, rectY, rectWidth, rectHeight, rectArcWidth, rectArcHeight);
gradient = new LinearGradientPaint(rectX, 0, rectX + rectWidth / 2f, 0, new float[]{0f, 1f}, new Color[]{Color.BLUE, Color.CYAN}, LinearGradientPaint.CycleMethod.REFLECT);
T component = (target != null) ? target.get() : null;
if ((component != null) && (component.isDisplayable())) {
// We use a half-size image only.
int imageWidth = component.getWidth() / 2;
int imageHeight = component.getHeight() / 2;
if ((imageWidth != 0) && (imageHeight != 0)) {
image = GraphicsUtilities.createCompatibleImage(imageWidth, imageHeight);
Graphics2D g = image.createGraphics();
try {
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
if (!component.isOpaque()) {
g.setColor(Color.GRAY);
g.fillRect(0, 0, imageWidth, imageHeight);
}
g.scale(0.5, 0.5);
component.paint(g);
}
finally {
g.dispose();
}
// Gaussian blur.
GaussianBlurFilter gb = new GaussianBlurFilter(5);
image = gb.filter(image, null);
}
}
}
/** {@inheritDoc}
*/
@Override
protected void paintComponent(Graphics g) {
Dimension size = getSize();
Insets insets = getInsets();
int width = size.width - (insets.left + insets.right);
int height = size.height - (insets.top + insets.bottom);
Graphics2D g2d = (Graphics2D) g.create(insets.left, insets.top, width, height);
try {
draw(g2d, width, height);
}
finally {
g2d.dispose();
}
}
private void draw(Graphics2D g, double width, double height) {
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (image != null) {
g.drawImage(image, 0, 0, (int) Math.ceil(width), (int) Math.ceil(height), null);
}
g.setColor(getBackground());
g.fillRect(0, 0, (int) Math.ceil(width), (int) Math.ceil(height));
g.setPaint(gradient);
g.fill(progressBar);
g.setColor(getForeground());
g.setStroke(stroke);
g.draw(progressBar);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.addColumn("Name");
tableModel.addColumn("Price");
tableModel.addColumn("Quantity");
tableModel.addColumn("Tax");
tableModel.addColumn("Weight");
tableModel.addColumn("Available");
tableModel.addRow(new Object[]{"Guild Wars Prophecies", "$120.00", "1", "0.03", "0.001", Boolean.TRUE});
tableModel.addRow(new Object[]{"Guild Wars Factions", "$120.00", "1", "0.03", "0.001", Boolean.TRUE});
tableModel.addRow(new Object[]{"Guild Wars Nightfall", "$120.00", "1", "0.03", "0.001", Boolean.TRUE});
tableModel.addRow(new Object[]{"Guild Wars Eye of the North", "$120.00", "1", "0.03", "0.001", Boolean.TRUE});
tableModel.addRow(new Object[]{"World of Warcraft", "$120.00", "1", "0.03", "0.001", Boolean.TRUE});
tableModel.addRow(new Object[]{"World of Warcraft The Burning Crusade", "$120.00", "1", "0.03", "0.001", Boolean.TRUE});
tableModel.addRow(new Object[]{"World of Warcraft Wrath of the Liche King", "$120.00", "1", "0.03", "0.001", Boolean.TRUE});
tableModel.addRow(new Object[]{"The Legend of Zelda Twilight Princess", "$120.00", "1", "0.03", "0.001", Boolean.TRUE});
tableModel.addRow(new Object[]{"The Legend of Zelda Phantom Hourglass", "$120.00", "1", "0.03", "0.001", Boolean.TRUE});
tableModel.addRow(new Object[]{"Unreal Tournament III", "$120.00", "1", "0.03", "0.001", Boolean.TRUE});
tableModel.addRow(new Object[]{"Crysis", "$120.00", "1", "0.03", "0.001", Boolean.TRUE});
JTable table = new JTable(tableModel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane tableScroll = new JScrollPane(table);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1, 2));
frame.add(tableScroll);
frame.add(new WaitingPane<JComponent>(tableScroll));
frame.setSize(500, 200);
frame.setVisible(true);
}
});
}
///////////////////////
// Event management. //
///////////////////////
/** {@inheritDoc}
*/
public void componentResized(ComponentEvent event) {
image = null;
recalculate();
repaint();
}
/** {@inheritDoc}
*/
public void componentMoved(ComponentEvent event) {
}
/** {@inheritDoc}
*/
public void componentShown(ComponentEvent event) {
recalculate();
repaint();
}
/** {@inheritDoc}
*/
public void componentHidden(ComponentEvent event) {
image = null;
}
} |
Partager