Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Thanks!! Here is the Starter File you must use: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Etch_A_Sketch implements MouseListener, MouseMotionListener // NOTE multiple interfaces

Thanks!!

image text in transcribedHere is the Starter File you must use:

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Etch_A_Sketch implements MouseListener, MouseMotionListener // NOTE multiple interfaces { JFrame window; Container contentPane; int mouseX,mouseY,oldX,oldY; JLabel coords; Color currDrawingColor = Color.BLACK; // ADD A BUTTON THAT WHEN CLICKED, CHANGES THE CURR COLOR public Etch_A_Sketch() { JFrame window = new JFrame("Classic Etch a Sketch"); contentPane = window.getContentPane(); contentPane.setLayout( new FlowLayout() ); coords = new JLabel(); coords.setFont(new Font("TimesRoman", Font.ITALIC + Font.BOLD, 32)); contentPane.add( coords); contentPane.addMouseListener(this); // "this" is the class that implements that listener contentPane.addMouseMotionListener(this); // "this" is the class that implements that listener window.setSize(640,480); window.setVisible(true); } // .............................................................. // IMPLEMENTING MOUSELISTENER REQUIRES YOU TO WRITE (OVER-RIDE) THESE METHODS //when you press & release with NO MOVEMENT while pressed public void mouseClicked( MouseEvent me) { mouseX = me.getX(); mouseY = me.getY(); reportEventCoords("Mouse clicked at: " + mouseX + "," + mouseY ); } // when you press public void mousePressed( MouseEvent me) { mouseX = me.getX(); mouseY = me.getY(); reportEventCoords("Mouse Pressed at: " + mouseX + "," + mouseY ); //repaint(); } //when you let release after dragging public void mouseReleased( MouseEvent me) { mouseX = me.getX(); mouseY = me.getY(); reportEventCoords("Mouse released at: " + mouseX + "," + mouseY ); //repaint(); } // the mouse just moved off of the JFrame public void mouseExited( MouseEvent me) { mouseX = me.getX(); mouseY = me.getY(); reportEventCoords("Mouse exited at: " + mouseX + "," + mouseY ); //repaint(); } // the mouse just moved onto the JFrame public void mouseEntered( MouseEvent me) { mouseX = me.getX(); mouseY = me.getY(); reportEventCoords("Mouse Entered at: " + mouseX + "," + mouseY ); //repaint(); } // ............................................................... // IMPLEMENTING MOUSEMOTIONLISTENER REQUIRES YOU WRITE (OVER-RIDE) THESE METHODS // mouse is moving while pressed public void mouseDragged( MouseEvent me) { mouseX = me.getX(); mouseY = me.getY(); if (oldX ==0 ) { oldX=mouseX; oldY=mouseY; return; } // draw dot (actually small line segment) between old (x,y) and current (x,y) Graphics g = contentPane.getGraphics(); // use g to draw onto the pane g.setColor( currDrawingColor ); g.drawLine( oldX,oldY, mouseX, mouseY ); oldX = mouseX; oldY = mouseY; reportEventCoords("Mouse Dragged at: " + mouseX + "," + mouseY ); //repaint(); } // moved mouse but not pressed public void mouseMoved( MouseEvent me) { mouseX = me.getX(); mouseY = me.getY(); reportEventCoords("Mouse Moved at: " + mouseX + "," + mouseY ); //repaint(); } // .............................................................. public static void main( String[] args) { new Etch_A_Sketch(); } // a helper utility private void reportEventCoords( String msg ) { coords.setText( msg ); } }//EOF 

Here is JFrame2.java if you need help:

import java.awt.*; import javax.swing.*; import java.awt.event.*; // ACTIONLISTENER INTERFACE public class JFrame2 { public static void main(String [] args) { JFrame window = new JFrame("Events"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // CLICKING THE X WILL KILL THE APP NOW // JBUTTON IS AN -ACTIVE- CONTROL. CAN PRE PROGRAMMED TO REACT TO BEING CLICKED VIA ACTIONLISTENER JButton button = new JButton("Click Me to change my color"); button.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 60)); ActionListener listener = new MyListener(); button.addActionListener(listener); Container content = window.getContentPane(); content.setLayout(new FlowLayout()); content.add(button); window.setSize(640,480); window.setVisible(true); } }// END CLASS JFRAME2 /* ################################################################################################### FOR THE FIRST TIME YOU ARE USING A JAVA FILE THAT CONTAINS MORE THAN ONE CLASS DEFINITION. WE NEED THIS CLASS BECUASE WE WANT TO WRITE SOME CODE THAT GETS TRIGGERED EVERY TIME A JBUTTON OUT IN MAIN GETS CLICKED. WE NEED TO IMPLEMENT AN INTERFACE THAT ALLOWS US TO CONTROL WHAT HAPPENS WHEN OUR JBUTTON IS CLICKED. THIS CLASS IS/MUST BE PRIVATE SINCE YOU ARE ONLY ALLOWED TO HAVE ONE PUBLIC CLASS AT THE TOP LEVEL IN A SOURCE FILE. */ class MyListener implements ActionListener // MyListener is JUST THE NAME WE MAKE UP FOR OUR CLASS { // COULD HAVE INITIALIZED IN C'TOR BUT WE DID IT QIK n DIRTY int index = 0; Color[] myColors = {Color.RED, Color.BLUE, Color.CYAN, Color.YELLOW, Color.ORANGE, Color.MAGENTA, Color.GREEN}; /* EACH TIME A JBUTTON IS CLICKED OUT ON THE JFRAME, THIS METHOD IS AUTOMATICALLY CALLED BY THE GUI INTERFACE. SINCE THERE COULD BE MANY JBUTTON OBJECTS ON OUR JFRAME, THE REF TO THE SPECIFIC BUTTON THAT WAS CLICKED IS AUTOMATICALLY PASSED IN WHEN THIS METHOD IS TRIGGERED BY THE CLICK. THIS IS WHERE WE WRITE OUR CODE THAT WE WANT TO BE EXECUTED EVERY TIME A JBUTTON IS CLICKED */ public void actionPerformed(ActionEvent e) { Component refToJButtonThatWasClicked = (Component) e.getSource(); refToJButtonThatWasClicked.setForeground(myColors[index]); index = (index + 1) % myColors.length; refToJButtonThatWasClicked.setBackground(myColors[index]); } } 
You are to enhance the Etch_A_Sketch.java program as follows: Get rid of the x,y coordinate reporting. . Add a JButton the user can click to change the drawing color. (hint read JFrame2.java) a simple JButton that when clicked, toggles between 4 colors is sufficient Be sure you understand the difference between changing the color of a button's background vs. changing the current drawing color of the Graphics object. 89 90 91 92 93 94 95 96 // draw dot (actually small line segment) between old (x,y) and current (x, y) Graphics g= contentPane.getGraphics(); // use g to draw onto the pane g. SetColor ( currDrawingColor - this global (member) var gets changed in your actionPerformed code g.drawLine ( oldx,oldY, mouseX, mouseY) o1dX = mouseX ; oldY mouseY ; reportEventCoords ("Mouse Dragged at: "mouseX + ","mouseY everv time vou click the new Button vou will add get rid of all these calls You are to enhance the Etch_A_Sketch.java program as follows: Get rid of the x,y coordinate reporting. . Add a JButton the user can click to change the drawing color. (hint read JFrame2.java) a simple JButton that when clicked, toggles between 4 colors is sufficient Be sure you understand the difference between changing the color of a button's background vs. changing the current drawing color of the Graphics object. 89 90 91 92 93 94 95 96 // draw dot (actually small line segment) between old (x,y) and current (x, y) Graphics g= contentPane.getGraphics(); // use g to draw onto the pane g. SetColor ( currDrawingColor - this global (member) var gets changed in your actionPerformed code g.drawLine ( oldx,oldY, mouseX, mouseY) o1dX = mouseX ; oldY mouseY ; reportEventCoords ("Mouse Dragged at: "mouseX + ","mouseY everv time vou click the new Button vou will add get rid of all these calls

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions

Question

What is a verb?

Answered: 1 week ago