📄 zborderchooser.java
字号:
/*
* Copyright 2002 EZCell , Inc. All rights reserved.
* Version 1.0.
* Author W.John
*/
package ezcell;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
public class ZBorderChooser extends JDialog {
private static ZBorderChooser sharedInstance;
final static int IDLE = 0;
final static int SELECT_READY = 1;
final static int LEFT = 1;
final static int XCENTER = 2;
final static int RIGHT = 4;
final static int TOP = 8;
final static int YCENTER = 16;
final static int BOTTOM = 32;
final static int KONG = 0;
final static int SHI = XCENTER + YCENTER;
final static int KOU = LEFT + RIGHT + TOP + BOTTOM;
final static int TIAN = KOU + XCENTER + YCENTER;
final static int PREVIEW_SIZE = 80;
private static int pens;
private static int option;
private ZColorPicker colorPicker;
private ZPenPicker penPicker;
private _BorderPreview viewer;
public ZBorderChooser(JFrame owner) {
super(owner, "Choose Borders ...", true);
setSize(320, 234);
this.setResizable(false);
init();
ZToolkit.moveCenter(this);
}
/**
*
* @return
*/
public ZPen[] getPens() {
ZPen[] rps = new ZPen[6];
ZPen pen = new ZPen(((Integer) penPicker.getSelectedItem()).intValue(), colorPicker.getColor());
int m = 1;
for (int i = 0; i < 6; i++, m = m << 1)
if ((m & pens) != 0) {
rps[i] = pen;
}
return rps;
}
/**
*
* @param owner
*
* @return
*/
static public ZBorderChooser getSharedInstance(JFrame owner) {
if (sharedInstance == null) {
sharedInstance = new ZBorderChooser(owner);
}
return sharedInstance;
}
/**
*
* @return
*/
public int showDialog() {
option = JOptionPane.CANCEL_OPTION;
this.validate();
super.show();
return option;
}
/**
*
* @param gbc
* @param gx
* @param gy
* @param gw
* @param gh
* @param wx
* @param wy
*/
private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
/**
*/
private void init() {
viewer = new _BorderPreview();
viewer.setBorder(new EmptyBorder(20, 10, 20, 10));
colorPicker = new ZColorPicker();
penPicker = new ZPenPicker();
JPanel p = new JPanel(new GridLayout(1, 4));
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
pens = Integer.parseInt(e.getActionCommand());
viewer.setPen();
viewer.repaint();
}
};
JButton button = new JButton(new ImageIcon(getClass().getResource("/icons/bordertian.gif")));
button.addActionListener(lst);
button.setActionCommand(String.valueOf(TIAN));
p.add(button);
button = new JButton(new ImageIcon(getClass().getResource("/icons/borderkou.gif")));
button.addActionListener(lst);
button.setActionCommand(String.valueOf(KOU));
p.add(button);
button = new JButton(new ImageIcon(getClass().getResource("/icons/bordershi.gif")));
button.addActionListener(lst);
button.setActionCommand(String.valueOf(SHI));
p.add(button);
button = new JButton(new ImageIcon(getClass().getResource("/icons/borderkong.gif")));
button.addActionListener(lst);
button.setActionCommand(String.valueOf(KONG));
p.add(button);
JPanel p1 = new JPanel();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
p1.setLayout(gridbag);
buildConstraints(constraints, 0, 0, 1, 1, 100, 26);
gridbag.setConstraints(p, constraints);
p1.add(p);
buildConstraints(constraints, 0, 1, 1, 1, 0, 24);
gridbag.setConstraints(colorPicker, constraints);
// colorPicker.setBorder(new EmptyBorder(10, 0, 10, 0));
p1.add(colorPicker);
buildConstraints(constraints, 0, 2, 1, 1, 0, 24);
gridbag.setConstraints(penPicker, constraints);
p1.add(penPicker);
JPanel p2= new JPanel();
buildConstraints(constraints, 0, 3, 1, 1, 0, 26);
gridbag.setConstraints(p2, constraints);
p1.add(p2);
//p1.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
JPanel p0 = new JPanel(new GridLayout(1, 1));
p0.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
// p0.setLayout()
// viewer.setPreferredSize(new Dimension(150,150));
p0.add(viewer);
p = new JPanel(new GridLayout(1, 2));
p.setBorder(new EmptyBorder(10, 10, 10, 10));
p.add(p0);
p.add(p1);
// command pane
JPanel commandPane = new JPanel(new FlowLayout());
p1 = new JPanel(new GridLayout(1, 2, 10, 2));
JButton btOK = new JButton("OK");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (pens != 0) {
option = JOptionPane.OK_OPTION;
}
setVisible(false);
}
};
btOK.addActionListener(lst);
p1.add(btOK);
JButton btCancel = new JButton("Cancel");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
option = JOptionPane.CANCEL_OPTION;
setVisible(false);
}
};
btCancel.addActionListener(lst);
p1.add(btCancel);
commandPane.add(p1);
gridbag = new GridBagLayout();
this.getContentPane().setLayout(gridbag);
buildConstraints(constraints, 0, 0, 1, 1, 100, 80);
gridbag.setConstraints(p, constraints);
this.getContentPane().add(p);
buildConstraints(constraints, 0, 1, 1, 1, 100, 20);
gridbag.setConstraints(commandPane, constraints);
this.getContentPane().add(commandPane);
}
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
class _BorderPreview extends JPanel {
private Cursor defaultCursor;
private Cursor selectCursor;
private ZPen selectedPen = new ZPen(3, Color.black);
private ZPen nonselectedPen = new ZPen(3, Color.lightGray);
private Point offset;
private int state;
private ZShape.SimpleHLine selectedLine;
private ZShape.SimpleHLine[] lines = new ZShape.SimpleHLine[] {
new ZShape.SimpleHLine(0, 0, 0, PREVIEW_SIZE, nonselectedPen, LEFT), //
new ZShape.SimpleHLine(0, 0, PREVIEW_SIZE, 0, nonselectedPen, TOP), //
new ZShape.SimpleHLine(PREVIEW_SIZE, 0, PREVIEW_SIZE, PREVIEW_SIZE, nonselectedPen, RIGHT), //
new ZShape.SimpleHLine(0, PREVIEW_SIZE, PREVIEW_SIZE, PREVIEW_SIZE, nonselectedPen, BOTTOM), //
new ZShape.SimpleHLine((int) (PREVIEW_SIZE / 2), 0, (int) (PREVIEW_SIZE / 2), PREVIEW_SIZE, nonselectedPen, XCENTER), //
new ZShape.SimpleHLine(0, (int) (PREVIEW_SIZE / 2), PREVIEW_SIZE, (int) (PREVIEW_SIZE / 2), nonselectedPen, YCENTER)
};
_BorderPreview() {
defaultCursor = (Cursor) ZUIManager.getDefault().get("Cursor.move");
selectCursor = (Cursor) ZUIManager.getDefault().get("Cursor.thin.cross");
offset = new Point((int) ((142 - PREVIEW_SIZE) / 2), (int) ((135 - PREVIEW_SIZE) / 2));
this.setBackground(Color.white);
this.setToolTipText("You can select a border by clicking mouse!");
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (selectedLine != null) {
ZPen pen = (selectedLine.getPen() == selectedPen) ? nonselectedPen : selectedPen;
if (pen == selectedPen) {
pens |= selectedLine.getID();
} else {
pens &= (~selectedLine.getID());
}
setPen();
repaint();
}
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
p.x -= offset.x;
p.y -= offset.y;
for (int i = 0; i < lines.length; i++) {
if ((selectedLine = (ZShape.SimpleHLine) lines[i].hit(p)) != null) {
if (state == IDLE) {
_BorderPreview.this.setCursor(selectCursor);
}
state = SELECT_READY;
return;
}
}
if (state == SELECT_READY) {
_BorderPreview.this.setCursor(defaultCursor);
}
state = IDLE;
}
});
}
/**
*/
public void setPen() {
for (int i = 0; i < lines.length; i++) {
if ((lines[i].getID() & pens) != 0) {
lines[i].setPen(selectedPen);
} else {
lines[i].setPen(nonselectedPen);
}
}
}
/**
*
* @param g
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.translate(offset.x, offset.y);
for (int i = 0; i < lines.length; i++)
if (lines[i].getPen() == nonselectedPen) {
lines[i].paint2(g2);
}
for (int i = 0; i < lines.length; i++)
if (lines[i].getPen() == selectedPen) {
lines[i].paint2(g2);
}
}
}
}
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
class ZPenPicker extends JComboBox {
private static int[] values = new int[] { 0, 1, 2, 3, 4 };
ZPenPicker() {
init();
setRenderer(new _Renderer());
this.setSelectedIndex(1);
}
/**
*/
private void init() {
for (int i = 0; i < values.length; i++) {
addItem(new Integer(values[i]));
}
}
/**
* DOCUMENT ME!
*
* @version 1.00
* @author W.John
*/
class _Renderer extends JPanel implements ListCellRenderer {
protected BasicStroke pen;
protected Color focusColor = (Color) UIManager.get("List.selectionBackground");
protected Color nonFocusColor = Color.white;
protected boolean hasFocus;
/**
*
* @param list
* @param obj
* @param row
* @param sel
* @param hasFocus
*
* @return
*/
public Component getListCellRendererComponent(JList list, Object obj, int row, boolean sel, boolean hasFocus) {
this.hasFocus = (hasFocus || sel);
if (obj instanceof Integer) {
pen = new BasicStroke(((Integer) obj).intValue());
}
// this.setPreferredSize(new Dimension(60,21));
return this;
}
/**
*
* @param g
*/
public void paintComponent(Graphics g) {
setBackground(hasFocus ? focusColor : nonFocusColor);
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (pen.getLineWidth() == 0) {
g2.drawString("none", 12, 17); //getHeight());
} else {
g2.setStroke(pen);
g2.setColor(Color.black);
g2.draw(new Line2D.Float(1, this.getHeight() / 2.0f, this.getWidth(), this.getHeight() / 2.0f));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -