Java Reference
In-Depth Information
FlowLayout canvasPanelLayout = new FlowLayout(FlowLayout.CENTER, 10, 10);
JPanel canvasPanel = new JPanel(canvasPanelLayout);
canvasPanel.add(canvas);
changeFillButton = new JButton("Change Fill");
changeStrokeButton = new JButton("Change Stroke");
FlowLayout buttonPanelLayout = new FlowLayout(FlowLayout.CENTER, 10, 10);
JPanel buttonPanel = new JPanel(buttonPanelLayout);
buttonPanel.add(changeFillButton);
buttonPanel.add(changeStrokeButton);
frame.add(canvasPanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.pack();
}
}
private static class Controller {
private View view;
private Controller(final Model model, final View view) {
this.view = view;
this.view.changeFillButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (model.fillColor.equals(Color.LIGHT_GRAY)) {
model.fillColor = Color.GRAY;
} else {
model.fillColor = Color.LIGHT_GRAY;
}
view.canvas.repaint();
}
});
this.view.changeStrokeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (model.strokeColor.equals(Color.DARK_GRAY)) {
model.strokeColor = Color.BLACK;
} else {
model.strokeColor = Color.DARK_GRAY;
}
view.canvas.repaint();
}
});
}
public void mainLoop() {
view.frame.setVisible(true);
}
}
}
Search WWH ::




Custom Search