Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help writing this code for a java program. If anyone could help that'll be great! Thanks! Here is the code in text form:

I need help writing this code for a java program. If anyone could help that'll be great! Thanks! image text in transcribed

image text in transcribed

image text in transcribed

Here is the code in text form:

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Etch_A_Sketch implements MouseListener, MouseMotionListener // NOTE multiple interfaces { JFrame window; Container content; int mouseX,mouseY,oldX,oldY; JLabel coords; public Etch_A_Sketch() { JFrame window = new JFrame("Classic Etch a Sketch"); content = window.getContentPane(); content.setLayout( new FlowLayout() ); coords = new JLabel(); coords.setFont(new Font("TimesRoman", Font.ITALIC + Font.BOLD, 32)); content.add( coords); content.addMouseListener(this); // "this" is the class that implements that listener content.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 = content.getGraphics(); // use g to draw onto the pane 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 
You must hand in a single file named Etch A Sketch java for your lab-07 submission. It will not (cannot) be script graded. It will be checked for compilation success at handin time. You are to enhance this Etch A-sketch java program as follows: get rid of the x,y coodinate reporting. add a control that allows the user to change the drawing color (like Paint but can be MUCH simpler). a simple JButton that when clicked, toggles between 4 colors is sufficient

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

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions

Question

Write short notes on Interviews.

Answered: 1 week ago

Question

Define induction and what are its objectives ?

Answered: 1 week ago

Question

Discuss the techniques of job analysis.

Answered: 1 week ago

Question

How do we do subnetting in IPv6?Explain with a suitable example.

Answered: 1 week ago