package elte.java2_utikalauz5.java2d; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; /** peldaprogram az egerrel valo kivalasztasokra. @link.forrásfájl {@docRoot}/../data/java2d/src TalalatVizsgalat.java @link.letöltés {@docRoot}/../data/java2d TalalatVizsgalat.jar @since Java 2 Útikalauz programozóknak */ class TalalatVizsgalat extends Canvas implements MouseListener { /** Verziószám */ private final static long serialVersionUID = 15L; int x, y, selected = 0; boolean first = true; Rectangle2D kekTeglalap = new Rectangle2D.Float (10.0f, 10.0f, 150.0f, 100.0f); Rectangle2D zoldTeglalap = new Rectangle2D.Float (200.0f, 10.0f, 150.0f, 100.0f); public void mouseClicked (MouseEvent e) { } public void mouseEntered (MouseEvent e) { } public void mouseExited (MouseEvent e) { } public void mousePressed (MouseEvent e) { x = e.getX (); y = e.getY (); repaint (); } public void mouseReleased (MouseEvent e) { } public void update (Graphics g) { paint (g); } public void initScreen () { Frame f = new Frame (); f.add (this); addMouseListener (this); f.addWindowListener ( new WindowAdapter () { public void windowClosing (WindowEvent e) { System.exit (0); } } ); f.setSize (500, 300); f.setVisible (true); } public void paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; if (first) { first = false; g2.setColor (Color.blue); g2.fill (kekTeglalap); g2.setColor (Color.green); g2.fill (zoldTeglalap); return; } g2.setColor (Color.white); if (selected == 1) { g2.draw (kekTeglalap); g2.setColor (Color.blue); g2.fill (kekTeglalap); } else if (selected == 2) { g2.draw (zoldTeglalap); g2.setColor (Color.green); g2.fill (zoldTeglalap); } g2.setColor (Color.red); if (kekTeglalap.contains (x, y) == true) { selected = 1; g2.draw (kekTeglalap); } else if (zoldTeglalap.contains (x, y) == true) { selected = 2; g2.draw (zoldTeglalap); } else selected = 0; } public static void main (String[] args) { TalalatVizsgalat t = new TalalatVizsgalat (); t.initScreen (); } }