Hello, I have a question about Java. I am working on the following code (shown below). For the mouseMoved event the coordinates must be displayed using black characters, and for the mouseDragged...


Hello, I have a question about Java. I am working on the following code (shown below). For the mouseMoved event the coordinates must be displayed using black characters, and for the mouseDragged event, the coordinates must be displayed using red characters. How can I do that? I would really appreciate your help. Thanks in advance!




// Proj08 file

import java.awt.Frame;

public class Proj08 {

  public static void main(String[] args){

    Frame aFrame = new Proj08Runner();

  }//end main

}//end class Proj08




import java.awt.*;

import java.awt.event.*;






// Proj08Runner file

class Proj08Runner extends Frame {



    GUI gui = new GUI();



    class MyFrame extends Frame {


        int xCoor;

        int yCoor;


        MyFrame() {//constructor



            this.setTitle("Gretchen Suarez");

            this.setSize(300,100);



        } //end constructor


        public void paint(Graphics g) {



            //display coordinate information on the Frame

            g.drawString("" + xCoor + ", " + yCoor, xCoor, yCoor);



        }//end paint()



    }//end class MyFrame



    class GUI {



        public GUI() {//constructor



            //Create a visual object of type MyFrame named Frame1

            MyFrame myFrame = new MyFrame();

            myFrame.setVisible(true);


            //Instantiate and register Listener object which will

            // terminate the program when the user closes

            // the Frame.

            WProc1 winProcCmd1 = new WProc1();

            myFrame.addWindowListener(winProcCmd1);


            //Instantiate and register Listener object which will

            // process mouseMoved events to display coordinate

            // information on the Frame object named myFrame1.

            MyMouseMotionProcessor mouseMotionProc =

            new MyMouseMotionProcessor(myFrame);

            myFrame.addMouseMotionListener(mouseMotionProc);


        }//end constructor



    }//end class GUI definition



    //This listener class monitors for mouseMove events and

    // displays the coordinates of the mouse pointer when the

    // mouse is moved inside the client area of the Frame.

    class MyMouseMotionProcessor extends MouseMotionAdapter {



        MyFrame refToFrame; //save references to the Frame


        //Constructor

        MyMouseMotionProcessor(MyFrame inFrame) {



            //save incoming object reference

            refToFrame = inFrame;



        }// end constructor


        public void mouseMoved(MouseEvent e) {



            //Get X and Y coordinates of mouse pointer and store

            // in instance variables of the Frame object

            refToFrame.xCoor = e.getX();

            refToFrame.yCoor = e.getY();

            //Force a repaint to display the coordinate information

            refToFrame.repaint();



        }//end mouseMoved()



        public void mouseDragged(MouseEvent e) {



            //Get X and Y coordinates of mouse pointer and store

            // in instance variables of the Frame object

            refToFrame.xCoor = e.getX();

            refToFrame.yCoor = e.getY();

            //Force a repaint to display the coordinate information

            refToFrame.repaint();



        }//end mouseMoved()


    }//end class MyMouseMotionProcessor



    //The following listener is used to terminate the program

    // when the user closes the frame object.

    class WProc1 extends WindowAdapter {



        public void windowOpened(WindowEvent e) {



            System.out.println("\nI certify that this program is my own work" +

                               "\nand is not the work of others. I agree not" +

                               "\nto share my solution with others." +

                               "\nGretchen Suarez\n");

        }



        public void windowClosing(WindowEvent e) {



            System.exit(0);



        }//end windowClosing()



    }//end class WProc1



}



Jun 04, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here