Exam the incomplete code program CircleColorChange. This application displays a
ID: 3740399 • Letter: E
Question
Exam the incomplete code program CircleColorChange. This application displays a blue filled circle. If the mous button is button is pressed while over the circle, the circle turns to yellow. When the mouse is released, the circle turns to blue.
A. Write the code that will set the radius of circ to 100. This code replaces comment *A*
B. Write the code that will set the center of the circ to the coordinates (150,150) This code replaces comment *B*
C. Write the code that will register the UpHandler class as a handler for mouse released events on the circle. This code replaces comment *C*
D. Write the code that will register the DownHandler class as a handler for mouse pressed events on the circle. This code replaces comment *D*
E. Write the code that will ad the circle circ t the pane p. This code replaces comment *E*
F. Write the code in the UpHandler class that will change the fill color of the circle circ to COLOR_A. This code replaces comment *F*
G. Write the code in the DownHandler class that will change the fill color of circle circ to COLOR_B. This code replaces comment *G*
The code to finish wasn’t given so write from scratch in Java
Explanation / Answer
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class CircleColorChange extends JFrame implements MouseListener
{
int x1=150,y1=150,r=100; //center at (150,150) radius=100
int X1,Y1;
public CircleColorChange()
{
setSize(1000, 1000);
setVisible(true);
this.addMouseListener(this);
Graphics g=getGraphics();
g.setColor(Color.BLACK);
g.drawOval(x1, y1, 2*r, 2*r); //draw the circle of given radius
g.setColor(Color.BLUE);
g.fillOval(x1, y1, 2*r, 2*r);
}
public static void main(String[] args)
{
new CircleColorChange();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override//change the color yellow when pressed
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub
Graphics g1=getGraphics();
X1=e.getX();
Y1=e.getY();
System.out.println(X1 +" "+Y1);
if(Math.abs(X1-x1)<2*r && Math.abs(Y1-y1)<2*r)
{
g1.setColor(Color.YELLOW);
g1.fillOval(x1, y1, 2*r, 2*r);
}
}
@Override//change the color to blue when released
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
Graphics g2=getGraphics();
X1=e.getX();
Y1=e.getY();
if(Math.abs(X1-x1)<2*r && Math.abs(Y1-y1)<2*r)
{
g2.setColor(Color.BLUE);
g2.fillOval(x1, y1, 2*r, 2*r);
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.