Create an applet which will be interactive (uses listeners by implementing Mouse
ID: 3539097 • Letter: C
Question
Create an applet which will be interactive (uses listeners by implementing MouseListener and MouseMotionListener interfaces or by extending MouseAdapter class).
PART 1)
A Blackboard
Write an applet called Blackboard, which displays and behaves properly and make sure that you drag the mouse for drawing in the applet when it runs:
Here are some required specifications:
To draw in a component there are two techniques:
1. override the component's paint method (or sometimes paintComponent method), and use the Graphics object that's supplied as a parameter to draw with:
public void paint(Graphics page) {
...
page.draw...
...
}
2. use the component's built-in accessor to get the Graphics object:
...
Graphics page = panel.getGraphics();
...
page.draw...
...
Sometimes the second technique is more convenient (e.g. when the paint or paintComponent method isn't appropriate). Use Swing components, whenever possible. For example, your applet should extend JApplet; the button should be a JButton.
You will probably wish to create an HTML file for your applet, for testing purposes. In the applet tag, use a width of 400 and a height of 200.
PART 2)
Make a copy of your Blackboard applet from part 1, and name it as Blackboard1. Modify Blackboard1 applet to extend the MouseAdapter class instead of implementing both MouseListener and MouseMotionListener interfaces.
Explanation / Answer
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Blackboard extends JApplet {
JPanel panel;
JButton clear;
int x1,y1,x2,y2;
public void init() {
//Container container = getContentPane();
x1=0;
x2=0;
y1=0;
y2=0;
panel = new JPanel();
clear = new JButton("Clear");
panel.add(clear);
add(panel);
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
panel.updateUI();
}
});
panel.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
x1 = arg0.getX();
y1 = arg0.getY();
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
panel.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent arg0) {
x2= arg0.getX();
y2= arg0.getY();
paintComponent(panel.getGraphics());
}
});
panel.setLayout(new FlowLayout());
panel.setSize(400, 200);
panel.setBackground(Color.BLACK);
}
protected void paintComponent(Graphics g) {
g.setColor(Color.WHITE);
g.drawLine(x1, y1, x2, y2);
}
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.