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

I need help writting this java program.. You must hand in a single file named Et

ID: 3804417 • Letter: I

Question

I need help writting this java program..

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

Here is the code that needs written:

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Etch_A_Sketch implements MouseListener, MouseMotionListener {

JFrame window;
Container content;
int mouseX,mouseY,oldX,oldY;
JLabel coords;
JButton[] colors = new JButton[5];
Color lineColor;

public Etch_A_Sketch() {
JFrame window = new JFrame("Classic Etch a Sketch");
content = window.getContentPane();
content.setLayout( new FlowLayout() );   
ButtonListener listener = new ButtonListener();
colors[0] = new JButton("Red");
colors[0].addActionListener(listener);
colors[1] = new JButton("Yellow");
colors[1].addActionListener(listener);
colors[2] = new JButton("Black");
colors[2].addActionListener(listener);
colors[3] = new JButton("Blue");
colors[3].addActionListener(listener);
colors[4] = new JButton("Pink");
colors[4].addActionListener(listener);
lineColor = Color.red;
  
for (int i = 0; i < 5; i++)
content.add(colors[i]);
content.addMouseListener(this);
content.addMouseMotionListener(this);
window.setSize(640,480);
window.setVisible(true);
}


// ..............................................................
// IMPLEMENTING MOUSELISTENER REQUIRES YOU TO WRITE (OVER-RIDE) THESE METHODS

public void mouseClicked( MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
//repaint();   
}
public void mousePressed( MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
//repaint();
}

public void mouseReleased( MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
//repaint();
}

public void mouseExited( MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
//repaint();
}
public void mouseEntered( MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
//repaint();
}

// ...............................................................
// IMPLEMENTING MOUSEMOTIONLISTENER REQUIRES YOU WRITE (OVER-RIDE) THESE METHODS

public void mouseDragged( MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();

if (oldX ==0 ) {
oldX=mouseX;
oldY=mouseY;
return;
}
// drawPoint( x,y );
  
Graphics g = content.getGraphics();
g.setColor(lineColor);
g.drawLine( oldX,oldY, mouseX, mouseY );

oldX = mouseX;
oldY = mouseY;
//repaint();
}
public void mouseMoved( MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
//repaint();
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Component whichButton = (Component) e.getSource();

if (whichButton == colors[0])
lineColor = Color.red;
  
if (whichButton == colors[1])
lineColor = Color.yellow;

if (whichButton == colors[2])
lineColor = Color.black;
  
if (whichButton == colors[3])
lineColor = Color.blue;
  
if (whichButton == colors[4])
lineColor = Color.pink;
}
}
public static void main( String[] args) {
new Etch_A_Sketch();
}
}