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

Using the following 2 Java classes (FileObject and SerObject), modify the MouseF

ID: 3738934 • Letter: U

Question

Using the following 2 Java classes (FileObject and SerObject), modify the MouseFrame class to add 3 buttons (clear, save, and load). Clear clears the screen. Save saves the drawing. Load loads the saved drawing.

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.Serializable;

import javax.swing.JButton;

import javax.swing.JFrame;

public class MouseFrame1 extends Frame{

    public MouseFrame1(){

        setSize(500, 500);

        setTitle("Mouse Stuff");

        addWindowListener(new WindowAdapter(){

            public void windowClosing(WindowEvent we){

                System.exit(0);

            }

        });

        add(new MousePanel(), BorderLayout.CENTER);

        setVisible(true);

    }

    public static void main(String[] args){

        new MouseFrame1();

}

}

class MousePanel extends Panel implements MouseListener, MouseMotionListener{

    int lastx, lasty;

    ArrayList<Line>al;

Button b1, b2, b3;

TextField tf;

    Panel p1, p2;

    String newline;

    public MousePanel(){

        addMouseListener(this);

        addMouseMotionListener(this);

        al = new ArrayList<Line>();

        p2 = new Panel();

        b1 = new Button("Save");

        p2.add(b1);

        p2 = new Panel();

        b2 = new Button("Clear");

        p2.add(b2);

        p2 = new Panel();

        b3 = new Button("Load");   

        p2.add(b3);

        

    }

    public void record(int x, int y){

        lastx = x;

        lasty = y;

    }

    public void mouseEntered(MouseEvent me){

        //System.out.println("Mouse Entered");

        record(me.getX(), me.getY());

    }

    public void mouseExited(MouseEvent me){

        //System.out.println("Mouse Exited");

    }

    public void mousePressed(MouseEvent me){

        //System.out.println("Mouse Pressed");

        record(me.getX(), me.getY());

    }

    public void mouseClicked(MouseEvent me){

        //System.out.println("Mouse Clicked");

    }

    public void mouseReleased(MouseEvent me){

        //System.out.println("Mouse Released");

    }

    public void mouseMoved(MouseEvent me){

        //System.out.println("Mouse Moved");

    }

    public void mouseDragged(MouseEvent me){

        //System.out.println("Mouse Dragged");

        int currx = me.getX();

        int curry = me.getY();

        al.add(new Line(lastx, lasty, currx, curry));

        record(currx, curry);

        repaint();

    }

    public void paint(Graphics g){

        for(Line current: al){

            g.drawLine(current.startx, current.starty,

                current.endx, current.endy);

        }

    }

    public void update(Graphics g){

        paint(g);

    }

class Line implements Serializable{

        int startx, starty, endx, endy;

        public Line(int sx, int sy, int ex, int ey){

            startx = sx;

            starty = sy;

            endx = ex;

            endy = ey;

        }

}

public void clearButton(ActionEvent ae){

        

        String temp = tf.getText();

    tf.setText(tf.getText());

    

        b1.addActionListener(new ActionListener(){

    @Override

    public void actionPerformed(ActionEvent ae) {

   TextField tf = (TextField)ae.getSource();

    

    }

        });

        b2.addActionListener(new ActionListener() {

    @Override

    public void actionPerformed(ActionEvent ae) {

    TextField tf = (TextField)ae.getSource();

            

    }

        });

        

        b3.addActionListener(new ActionListener() {

    @Override

    public void actionPerformed(ActionEvent ae) {

    TextField tf = (TextField)ae.getSource();

            

    }

});

}

}














?

Explanation / Answer

import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.io.FileReader; import java.io.FileWriter; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.text.JTextComponent; public class LoadSave { public static void main(String args[]) { final String filename = "text.out"; JFrame frame = new JFrame("Loading/Saving Example"); Container content = frame.getContentPane(); final JTextField textField = new JTextField(); content.add(textField, BorderLayout.NORTH); JPanel panel = new JPanel(); Action loadAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { try { doLoadCommand(textField, filename); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }; loadAction.putValue(Action.NAME, "Load"); JButton loadButton = new JButton(loadAction); panel.add(loadButton); Action saveAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { try { doSaveCommand(textField, filename); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }; saveAction.putValue(Action.NAME, "Save"); JButton saveButton = new JButton(saveAction); panel.add(saveButton); Action clearAction = new AbstractAction() { { putValue(Action.NAME, "Clear"); } public void actionPerformed(ActionEvent e) { textField.setText(""); } }; JButton clearButton = new JButton(clearAction); panel.add(clearButton);