import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
@SuppressWarnings("serial")
public class GUI extends JFrame
implements ActionListener
{
//API buttons
private JButton quizButton;
private JButton homeworkButton;
private JButton midtermButton;
private JButton finalprojectButton;
private JButton coursescoreButton;
private JButton coursegradeButton;
//button flags
private boolean drawQuizGraph = false;
private boolean drawHomeworkGraph = false;
private boolean drawMidtermGraph = false;
private boolean drawFinalProjectGraph = false;
private boolean drawCourseAveragesGraph = false;
private boolean drawCourseGradesGraph = false;
//grade and score counters
private int aGrades = 0;
private int aMinusGrades = 0;
private int bPlusGrades = 0;
private int bGrades = 0;
private int bMinusGrades = 0;
private int cGrades = 0;
private int midtermScore = 0;
private int finalProjectScore = 0;
// Constructor
public GUI(HashMap<String, Student> studentMap)
{
super("Graphs"); // Window title
// Get the container and add the buttons
Container c = getContentPane();
c.setLayout(new FlowLayout());
quizButton = new JButton("Quiz Averages"); // Create a button
quizButton.addActionListener( this ); // Register the event handler
c.add(quizButton); // Add the button to the Application
homeworkButton = new JButton("Homework Averages"); // Create a button
homeworkButton.addActionListener( this ); // Register the event handler
c.add(homeworkButton); // Add the button to the Application
midtermButton = new JButton("Midterm Scores"); // Create a button
midtermButton.addActionListener( this ); // Register the event handler
c.add(midtermButton); // Add the button to the Application
finalprojectButton = new JButton("Final Project Scores"); // Create a button
finalprojectButton.addActionListener( this ); // Register the event handler
c.add(finalprojectButton); // Add the button to the Application
coursescoreButton = new JButton("Course Averages"); // Create a button
coursescoreButton.addActionListener( this ); // Register the event handler
c.add(coursescoreButton); // Add the button to the Application
coursegradeButton = new JButton("Course Grades"); // Create a button
coursegradeButton.addActionListener( this ); // Register the event handler
c.add(coursegradeButton); // Add the button to the Application
Collection<Student> students = studentMap.values();
Iterator<Student> mapItr = students.iterator();//map iterator
while (mapItr.hasNext())//iterate to end of map
{
Student currentStudent = mapItr.next();
if (currentStudent.getCourseGrade() == "A")
aGrades++;
else if (currentStudent.getCourseGrade() == "B+")
bPlusGrades++;
else if (currentStudent.getCourseGrade() == "B")
bGrades++;
else if (currentStudent.getCourseGrade() == "B-")
bMinusGrades++;
else
cGrades++;
System.out.println("Totals printing");
System.out.println(aGrades);
System.out.println(bGrades);
System.out.println(cGrades);
}
}
public void paintGraphYLabels( Graphics g )//draws Y axis graph labels
{
g.drawString("0 Students", 10, 750);
g.drawString("10 Students", 10, 650);
g.drawString("20 Students", 10, 550);
g.drawString("30 Students", 10, 450);
g.drawString("40 Students", 10, 350);
g.drawString("50+ Students", 10, 250);
}
public void paintGraphXLabels( Graphics g )//draws X axis graph labels
{
g.drawString("100-95", 145, 780);
g.drawString("94-90", 245, 780);
g.drawString("89-85", 345, 780);
g.drawString("84-80", 445, 780);
g.drawString("79-75", 545, 780);
g.drawString("74 or less", 645, 780);
}
public void paintGraphBars( Graphics g )//method draws actual graph bars
{
//draw the bars
g.setColor(Color.blue);
int x1 = 165;
int y1 = 700;
g.fillRect(x1, y1, 50, 50);
g.setColor(Color.green);
x1 = 265;
y1 = 700;
g.fillRect(x1, y1-100, 50, 150);
g.setColor(Color.orange);
x1 = 365;
y1 = 700;
g.fillRect(x1, y1-100, 50, 150);
g.setColor(Color.yellow);
x1 = 465;
y1 = 700;
g.fillRect(x1, y1-100, 50, 150);
g.setColor(Color.red);
x1 = 565;
y1 = 700;
g.fillRect(x1, y1-100, 50, 150);
g.setColor(Color.pink);
x1 = 665;
y1 = 700;
g.fillRect(x1, y1-150, 50, 200);
}
public void paint( Graphics g )
{
super.paint(g);
g.setColor(Color.black);
g.setFont(new Font("Times", Font.BOLD, 20));
if (drawQuizGraph == true)//Quiz button clicked, draw the graph
{
g.drawString(" Quiz Averages", 10, 150);//graph title
paintGraphXLabels(g);
paintGraphYLabels(g);
paintGraphBars(g);
//set flag to false
drawQuizGraph = false;
}
else if (drawHomeworkGraph == true)//Course Grade button clicked, draw the graph
{
g.drawString(" Homework Averages", 10, 150);//graph title
paintGraphXLabels(g);
paintGraphYLabels(g);
paintGraphBars(g);
drawHomeworkGraph = false;
}
else if (drawMidtermGraph == true)//Course Grade button clicked, draw the graph
{
g.drawString(" Midterm Scores", 10, 150);//graph title
paintGraphXLabels(g);
paintGraphYLabels(g);
paintGraphBars(g);
drawMidtermGraph = false;
}
else if (drawFinalProjectGraph == true)//Course Grade button clicked, draw the graph
{
g.drawString(" Final Project Scores", 10, 150);//graph title
paintGraphXLabels(g);
paintGraphYLabels(g);
paintGraphBars(g);
drawFinalProjectGraph = false;
}
else if (drawCourseAveragesGraph == true)//Course Grade button clicked, draw the graph
{
g.drawString(" Course Score Averages", 10, 150);//graph title
paintGraphXLabels(g);
paintGraphYLabels(g);
paintGraphBars(g);
drawCourseAveragesGraph = false;
}
else if (drawCourseGradesGraph == true)//Course Grade button clicked, draw the graph
{
g.drawString(" Course Grade Averages", 10, 150);//graph title
paintGraphYLabels(g);
//X axis labels
g.drawString("A", 185, 780);
g.drawString("A-", 285, 780);
g.drawString("B+", 385, 780);
g.drawString("B", 485, 780);
g.drawString("B-", 585, 780);
g.drawString("C", 685, 780);
g.setColor(Color.blue);
int x1 = 175;
int y1 = 700;
g.fillRect(x1, y1, 50, 50);
g.setColor(Color.green);
x1 = 275;
y1 = 700;
g.fillRect(x1, y1-100, 50, 150);
g.setColor(Color.orange);
x1 = 375;
y1 = 700;
g.fillRect(x1, y1-100, 50, 150);
g.setColor(Color.yellow);
x1 = 475;
y1 = 700;
g.fillRect(x1, y1-100, 50, 150);
g.setColor(Color.red);
x1 = 575;
y1 = 700;
g.fillRect(x1, y1-100, 50, 150);
g.setColor(Color.pink);
x1 = 675;
y1 = 700;
g.fillRect(x1, y1-150, 50, 200);
drawCourseGradesGraph = false;
}
}
// Event Handler Code, for each button clicked, set flag to true and corresponding draw graph
public void actionPerformed( ActionEvent e )
{
if (e.getSource() == quizButton)
{
drawQuizGraph = true;
repaint();
}
else if (e.getSource() == homeworkButton)
{
drawHomeworkGraph = true;
repaint();
}
else if (e.getSource() == midtermButton)
{
drawMidtermGraph = true;
repaint();
}
else if (e.getSource() == finalprojectButton)
{
drawFinalProjectGraph = true;
repaint();
}
else if (e.getSource() == coursescoreButton)
{
drawCourseAveragesGraph = true;
repaint();
}
else if (e.getSource() == coursegradeButton)
{
drawCourseGradesGraph = true;
repaint();
}
}
}