¿Cómo escribir un pequeño programa para dibujar gráficos en Java?
importar java.awt.*;
importar java.awt.event.*
importar java.awt.geom.*;
import javax.swing.*;
//Dibujo de formas irregulares
clase pública IrregularShapeDemo extiende JFrame {
GeneralPath gPath= new GeneralPath( ) ; // Instancia de objeto GeneralPath
Point aPoint;
//Constructor
public IrregularShapeDemo() {
super(" Dibujo de gráficos irregulares"); //Llamar al constructor de la clase principal
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //Permitir eventos
setSize(300, 200); // Establecer el tamaño de la ventana
setVisible(true); //Establecer la visibilidad de la ventana
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Salir del programa al cerrar la ventana
}
public void paint(Graphics g) { //Sobrecarga el método paint() del componente de ventana
Graphics2D g2D = (Graphics2D)g //Obtiene el entorno de gráficos
g2D.draw(gPath); //Dibujar ruta
}
public static void main(String[] args) {
new IrregularShapeDemo();
}
protected void ProcessMouseEvent(MouseEvent e) { //Procesamiento de eventos del mouse
if(e.getID() == MouseEvent .MOUSE_PRESSED) {
aPoint = e.getPoint(); //Obtener el punto actual del mouse
gPath = new GeneralPath() //Volver a crear una instancia del objeto GeneralPath
p>gPath.moveTo(aPoint.x, aPoint.y); //Establecer punto de ruta
}
}
proceso vacío protegidoMouseMotionEvent( MouseEvent e ) { //Procesamiento de eventos de movimiento del mouse
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); punto actual del mouse
gPath.lineTo(aPoint.x, aPoint.y); //Establece la ruta
gPath.moveTo(aPoint.x, aPoint.y); p>
repaint(); //Redibujar componente
}
}
}<
/p>