PaintBox.java: Difference between revisions
Appearance
m wrong content type |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
From: [[Into Java - Part XVI]] | |||
<PRE> | |||
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(); | |||
} | |||
} | |||
</PRE> | |||
[[Category:Into Java]] | |||
Latest revision as of 23:52, 13 June 2020
From: Into Java - Part XVI
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();
}
}