Preguntas sobre programación simple de JAVA, responda con atención
importar?javax.swing.*;
importar?java.awt.*;
importar?java.awt.event.MouseEvent;
importar?java.awt.event.MouseListener;
importar?java.awt.event.MouseMotionListener;
¿clase?pública?CirclePanel?extends?JPanel?{
privado?int?x;
privado?int?y;
privado?int?radio;
privado?int?mouseDiffX ?=?0;
privado?int?mouseDiffY?=?0;
privado?boolean?activado?=?false;
público?CirclePanel( final?int?x,?final?int?y,?final?int?radius){
super();
this.x?=?x;
este.y?=?y;
este.radio?=?radio;
addMouseListener(new?MouseListener()?{
@Override
public?void?mouseClicked(final?MouseEvent?mouseEvent)?{}
@Override
public?void?mousePressed(final?MouseEvent? mouseEvent)?{
mouseDiffX?=?mouseEvent.getX()?-?CirclePanel.this.x;
mouseDiffY?=?mouseEvent.getY()?-?CirclePanel. this.y;
activado?=?mouseEvent.getPoint().distance(
new?Point(CirclePanel.this.x,?CirclePanel.this.y))?< ?radio;
}
@Override
public?void?mouseReleased(final?MouseEvent?mouseEvent)?{activated?=?false;} p>
@Override
público?void?mouseEntered(final?MouseEvent?mouseEvent)?{}
@Override
public?void?mouseExited (final?MouseEvent?mouseEvent)?{}
});
addMouseMotionListener(new?MouseMotionListener()?{
@Override
public?void?mouseDragged(final?MouseEvent?mouseEvent)?{
if(activado){
CirclePanel.this.x?=?mouseEvent.getX()?- ?mus
eDiffX;
CirclePanel.this.y?=?mouseEvent.getY()?-?mouseDiffY;
paintComponent(CirclePanel.this.getGraphics());
}
}
@Override
public?void?mouseMoved(final?MouseEvent?mouseEvent)?{}
} );
}
@Override
¿protegido?void?paintComponent(final?Graphics?graphics)?{
super.paintComponent (gráficos);
graphics.drawOval(x,?y,?radius,?radius);
}
public?static?void?main( String[]?args)?{
final?JFrame?f?=?new?JFrame("Prueba");
f.setSize(500,?500);
f.setVisible(true);
f.getContentPane().add(new?CirclePanel(40,?40,?50));
f .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}