📄 helpercomponent.java
字号:
/*
* Copyright (c) 2002-2008 TeamDev Ltd. All rights reserved.
*
* Use is subject to license terms.
*
* The complete licence text can be found at
* http://www.teamdev.com/winpack/license.jsf
*/
package teamdev.jxcapture.samples.demo;
import javax.swing.*;
import javax.swing.plaf.BorderUIResource;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ResourceBundle;
/**
* @author Ikryanov Vladimir
*/
public class HelperComponent extends JComponent {
private static final int ZOOM_VALUE = 5;
private static final int MARGIN = 10;
private int imageSize;
private int borderOffset;
private int areaOffset;
private final Color borderColor1 = new Color(105, 105, 105);
private final Color borderColor2 = new Color(210, 210, 210);
private final Color backgroundColor = new Color(149, 149, 149);
private BufferedImage backgroundImage;
private Image currentThumbnail;
private Rectangle currentRect = new Rectangle();
private Point currentPoint = new Point();
private JLabel navigationBar = new JLabel();
public HelperComponent(BufferedImage image, Dimension initialSize) {
imageSize = initialSize.width - 2 * MARGIN;
borderOffset = imageSize / ZOOM_VALUE;
areaOffset = borderOffset / 2;
int width = image.getWidth() + borderOffset;
int height = image.getHeight() + borderOffset;
backgroundImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphics = backgroundImage.getGraphics();
graphics.drawImage(image, areaOffset, areaOffset, null);
setPreferredSize(initialSize);
setSize(initialSize);
createHelpPane();
updateNavigationBar();
}
public void updateComponent(Point point, Rectangle rect) {
currentPoint = point;
currentRect = rect;
updateNavigationBar();
updateThumbnail();
}
private void createHelpPane() {
setBorder(new BorderUIResource.EmptyBorderUIResource(2 * MARGIN + imageSize, MARGIN, MARGIN, MARGIN));
setLayout(new BorderLayout());
ResourceBundle resource = ApplicationSettings.getResourceBundle();
JPanel contentPane = new JPanel(new GridBagLayout());
contentPane.setOpaque(false);
JLabel selection1Label = new JLabel(resource.getString("HelpDialog.Label.Selection1.Text"));
JLabel selection2Label = new JLabel(resource.getString("HelpDialog.Label.Selection2.Text"));
JLabel cancelLabel = new JLabel(resource.getString("HelpDialog.Label.Cancel.Text"));
JLabel helpLabel = new JLabel(resource.getString("HelpDialog.Label.Help.Text"));
selection1Label.setForeground(Color.WHITE);
selection2Label.setForeground(Color.WHITE);
cancelLabel.setForeground(Color.WHITE);
helpLabel.setForeground(Color.DARK_GRAY);
navigationBar.setHorizontalAlignment(SwingConstants.CENTER);
navigationBar.setForeground(Color.WHITE);
contentPane.add(navigationBar,
new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
contentPane.add(selection1Label,
new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
contentPane.add(selection2Label,
new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
contentPane.add(cancelLabel,
new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
contentPane.add(helpLabel,
new GridBagConstraints(0, 4, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH,
GridBagConstraints.HORIZONTAL, new Insets(10, 0, 0, 0), 0, 0));
contentPane.add(Box.createVerticalGlue(),
new GridBagConstraints(0, 5, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
add(contentPane, BorderLayout.CENTER);
}
private void updateThumbnail() {
if (currentThumbnail != null) {
currentThumbnail.flush();
currentThumbnail = null;
}
currentThumbnail = createThumbnail(currentPoint, currentRect);
repaint();
}
private void updateNavigationBar() {
navigationBar.setText("<html><b><p align='center'>X = " + currentPoint.x + ", Y = " + currentPoint.y +
"</p>Width = " + currentRect.width + ", Height = " + currentRect.height);
}
private Image createThumbnail(Point point, Rectangle rect) {
int offsetX = point.x;
int offsetY = point.y;
// draw rectangle
drawSelectedRect(rect);
// create thumbnail image
BufferedImage image = backgroundImage.getSubimage(offsetX, offsetY, borderOffset, borderOffset);
BufferedImage subImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = subImage.getGraphics();
g.drawImage(image, 0, 0, null);
// cleare rect paint
drawSelectedRect(rect);
if (rect.isEmpty()) {
// draw cursor
g.setXORMode(Color.RED);
// draw vertical line
g.drawLine(areaOffset, areaOffset - 5, areaOffset, areaOffset + 5);
// draw horisontal line
g.drawLine(areaOffset - 5, areaOffset, areaOffset + 5, areaOffset);
}
return subImage.getScaledInstance(imageSize, imageSize, BufferedImage.SCALE_FAST);
}
private void drawSelectedRect(Rectangle rect) {
if (!rect.isEmpty()) {
Graphics graphics = backgroundImage.getGraphics();
graphics.setXORMode(Color.BLACK);
graphics.drawRect(rect.x + areaOffset, rect.y + areaOffset, rect.width, rect.height);
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rect = getBounds();
// draw border
g.setColor(borderColor1);
g.drawRect(0, 0, rect.width - 1, rect.height - 1);
g.setColor(borderColor2);
g.drawRect(1, 1, rect.width - 3, rect.height - 3);
g.setColor(backgroundColor);
g.fillRect(2, 2, rect.width - 4, rect.height - 4);
// draw thumbnail
if (currentThumbnail != null) {
g.drawImage(currentThumbnail, 10, 10, null);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -