PaintBox.java: Difference between revisions
Appearance
m keep the garbage |
m Ak120 moved page Editing Into Java - Part XVI:PaintBox.java to PaintBox.java |
(No difference)
| |
Revision as of 18:16, 22 February 2018
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PaintBox extends JFrame {
private SlidePanel colorChooser;
private JPanel colorView;
private TopButtonPanel topPanel;
private PaintPanel paintArea;
/* the constructor */
public PaintBox() {
firstInit();
init();
}
private void init() { // a helper method
Container pane = getContentPane();
/* the color chooser panel at the east side */
JPanel eastPanel = new JPanel();
/* A layout manager of the more complex kind */
GridBagLayout gbl = new GridBagLayout();
eastPanel.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
colorChooser = new SlidePanel(this);
gbc.gridx = 0; // first column
gbc.gridy = 0; // first row
eastPanel.add(colorChooser, gbc);
colorView = new JPanel();
colorView.setBackground(Color.black);
gbc.gridx = 0; // first column
gbc.gridy = 1; // second row
gbc.ipadx = 90;
gbc.ipady = 90;
eastPanel.add(colorView, gbc);
pane.add(eastPanel, "East");
/* the buttons at the top */
topPanel = new TopButtonPanel(this);
pane.add(topPanel, "North");
/* the main paint area */
paintArea = new PaintPanel();
pane.add(paintArea, "Center");
}
public void setColor(int r, int g, int b) {
colorView.setBackground(new Color(r, g, b));
paintArea.setColor(new Color(r, g, b));
}
public void setFilled(boolean b) {
paintArea.setFilled(b);
}
public void setShape(int i) {
paintArea.setShape(i);
}
/* A common GUI constructor helper method */
private void firstInit() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setTitle("PaintBox");
setSize(600,400);
/* Find the middle position on the screen */
Dimension dim = Toolkit.getDefaultToolkit().
getScreenSize();
int w = (dim.width - 600) / 2;
int h = (dim.height -300) / 2;
if (w < 0 || h < 0) {
w = 0;
h = 0;
}
setLocation(w, h);
}
public static void main(String[] arg) {
PaintBox p = new PaintBox();
p.show();
}
}