JAVA: For this project, you will create a GUI drawing application. *The user can
ID: 653761 • Letter: J
Question
JAVA: For this project, you will create a GUI drawing application.
*The user can select from three different pen colors.
*The user can also select an "eraser" pen.(Hint: You dont really need to "erase" a point. use a shortcut )
*When the user clicks in the drawing space, the pen is activated. The user can then move the mouse to draw. The user clicks again to de-activate the pen.(Note: the user does not click-and-drag to draw. Instead, it's click once to turn on, move to draw, click once to turn off.)
*There is a clear button that clears the entire drawing space.
*You need to keep track of both the location and color of a point on which the user has drawn. Use an ArrayList to keep track of this.(What kind of object should the ArrayList hold? Does Java provide a class that represents a Point with Color?)
*For full credit, design an object-oriented solution to keep track of an object with a location and a color.
* Include an additional drawing functionality. This must be different from simply choosing another pen color.
Explanation / Answer
/* <applet code="Mouse" width=256 height=256></applet>*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class Mouse extends Applet implements MouseListener{
int sty,stx;
int endx,endy;
Graphics g;
public void init(){addMouseListener(this);add(new Button("Erase")); }
public void mousePressed(MouseEvent e){stx=e.getX();sty=e.getY();}
public void mouseReleased(MouseEvent e){endx=e.getX();endy=e.getY();repaint();}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
/*public void actionPerformed(ActionEvent ae)
{
if (ae.equals("Erase")){g.remove(); }
}*/
public void paint(Graphics g){
g.drawLine(stx,sty,endx,endy);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.