Java AWT Graphics paint mode

Description

Java AWT Graphics paint mode

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JPanel;

class DrawPanel extends JPanel {
   int chsX = 100, chsY = 100;

   public DrawPanel() {

      super.addMouseMotionListener(new MouseMotionAdapter() {
         public void mouseMoved(MouseEvent me) {
            int x = me.getX();
            int y = me.getY();
            chsX = x - 10;/*  w  ww  .j a v  a2s  . c o  m*/
            chsY = y - 10;
            repaint();
         }
      });
   }

   public void paintComponent(Graphics g) {
      g.drawLine(10, 0, 100, 100);
      g.drawLine(10, 100, 100, 0);
      g.setColor(Color.blue);
      g.drawLine(40, 25, 250, 180);
      g.drawLine(75, 90, 400, 400);
      g.setColor(Color.green);
      g.drawRect(10, 10, 60, 50);
      g.fillRect(10, 10, 60, 50);
      g.setColor(Color.red);
      g.drawRoundRect(90, 10, 60, 50, 15, 15);
      g.fillRoundRect(70, 90, 140, 100, 30, 40);
      g.setColor(Color.cyan);
      g.drawLine(20, 150, 400, 40);
      g.drawLine(50, 290, 80, 19);

      // xor cross hairs
      g.setXORMode(Color.black);
      g.drawLine(chsX-10, chsY, chsX+10, chsY);
      g.drawLine(chsX, chsY-10, chsX, chsY+10);
      g.setPaintMode();  

   }
}

public class Main {
   public static void main(String[] args) {
      DrawPanel panel = new DrawPanel();

      JFrame application = new JFrame();

      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      application.add(panel);
      application.setSize(250, 250);
      application.setVisible(true);
   }
}



PreviousNext

Related