Painter.java

a simple paint program


Painter.java

// Painter.java
// Using class MouseMotionAdapter.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class PainterPanel extends JPanel {
   private int xValue = -10, yValue = -10;

   public PainterPanel()
   {
      add(
         new Label( "Drag the mouse to draw" ),
         BorderLayout.SOUTH );

      addMouseMotionListener(
         new MouseMotionAdapter() {
            public void mouseDragged( MouseEvent e )
            {
               xValue = e.getX();
               yValue = e.getY();
               repaint();
            }           
         }
      );
   }

   public void paint( Graphics g )
   {
      g.fillOval( xValue, yValue, 4, 4 );
   }
}

class mainThread implements Runnable
{
    public void run()
    {
	PainterPanel panel = new PainterPanel();
	JFrame frame = new JFrame("A simple paint program");
	frame.add(panel);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setSize( 360, 220 );
	frame.setVisible(true);
    }
}


public class Painter 
{
    public static void main( String args[] )
    {
	EventQueue.invokeLater(new mainThread());
    }
}


Maintained by John Loomis, updated Sun Feb 17 19:14:25 2019