Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a model, named ChartModel, which holds data in an array of double element

ID: 639353 • Letter: C

Question

Create a model, named ChartModel, which holds data in an array of double elements named data, and the names for the data in an array of strings named dataName. For example, the enrollment data {200, 40, 50, 100, 40} stored in the array data are for {"CS", "Math", "Chem", "Biol", "Phys"} in the array dataName. These two properties have their respective get methods, but not individual set methods. Both properties are set together in the setChartData(String[] newDataName, double[] newData) method so that they can be displayed properly. Create a view named PieChart to present the data in a pie chart, and create a view named BarChart to present the data in a bar chart, as shown in Figure (a).

In addition, create a ChartController that obtains the array of doubles (data) and the array of strings (dataname) from the user.

(Hint: Each pie represents a percentage of the total data. Color the pie using the colors from an array named colors, which is {Color.red, Color.yellow, Color.green, Color.blue, Color.cyan, Color.magenta, Color.orange, Color.pink, Color.darkGray}. Use colors[i % colors.length] for the ith pie. Use black color to display the data names.) )

Explanation / Answer

ChartModel

import java.awt.event.*;
import java.util.*;

public class ChartModel {
private double[] data = {200, 40, 50, 100, 40, 10};
private String[] dataName = {"CS", "Math", "Chem", "Biol", "Phys", "Buss"};

private transient Vector actionListeners;

public ChartModel() {
}

public double[] getData() {
return data;
}

public synchronized void removeActionListener(ActionListener l) {
if (actionListeners != null && actionListeners.contains(l)) {
Vector v = (Vector) actionListeners.clone();
v.removeElement(l);
actionListeners = v;
}
}

public synchronized void addActionListener(ActionListener l) {
Vector v = actionListeners == null ? new Vector(2) : (Vector) actionListeners.clone();
if (!v.contains(l)) {
v.addElement(l);
actionListeners = v;
}
}

protected void fireActionPerformed(ActionEvent e) {
if (actionListeners != null) {
Vector listeners = actionListeners;
int count = listeners.size();
for (int i = 0; i < count; i++) {
((ActionListener) listeners.elementAt(i)).actionPerformed(e);
}
}
}

public void setChartData(String[] newDataName, double[] newData) {
dataName = newDataName;
data = newData;
// System.arraycopy(newData, 0, data, 0, newData.length);
fireActionPerformed(new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, null));
}

public String[] getDataName() {
return dataName;
}
}

PieChart

import java.awt.*;
import javax.swing.JPanel;
import java.awt.event.*;

public class PieChart extends JPanel implements ActionListener {
BorderLayout borderLayout1 = new BorderLayout();
private ChartModel model;

public PieChart() {
this.setLayout(borderLayout1);
}

Color[] colors = {Color.red, Color.yellow, Color.green, Color.blue,
Color.cyan, Color.magenta, Color.orange, Color.pink, Color.darkGray};

public void paintComponent(Graphics g) {
super.paintComponent(g);

if (model == null) return;

int radius = (int)(Math.min(getWidth(), getHeight()) * 0.5 * 0.9);
int x = getWidth() / 2 - radius;
int y = getHeight()/2 - radius;

String[] dataName = model.getDataName();
double[] data = model.getData();

double total = 0;
for (int i = 0; i < data.length; i++)
total += data[i];

int angle1 = 0;
int angle2 = 0;
for (int i = 0; i < data.length; i++)
{
angle1 = angle1 + angle2;
angle2 = (int)Math.ceil(360 * data[i] / total);
g.setColor(colors[i % colors.length]);
g.fillArc(x, y, 2 * radius, 2 * radius, angle1, angle2);
g.setColor(Color.black);
g.drawString(dataName[i],
(int)(getWidth() / 2 + radius * Math.cos((angle1 + angle2 / 2) * 2 * Math.PI / 360)),
(int)(getHeight() / 2 - radius * Math.sin((angle1+angle2 / 2) * 2 * Math.PI / 360)));
}
}

public void setModel(ChartModel newModel)
{
model = newModel;
model.addActionListener(this);
}

public ChartModel getModel()
{
return model;
}

public void actionPerformed(ActionEvent e)
{
repaint();
}
}

BarChart

import java.awt.*;
import javax.swing.JPanel;
import java.awt.event.*;

public class BarChart extends JPanel implements ActionListener {
BorderLayout borderLayout1 = new BorderLayout();
Color[] colors = {Color.red, Color.yellow, Color.green, Color.blue,
Color.cyan, Color.magenta, Color.orange, Color.pink,
Color.darkGray};
private ChartModel model;

public BarChart() {
this.setLayout(borderLayout1);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

if (model == null) return;

String[] dataName = model.getDataName();
double[] data = model.getData();

// Find the maximum value in the data
double max = data[0];
for (int i=1; i<data.length; i++)
max = Math.max(max, data[i]);

int barWidth = (int)((getWidth() - 10.0) / data.length - 10);
int maxBarHeight = getHeight() - 30;

g.drawLine(5, getHeight() - 10, getWidth() - 5, getHeight() - 10);

int x = 15;
for (int i = 0; i < data.length; i++) {
g.setColor(colors[i % colors.length]);
int newHeight = (int)(maxBarHeight * data[i] / max);
int y = getHeight() - 10 - newHeight;
g.fillRect(x, y, barWidth, newHeight);
g.setColor(Color.black);
g.drawString(dataName[i], x, y - 7);
x += barWidth + 10;
}
}

public void setModel(ChartModel newModel) {
model = newModel;
model.addActionListener(this);
}

public ChartModel getModel() {
return model;
}

public void actionPerformed(ActionEvent e) {
repaint();
}
}

ChartController

import javax.swing.table.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.event.*;

public class ChartController extends javax.swing.JPanel implements ActionListener {
// Table selection model
DefaultListSelectionModel listSelectionModel =
new DefaultListSelectionModel();

/** Creates new form ChartController */
public ChartController() {
initComponents();

listSelectionModel.addListSelectionListener(
new javax.swing.event.ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
listSelectionModel_valueChanged(e);
}
});
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
tableModel = new MyTableModel();
jPanel1 = new javax.swing.JPanel();
jbtInsert = new javax.swing.JButton();
jbtDelete = new javax.swing.JButton();
jbtUpdate = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();

setLayout(new java.awt.BorderLayout());

jbtInsert.setText("Insert");
jbtInsert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtInsertActionPerformed(evt);
}
});

jPanel1.add(jbtInsert);

jbtDelete.setText("Delete");
jbtDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDeleteActionPerformed(evt);
}
});

jPanel1.add(jbtDelete);

jbtUpdate.setText("Update");
jbtUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtUpdateActionPerformed(evt);
}
});

jPanel1.add(jbtUpdate);

add(jPanel1, java.awt.BorderLayout.NORTH);

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {

}
));
jTable1.setModel(tableModel);
jTable1.setSelectionModel(listSelectionModel);
jScrollPane1.setViewportView(jTable1);

add(jScrollPane1, java.awt.BorderLayout.CENTER);

}//GEN-END:initComponents

private void jbtUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtUpdateActionPerformed
try {
// Get the current row
this.tableModelToChartModel();
}
catch (Exception ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_jbtUpdateActionPerformed

private void jbtDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtDeleteActionPerformed
deleteRow();
}//GEN-LAST:event_jbtDeleteActionPerformed

private void tableModelToChartModel() {
// Notify the change to the chart model
int rowCount = tableModel.getRowCount();
String[] dataName = new String[rowCount];
double[] data = new double[rowCount];
for (int i=0; i<rowCount; i++) {
dataName[i] = (String)(tableModel.getValueAt(i, 0));
data[i] = ((Double)(tableModel.getValueAt(i, 1))).doubleValue();
}

this.getChartModel().setChartData(dataName, data);
}

private void deleteRow() {
// Remove the row in the table
tableModel.removeRow(
listSelectionModel.getLeadSelectionIndex());

// Notify the change to the circle model
int rowCount = tableModel.getRowCount();

tableModelToChartModel();
}

/**Handle the selection in the table*/
void listSelectionModel_valueChanged(ListSelectionEvent e) {
int selectedRow = jTable1.getSelectedRow();
}

public void setChartModel(ChartModel newChartModel) {
chartModel = newChartModel;
chartModelToTableModel();
chartModel.addActionListener(this);
}

private void chartModelToTableModel() {
String[] dataName = chartModel.getDataName();
double[] data = chartModel.getData();

Object[][] rows = new Object[dataName.length][2];

for (int i=0; i<dataName.length; i++) {
rows[i][0] = dataName[i];
rows[i][1] = new Double(data[i]);
}

Object[] columnHeader = {"DataName", "Data"};
tableModel.setDataVector(rows, columnHeader);
}

public ChartModel getChartModel() {
return chartModel;
}

private void jbtInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtInsertActionPerformed
tableModel.insertRow(listSelectionModel.getLeadSelectionIndex(),
new Object[] {"", new Double(0)});
}//GEN-LAST:event_jbtInsertActionPerformed

public void actionPerformed(ActionEvent e) {
/**@todo: Implement this java.awt.event.ActionListener method*/
chartModelToTableModel();
repaint();
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private MyTableModel tableModel;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JButton jbtUpdate;
private javax.swing.JButton jbtInsert;
private javax.swing.JButton jbtDelete;
// End of variables declaration//GEN-END:variables

/** Holds value of property chartModel. */
private ChartModel chartModel;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote