📄 zbrushchooser.java
字号:
/*
* put your module comment here
* formatted with JxBeauty (c) johann.langhofer@nextra.at
*/
package ezcell;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;
import javax.swing.border.*;
/**
* put your documentation comment here
*/
public class ZBrushChooser extends JDialog {
private static ZBrushChooser sharedInstance;
private CardLayout layout;
private JPanel brushesPane;
private JRadioButton colorButton;
private JRadioButton gradiantButton;
private JRadioButton imageButton;
private ButtonGroup buttonGroup1 = new ButtonGroup();
// image brush panel
private JPanel imageBrush;
private JTextField filePath;
private _ImagePreview pictureViewer;
// color panel
private JPanel colorBrush;
private JComboBox colorPicker;
// gradiant panel
private JPanel gradiantBrush;
private JComboBox colorPicker0;
private JComboBox colorPicker1;
private _GradiantPreview gradiantViewer;
private int option;
private ZBrush brush;
/**
* put your documentation comment here
* @param JFrame owner
*/
public ZBrushChooser (JFrame owner) {
super(owner, "Select Brush ...", true);
setSize(400, 300);
this.setResizable(false);
// option pane
colorButton = new JRadioButton("Color Brush");
gradiantButton = new JRadioButton("Gradiant Brush");
imageButton = new JRadioButton("Image Brush");
ButtonGroup group = new ButtonGroup();
group.add(colorButton);
group.add(gradiantButton);
group.add(imageButton);
JPanel optionPane = new JPanel();
optionPane.add(colorButton);
optionPane.add(gradiantButton);
optionPane.add(imageButton);
optionPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
ActionListener lst = new ActionListener() {
/**
* put your documentation comment here
* @param e
*/
public void actionPerformed (ActionEvent e) {
if (e.getSource() == colorButton)
layout.show(brushesPane, "color");
else if (e.getSource() == gradiantButton)
layout.show(brushesPane, "gradient");
else if (e.getSource() == imageButton)
layout.show(brushesPane, "image");
}
};
colorButton.addActionListener(lst);
gradiantButton.addActionListener(lst);
imageButton.addActionListener(lst);
// prepare color pickers
colorPicker = new ZColorPicker();
colorPicker0 = new ZColorPicker();
colorPicker1 = new ZColorPicker();
colorPicker0.setSelectedItem(Color.red);
colorPicker1.setSelectedItem(Color.white);
// image brush pane
imageBrush = new JPanel(new BorderLayout());
JPanel p1 = new JPanel(new BorderLayout());
p1.setBorder(new EmptyBorder(5, 25, 0, 25));
p1.add(new JLabel("Path:"), BorderLayout.WEST);
filePath = new JTextField();
p1.add(filePath, BorderLayout.CENTER);
JButton browser = new JButton("...");
lst = new ActionListener() {
/**
* put your documentation comment here
* @param e
*/
public void actionPerformed (ActionEvent e) {
ZFileChooser chooser = ZFileChooser.getSharedInstance(ZBrushChooser.this);
if (chooser.show() == JOptionPane.OK_OPTION) {
String path = chooser.getSelectedFile().getPath();
filePath.setText(path);
pictureViewer.loadImage(path);
}
}
};
browser.addActionListener(lst);
p1.add(browser, BorderLayout.EAST);
imageBrush.add(p1, BorderLayout.NORTH);
pictureViewer = new _ImagePreview();
imageBrush.add(pictureViewer, BorderLayout.CENTER);
// color brush pane
colorBrush = new JPanel(new BorderLayout());
p1 = new JPanel();
p1.setBorder(new EmptyBorder(50, 0, 50, 0));
p1.add(new JLabel("Color:"));
p1.add(colorPicker);
colorBrush.add(p1, BorderLayout.CENTER);
// gradiant brush
gradiantBrush = new JPanel();
p1 = new JPanel();
p1.add(new JLabel("Start Color :"));
p1.add(colorPicker0);
gradiantBrush.add(p1);
p1 = new JPanel();
p1.add(new JLabel("End Color :"));
p1.add(colorPicker1);
gradiantBrush.add(p1);
gradiantViewer = new _GradiantPreview();
gradiantViewer.setToolTipText("You can drag mouse to select start/end position.");
gradiantViewer.setCursor((Cursor)ZUIManager.getDefault().get("Cursor.thin.cross"));
colorPicker0.addActionListener(new ActionListener() {
/**
* put your documentation comment here
* @param e
*/
public void actionPerformed (ActionEvent e) {
gradiantViewer.setStartColor((Color)colorPicker0.getSelectedItem());
}
});
colorPicker1.addActionListener(new ActionListener() {
/**
* put your documentation comment here
* @param e
*/
public void actionPerformed (ActionEvent e) {
gradiantViewer.setEndColor((Color)colorPicker1.getSelectedItem());
}
});
gradiantBrush.add(gradiantViewer);
gradiantBrush.setBorder(new EmptyBorder(20, 0, 50, 0));
// 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() {
/**
* put your documentation comment here
* @param e
*/
public void actionPerformed (ActionEvent e) {
if (colorButton.isSelected())
brush = new ZColorBrush((Color)colorPicker.getSelectedItem());
else if (gradiantButton.isSelected())
brush = gradiantViewer.getBrush();
else {
String path = filePath.getText();
File file = new File(path);
if (!file.isFile()) {
JOptionPane.showMessageDialog(null, "file not exist !", "alert", JOptionPane.ERROR_MESSAGE);
return;
}
brush = new ZImageBrush(filePath.getText());
}
option = JOptionPane.OK_OPTION;
setVisible(false);
}
};
btOK.addActionListener(lst);
p1.add(btOK);
JButton btCancel = new JButton("Cancel");
lst = new ActionListener() {
/**
* put your documentation comment here
* @param e
*/
public void actionPerformed (ActionEvent e) {
option = JOptionPane.CANCEL_OPTION;
setVisible(false);
}
};
btCancel.addActionListener(lst);
p1.add(btCancel);
commandPane.add(p1);
commandPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(optionPane, BorderLayout.NORTH);
layout = new CardLayout();
brushesPane = new JPanel(layout);
brushesPane.add("color", colorBrush);
brushesPane.add("gradient", gradiantBrush);
brushesPane.add("image", imageBrush);
this.getContentPane().add(brushesPane, BorderLayout.CENTER);
this.getContentPane().add(commandPane, BorderLayout.SOUTH);
ZToolkit.moveCenter(this);
}
/**
* put your documentation comment here
* @param brush
* @return
*/
public int show (ZBrush brush) {
setProperty(brush);
this.show();
return option;
}
/**
* put your documentation comment here
* @param brush
*/
private void setProperty (ZBrush brush) {
if (brush instanceof ZColorBrush) {
colorPicker.setSelectedItem(((ZColorBrush)brush).getColor());
colorButton.setSelected(true);
layout.show(brushesPane, "color");
}
else if (brush instanceof ZGradientBrush) {
ZGradientBrush gb = (ZGradientBrush)brush;
colorPicker0.setSelectedItem(gb.getFromColor());
colorPicker1.setSelectedItem(gb.getToColor());
gradiantViewer.setProperty(gb);
gradiantButton.setSelected(true);
layout.show(brushesPane, "gradient");
}
else if (brush instanceof ZImageBrush) {
ZImageBrush ib = (ZImageBrush)brush;
filePath.setText(ib.getPath());
pictureViewer.loadImage(ib.getPath());
imageButton.setSelected(true);
layout.show(brushesPane, "image");
}
}
/**
* put your documentation comment here
* @return
*/
public ZBrush getSelectedBrush () {
return brush;
}
/**
* put your documentation comment here
* @param owner
* @return
*/
public static ZBrushChooser getSharedInstance (JFrame owner) {
if (sharedInstance == null)
sharedInstance = new ZBrushChooser(owner);
return sharedInstance;
}
/**
* put your documentation comment here
*/
class _ImagePreview extends JPanel {
ImageIcon thumbnail = null;
/**
* put your documentation comment here
*/
public _ImagePreview () {
setPreferredSize(new Dimension(280, 210));
}
/**
* put your documentation comment here
* @param fileName
*/
public void loadImage (String fileName) {
ImageIcon tmpIcon = new ImageIcon(fileName);
if (tmpIcon.getIconWidth() > 270) {
thumbnail = new ImageIcon(tmpIcon.getImage().getScaledInstance(270, -1, Image.SCALE_DEFAULT));
}
else {
thumbnail = tmpIcon;
}
repaint();
}
/**
* put your documentation comment here
* @param g
*/
public void paintComponent (Graphics g) {
if (thumbnail != null) {
int x = getWidth()/2 - thumbnail.getIconWidth()/2;
int y = getHeight()/2 - thumbnail.getIconHeight()/2;
if (y < 0) {
y = 0;
}
if (x < 5) {
x = 5;
}
thumbnail.paintIcon(this, g, x, y);
}
}
}
/**
* put your documentation comment here
*/
class _GradiantPreview extends JComponent {
final static int IDLE = 0;
final static int FROM_POINT_READY = 1;
private Point fromPoint;
private Point toPoint;
private Color fromColor = Color.red;
private Color toColor = Color.white;
private ZBrush brush;
boolean dirty = true;
private int state;
/**
* put your documentation comment here
*/
public _GradiantPreview () {
setPreferredSize(new Dimension(100, 100));
this.addMouseListener(new MouseAdapter() {
/**
* put your documentation comment here
* @param e
*/
public void mouseReleased (MouseEvent e) {
if (state == FROM_POINT_READY) {
toPoint = e.getPoint();
dirty = true;
repaint();
}
state = IDLE;
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
/**
* put your documentation comment here
* @param e
*/
public void mouseDragged (MouseEvent e) {
if (state == IDLE)
fromPoint = e.getPoint();
state = FROM_POINT_READY;
}
});
}
/**
* put your documentation comment here
* @param color
*/
public void setStartColor (Color color) {
fromColor = color;
dirty = true;
repaint();
}
/**
* put your documentation comment here
* @param color
*/
public void setEndColor (Color color) {
toColor = color;
dirty = true;
repaint();
}
/**
* put your documentation comment here
* @return
*/
public ZBrush getBrush () {
if (dirty)
makeBrush();
return brush;
}
/**
* put your documentation comment here
*/
private void makeBrush () {
if (!dirty)
return;
if (fromPoint == null)
fromPoint = new Point();
if (toPoint == null)
toPoint = new Point(this.getWidth(), this.getHeight());
brush = new ZGradientBrush(fromPoint, fromColor, toPoint, toColor, false);
dirty = false;
}
/**
* put your documentation comment here
* @param g
*/
public void paintComponent (Graphics g) {
getBrush().fill((Graphics2D)g, new ZRect(0, 0, this.getWidth(), this.getHeight()));
}
/**
* put your documentation comment here
* @param brush
*/
public void setProperty (ZGradientBrush brush) {
fromPoint = brush.getFromPoint();
toPoint = brush.getToPoint();
fromColor = brush.getFromColor();
toColor = brush.getToColor();
dirty = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -