Programming project
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package gradeStudentWork; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; public class FinalDrawingViewer extends JFrame{ public static void main(String[] args) { //set up the frame JFrame frame = new JFrame(); frame.setSize(300, 550); frame.setTitle("Drawing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //create GUI objects final JButton button = new JButton("Show Chart!"); final JButton button2 = new JButton("Clear Chart!"); //create the drawing object final FinalDrawingComponent chart = new FinalDrawingComponent();//polymorphism in action due to inheritance\ //so that the chart appears on the panel, dimensions can be less than the frame size chart.setPreferredSize(new Dimension(250, 250)); //create the panel and add all GUI and the chart JPanel panel = new JPanel(); //add the grid to the frame panel.add(button); panel.add(button2); panel.add(chart); //add panel to the frame frame.add(panel); frame.setVisible(true); class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { try{ File input = new File("drawing_data.txt"); Scanner in = new Scanner(input); try{ //changes the background color with every click chart.changeColor(); in.nextLine(); while(in.hasNextLine()){ String line = in.nextLine(); //data is split by a single space String [] lineContent = line.split(" "); //passing data [(x,y) position of Points to draw] //to the Component through appendValues method chart.appendValues(Integer.parseInt(lineContent[0]),Integer.parseInt(lineContent[1])); } } finally{ in.close(); } } catch(IOException e){ System.out.println("error file"); } } } ActionListener listener1 = new ButtonListener(); button.addActionListener(listener1); } }