I can't figure out how to implement this class:
public class ViewController extends CircleView {
public ViewController() {
// Register mouse listener
addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent e) {
CircleModel model = getModel(); // Get model
if (model == null) return;
if (e.isMetaDown())
model.setRadius(model.getRadius() - 5); // Right button
else
model.setRadius(model.getRadius() + 5); // Left button
}
});
}
}
************
To this program:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class CircleController extends JPanel {
private CircleModel model;
private JTextField jtfRadius = new JTextField();
private JButton jbtSend = new JButton("SEND");
private JComboBox jcboFilled = new JComboBox(new Boolean[]{
new Boolean(false), new Boolean(true)});
/** Creates new form CircleController */
public CircleController() {
// Panel to group labels
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3, 1));
panel1.add(new JLabel("Radius"));
panel1.add(new JLabel(""));
panel1.add(new JLabel("Filled"));
// Panel to group text field, combo box, and another panel
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(3, 1));
panel2.add(jtfRadius);
panel2.add(jbtSend);
panel2.add(jcboFilled);
setLayout(new BorderLayout());
add(panel1, BorderLayout.WEST);
add(panel2, BorderLayout.CENTER);
// Register listeners
jbtSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (model == null) return; // No model associated yet. Do nothing
model.setRadius(new Double(jtfRadius.getText()).doubleValue());
}
});
jcboFilled.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (model == null) return; // No model associated yet. Do nothing
model.setFilled(
((Boolean)jcboFilled.getSelectedItem()).booleanVal ue());
}
});
}
public void setModel(CircleModel newModel) {
model = newModel;
}
public CircleModel getModel() {
return model;
}
}
******************
import java.awt.event.*;
import java.util.*;
public class CircleModel {
/** Property radius. */
private double radius = 20;
/** Property filled. */
private boolean filled;
/** Property color. */
private java.awt.Color color;
/** Utility field used by event firing mechanism. */
private ArrayList<ActionListener> actionListenerList;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
// Notify the listener for the change on radius
processEvent(
new ActionEvent(this, ActionEvent.ACTION_PERFORMED,"radius"));
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
// Notify the listener for the change on filled
processEvent(
new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "filled"));
}
public java.awt.Color getColor() {
return color;
}
public void setColor(java.awt.Color color) {
this.color = color;
// Notify the listener for the change on color
processEvent(
new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "color"));
}
/** Register an action event listener */
public synchronized void addActionListener(ActionListener l) {
if (actionListenerList == null)
actionListenerList = new ArrayList<ActionListener>();
actionListenerList.add(l);
}
/** Remove an action event listener */
public synchronized void removeActionListener(ActionListener l) {
if (actionListenerList != null && actionListenerList.contains(l))
actionListenerList.remove(l);
}
/** Fire TickEvent */
private void processEvent(ActionEvent e) {
ArrayList list;
synchronized (this) {
if (actionListenerList == null) return;
list = (ArrayList)actionListenerList.clone();
}
for (int i = 0; i < list.size(); i++) {
ActionListener listener = (ActionListener)list.get(i);
listener.actionPerformed(e);
}
}
}
*********************
import java.awt.*;
import java.awt.event.*;
public class CircleView extends javax.swing.JPanel
implements ActionListener {
private CircleModel model;
public void actionPerformed(ActionEvent actionEvent) {
repaint();
}
/** Set a model */
public void setModel(CircleModel newModel) {
model = newModel;
if (model != null)
// Register the view as listener for the model
model.addActionListener(this);
repaint();
}
public CircleModel getModel() {
return model;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (model == null) return;
g.setColor(model.getColor());
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius = (int)model.getRadius();
if (model.isFilled()) {
g.fillOval(xCenter - radius, yCenter - radius,
2 * radius, 2 * radius);
}
else {
g.drawOval(xCenter - radius, yCenter - radius,
2 * radius, 2 * radius);
}
}
}
**********
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MVCDemo extends JApplet {
private JButton jbtController = new JButton("Show Controller");
private JButton jbtView = new JButton("Show View");
private CircleModel model = new CircleModel();
public MVCDemo() {
setLayout(new FlowLayout());
add(jbtController);
add(jbtView);
jbtController.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame("Controller");
CircleController controller = new CircleController();
controller.setModel(model);
frame.add(controller);
frame.setSize(200, 200);
frame.setLocation(200, 200);
frame.setVisible(true);
}
});
jbtView.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame frame = new JFrame("View");
CircleView view = new CircleView();
view.setModel(model);
frame.add(view);
frame.setSize(500, 200);
frame.setLocation(200, 200);
frame.setVisible(true);
}
});
}
public static void main(String[] args) {
MVCDemo applet = new MVCDemo();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setTitle("MVCDemo");
frame.getContentPane().add(applet, BorderLayout.CENTER);
frame.setSize(400, 320);
frame.setVisible(true);
}
}
***************
Thanks for helping!