package elte.java2_utikalauz5.java2d; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; /** peldaprogram az egyszeru elemek hasznalatara. @link.forrásfájl {@docRoot}/../data/java2d/src EgyszeruElemek.java @link.letöltés {@docRoot}/../data/java2d EgyszeruElemek.jar @since Java 2 Útikalauz programozóknak */ class EgyszeruElemek extends Canvas implements ActionListener { /** Verziószám */ private final static long serialVersionUID = 15L; Button gomb1 = new Button ("Ellipszis"); Button gomb2 = new Button ("Koriv"); Button gomb3 = new Button ("Lekerekitett teglalap"); Button gomb4 = new Button ("Teglalap"); Button gomb5 = new Button ("Vonal"); int type = 0; public void initScreen () { Frame f = new Frame (); Panel p = new Panel (); p.setLayout (new FlowLayout ()); p.add (gomb1); p.add (gomb2); p.add (gomb3); p.add (gomb4); p.add (gomb5); f.add (this); f.add ("South", p); gomb1.addActionListener (this); gomb2.addActionListener (this); gomb3.addActionListener (this); gomb4.addActionListener (this); gomb5.addActionListener (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; Ellipse2D ellipszis = new Ellipse2D.Float (250.0f, 100.0f, 50.0f, 100.0f); Arc2D koriv = new Arc2D.Float (250.0f, 100.0f, 50.0f, 100.0f, 0.0f, 120.0f, Arc2D.OPEN); Rectangle2D teglalap = new Rectangle2D.Float (250.0f, 100.0f, 50.0f, 100.0f); RoundRectangle2D lekerekitettTeglalap = new RoundRectangle2D.Float (250.0f, 100.0f, 50.0f, 100.0f, 10.0f, 10.0f); Line2D vonal = new Line2D.Float (250.0f, 100.0f, 300.0f, 200.0f); g2.setColor (Color.blue); switch (type) { case 1: g2.draw (ellipszis); break; case 2: g2.draw (koriv); break; case 3: g2.draw (lekerekitettTeglalap); break; case 4: g2.draw (teglalap); break; case 5: g2.draw (vonal); } } public static void main (String[] args) { EgyszeruElemek e = new EgyszeruElemek (); e.initScreen (); } public void actionPerformed (ActionEvent e) { if (e.getActionCommand ().compareTo ("Ellipszis") == 0) type = 1; else if (e.getActionCommand ().compareTo ("Koriv") == 0) type = 2; else if (e.getActionCommand ().compareTo ("Lekerekitett teglalap") == 0) type = 3; else if (e.getActionCommand ().compareTo ("Teglalap") == 0) type = 4; else type = 5; repaint (); } }