In the demomvc application, there is no connection between selecting a color in
ID: 3827651 • Letter: I
Question
In the demomvc application, there is no connection between selecting a color in the JList and drawing on the PaintPanel. In this lab, this will be changed so that the color selected by the user will become the color of the paint brush.
Task
Before starting to paint Points in DemoModel.java with different Colors, the Point array in DemoModel.java should be changed to an ArrayList<Point>. This will avoid out of bounds errors. You should do this first and ensure that your changes work before going on to other tasks.
DemoModel.java already stores the Color that the user has selected. Change PaintPanel.java to draw the Points using this Color. [Hint: There is a method in Graphics to change the drawing Color.] This change should result in all the Points between drawn using the currently selected Color, which is not quite what is desired.
So that Points can be drawn in different Colors, DemoModel.java needs to store a Color for each point. This could be done using an ArrayList<Color> and adding a Color to this ArrayList every time a Point is added.
Finally, PaintPanel.java needs to be able to access the Colors stored in DemoModel.java. Create a getColor method in DemoModel.java that will return the Color at a given index. This can be similar to the GetPoint method. Now PaintPanel.java can be changed to use this method to obtain a Color for each Point.
demomvc-
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
/**
* This demonstrates the controller in a model-view-controller pattern.
* Adapted from Figures 14.23 and 14.34.
* @author Tom Bylander
*/
public class DemoController implements ListSelectionListener, MouseMotionListener {
/**
* The model of this MVC example
*/
private DemoModel model;
/**
* The view of this MVC example
*/
private DemoView view;
public DemoController(DemoModel model, DemoView view) {
this.model = model;
this.view = view;
}
/**
* Add a point to the model when the user drags the mouse, and
* repaint the window. Need more logic to draw solid lines.
*/
public void mouseDragged(MouseEvent event) {
Point point = event.getPoint(); // find point
model.addPoint(point);
view.repaint();
} // end method mouseDragged
/**
* This method doesn't do anything, but it needs to be
* here to implement MouseMotionListener.
*/
public void mouseMoved(MouseEvent event) {
// this method intentionally left blank
}
/**
* Update the model when the user selects a color, and repaint the
* window.
*/
public void valueChanged(ListSelectionEvent event) {
Color color = view.getSelectedColor();
model.setSelectedColor(color);
view.repaint();
}
}
import java.awt.*;
/**
* The DemoModel class holds the information that is used by the GUI.
* Ask yourself the question, what data would be needed to recreate
* the state of the GUI? This data is what should be stored in the
* model.
* <p>
* The instance variables are from Fig. 14.34.
* @author Tom Bylander
*/
public class DemoModel {
/**
* The number of points
*/
private int pointCount;
/**
* An array of 10000 java.awt.Point references
*/
private Point[] points;
/**
* The color selected by the user
*/
private Color selectedColor;
public DemoModel() {
pointCount = 0;
points = new Point[10000];
selectedColor = Color.CYAN;
}
/**
* Add a Point to the points array.
* @param point the Point to be added to points.
*/
public void addPoint(Point point) {
// doesn't avoid out-of-bounds errors
points[pointCount] = point;
pointCount++;
}
/**
* Returns point at index i.
* Returns null if no such point exists.
* @param i
*/
public Point getPoint(int i) {
if (i >= 0 && i < pointCount) {
// probably should return a new point so that the return
// value cannot be used to change the array element
return points[i];
}
return null;
}
/**
* Store the color that the user selected.
* @param color the color selected by the user
*/
public void setSelectedColor(Color color) {
selectedColor = color;
}
/**
* @return the color selected by the user
*/
public Color getSelectedColor() {
return selectedColor;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* This demonstrates the model-view-controller design pattern.
* Adapted from Figures 14.23 and 14.34.
* @author Tom Bylander
*/
public class DemoMVC {
/**
* main method starts the application
*/
public static void main(String[] args) {
DemoModel model = new DemoModel();
DemoView view = new DemoView(model);
DemoController controller = new DemoController(model, view);
// register controller as the listener
view.registerListener(controller);
// start it up
view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
view.setSize(400, 300);
view.setVisible(true);
}
}
/**
* Register the controller as the listener to the JList and the
* MousePanel.
* @param listener
*/
public void registerListener(DemoController listener) {
colorList.addListSelectionListener(listener);
mousePanel.addMouseMotionListener(listener);
}
/**
* @return The color selected by the user.
*/
public Color getSelectedColor() {
return colors[colorList.getSelectedIndex()];
}
/**
* Sets the background color of the JList and calls super.paint(g)
* to paint the components.
*/
public void paint(Graphics g) {
listPanel.setBackground(model.getSelectedColor());
super.paint(g); // This will paint the components.
} // end method paint
}
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JPanel;
/**
* This is part of the view allowing the mouse to paint a JPanel.
* This is modified from Fig 14.34.
* @author Deitel & Associates, Inc.
* @author Tom Bylander
*/
public class PaintPanel extends JPanel {
/**
* The model of this MVC example (it stores the points)
*/
private DemoModel model;
/**
* Store the model that holds the points to be drawn.
* @param model
*/
public PaintPanel(DemoModel model) {
this.model = model;
} // end PaintPanel constructor
/**
* Draw ovals in a 4-by-4 bounding box at specified locations on
* the panel.
*/
public void paintComponent(Graphics g) {
super.paintComponent(g); // clears drawing area
int i = 0;
Point point = model.getPoint(0);
while (point != null) {
g.fillOval(point.x, point.y, 4, 4);
i++;
point = model.getPoint(i);
}
} // end method paintComponent
} // end class PaintPanel
Explanation / Answer
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JPanel;
/**
* This is part of the view allowing the mouse to paint a JPanel.
* This is modified from Fig 14.34.
* @author Deitel & Associates, Inc.
* @author Tom Bylander
*/
public class PaintPanel extends JPanel {
/**
* The model of this MVC example (it stores the points)
*/
private DemoModel model;
/**
* Store the model that holds the points to be drawn.
* @param model
*/
public PaintPanel(DemoModel model) {
this.model = model;
} // end PaintPanel constructor
/**
* Draw ovals in a 4-by-4 bounding box at specified locations on
* the panel.
*/
public void paintComponent(Graphics g) {
super.paintComponent(g); // clears drawing area
int i = 0;
Point point = model.getPoint(0);
while (point != null) {
g.setColor(model.getColor(i));
g.fillOval(point.x, point.y, 4, 4);
i++;
point = model.getPoint(i);
}
} // end method paintComponent
} // end class PaintPanel
---------------------------------------------------
import java.awt.*;
import java.util.ArrayList;
public class DemoModel {
/**
* The number of points
*/
private int pointCount;
/**
* An array of 10000 java.awt.Point references
*/
private ArrayList<Point> points;
private ArrayList<Color> colors;
/**
* The color selected by the user
*/
private Color selectedColor;
public DemoModel() {
pointCount = 0;
points = new ArrayList<Point>();
colors=new ArrayList<Color>();
selectedColor = Color.CYAN;
}
/**
* Add a Point to the points array.
* @param point the Point to be added to points.
*/
public void addPoint(Point point) {
// doesn't avoid out-of-bounds errors
points.add(point);
colors.add(selectedColor);
pointCount++;
}
/**
* Returns point at index i.
* Returns null if no such point exists.
* @param i
*/
public Point getPoint(int i) {
if (i >= 0 && i < pointCount) {
// probably should return a new point so that the return
// value cannot be used to change the array element
return points.get(i);
}
return null;
}
/**
* Store the color that the user selected.
* @param color the color selected by the user
*/
public void setSelectedColor(Color color) {
selectedColor = color;
}
/**
* @return the color selected by the user
*/
public Color getSelectedColor() {
return selectedColor;
}
public Color getColor(int index)
{
if(index<colors.size())
{
return colors.get(index);
}
else
{
return null;
}
}
}