// <applet code="MultiClass" width="300" height="300"></applet>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MultiClass extends JApplet {
// Object references can be declared:
// ClassType variableName;
// or declared and instantiated:
// ClassType reference/variable = new Object();
// Object on right hand side is created/instantiated
// with the new operator. A reference to it,
// saved in a member variable, is what we use to
// access the objects fields and methods.
// Use the "view" variable to access fields and call
// methods in this new instance of the View class.
View view = new View();
// Create an instance of the Control class and save
// a reference to it (the new object) in the member
// variable "control".
Control control = new Control(view);
public void init() {
System.out.println(getLayout().getClass().getSimpleName());
add(view);
add(control.getControlPanel(), "Last");
}
}
class View extends JPanel {
Color color = Color.blue;
Point center = new Point(150,125);
int side = 100;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.drawRect(center.x-side/2, center.y-side/2, side, side);
}
public void setCenterX(int x) {
center.x = x;
repaint();
}
public void setCenterY(int y) {
center.y = y;
repaint();
}
public void setColor(Color color) {
this.color = color;
repaint();
}
}
class Control implements ActionListener, ChangeListener {
// Object reference to the instance of the View class
// being shown in the applet. You can use this object
// reference to access fields and call methods in this
// instance of View.
View view;
JSlider xSlider;
JSlider ySlider;
public Control(View view) {
this.view = view;
}
public void actionPerformed(ActionEvent e) {
Color color = ((JButton)e.getSource()).getBackground();
view.setColor(color);
}
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider)e.getSource();
int value = slider.getValue();
if(slider == xSlider) {
view.setCenterX(value);
}
if(slider == ySlider) {
view.setCenterY(value);
}
}
public JPanel getControlPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,2,2,2);
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(getSliderPanel(), gbc);
panel.add(getButtonPanel(), gbc);
return panel;
}
private JPanel getSliderPanel() {
Point p = view.center;
xSlider = getSlider(p.x);
ySlider = getSlider(p.y);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,2,2,2);
addComponents("x", xSlider, panel, gbc);
addComponents("y", ySlider, panel, gbc);
return panel;
}
private JSlider getSlider(int value) {
JSlider slider = new JSlider(50, 250, value);
slider.addChangeListener(this);
return slider;
}
private void addComponents(String s, JSlider slider, Container c,
GridBagConstraints gbc) {
gbc.weightx = 0;
gbc.fill = GridBagConstraints.NONE;
c.add(new JLabel(s), gbc);
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
c.add(slider, gbc);
}
private JPanel getButtonPanel() {
Color[] colors = {
Color.red, Color.green.darker(), Color.blue, Color.orange
};
JPanel panel = new JPanel(new GridLayout(1,0));
for(int i = 0; i < colors.length; i++) {
JButton button = new JButton(" ");
button.setBackground(colors[i]);
button.addActionListener(this);
panel.add(button);
}
return panel;
}
} |