Implement an interactive graphics program which allows the user to draw dots via
ID: 642056 • Letter: I
Question
Implement an interactive graphics program which allows the user to draw dots via the mouse device. Specifically, the program should draw the trajectory of the mouse motion in form of independent dots when the left mouse button is pressed [20 points]. The dots should be colored in red. The program should stop drawing the trajectory when the button is released [10 points].
Repeat the button press [20 points] and the button release [10 points] process for the right mouse button. This time the dots should be colored in blue.
Error checking: The dots should not be overlaid on each other [20 points].
Submit the source files (html and JavaScript).
Explanation / Answer
Mouse.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Mouse extends Applet implements MouseListener
{
int sty,stx;
int endx,endy;
public void init()
{
addMouseListener(this);
}
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 paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(stx,sty,endx,endy);
}
}
/*
Mouse.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Mouse extends Applet implements MouseListener
{
int sty,stx;
int endx,endy;
public void init()
{
addMouseListener(this);
}
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 paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(stx,sty,endx,endy);
}
}
/*<applet code="Mouse.class" width=400 height = 400> </applet> */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.