package elte.java2_utikalauz5.reflect; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.lang.reflect.*; /** Egy nagyobb példa. @link.forrásfájl {@docRoot}/../data/reflect/src Shapes.java @link.letöltés {@docRoot}/../data/reflect Shapes.jar @since Java 2 Útikalauz programozóknak 5.0 */ public class Shapes extends JFrame { /** Verziószám. */ private final static long serialVersionUID = 15L; private JPanel canvas; private JTextField sName; private JTextField sX; private JTextField sY; private JTextField sWidth; private JTextField sHeight; public Shapes() { getContentPane().setLayout(new BorderLayout()); createCanvas(); createUI(); createMenu(); } protected void createCanvas() { canvas = new JPanel(); canvas.setSize(new Dimension(500, 400)); canvas.setLayout(new CanvasLayout()); canvas.setMinimumSize(new Dimension(500, 400)); canvas.setPreferredSize(new Dimension(600, 500)); getContentPane().add(canvas, BorderLayout.CENTER); canvas.repaint(); } public void createUI() { JPanel ui = new JPanel(); ui.add(new JLabel("Name:")); sName = new JTextField(10); ui.add(sName); ui.add(new JLabel("x:")); sX = new JTextField(3); ui.add(sX); ui.add(new JLabel("y:")); sY = new JTextField(3); ui.add(sY); ui.add(new JLabel("width:")); sWidth = new JTextField(3); ui.add(sWidth); ui.add(new JLabel("height:")); sHeight = new JTextField(3); ui.add(sHeight); ClearAction ca = new ClearAction(); JButton create = new JButton("Create"); ui.add(create); create.setDefaultCapable(true); create.addActionListener(ca); create.addActionListener(new CreateAction()); JButton clear = new JButton("Clear"); ui.add(clear); clear.addActionListener(ca); getContentPane().add(ui, BorderLayout.SOUTH); } protected void createMenu() { JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); menuBar.add(menu); setJMenuBar(menuBar); menu.add(new DumpAction("dump shapes")); menu.addSeparator(); menu.add(new AbstractAction("Exit") { /** Verziószám. */ private final static long serialVersionUID = 15L; public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public static void main(String[] args) { Shapes frame = new Shapes(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.pack(); frame.setVisible(true); } protected class ClearAction extends AbstractAction { /** Verziószám. */ private final static long serialVersionUID = 15L; public void actionPerformed(ActionEvent e) { sName.setText(""); sX.setText(""); sY.setText(""); sWidth.setText(""); sHeight.setText(""); } } // ClearAction protected class CreateAction extends AbstractAction { /** Verziószám. */ private final static long serialVersionUID = 15L; protected JComponent createShape(String name) { JComponent s = null; try { Class shapeClass = Class.forName(name); Constructor shapeConstructor = shapeClass.getConstructor(); s = (JComponent)shapeConstructor. newInstance(); } catch(Exception e) { System.err.println(e); } return s; } protected void setBoundsShape(Object s, int x, int y, int w, int h) { try { Class shapeClass = s.getClass(); Class parTypes[] = {Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE}; Method setBoundsMethod = shapeClass.getMethod( "setBounds", parTypes); Object parameters[] = {new Integer(x), new Integer(y), new Integer(w), new Integer(h)}; setBoundsMethod.invoke(s, parameters); } catch(Exception e) { System.err.println(e); } } public void actionPerformed(ActionEvent e) { String name = sName.getText(); int x, y, width, height; try { x = Integer.parseInt(sX.getText()); y = Integer.parseInt(sY.getText()); width = Integer.parseInt(sWidth.getText()); height = Integer.parseInt(sHeight.getText()); } catch(NumberFormatException ex) { System.err.println("please numbers!"); return; } if(x < 0) x = 0; if(x > 1000) x = 1000; if(y < 0) y = 0; if(y > 500) y = 500; if(width < 10) width = 10; if(width > 1000) width = 1000; if(height < 10) height = 10; if(height > 500) height = 500; JComponent s = createShape(name); if(s == null) return; setBoundsShape(s, x, y, width, height); s.setToolTipText(name); canvas.add(s); canvas.repaint(); } } // CreateAction protected class DumpAction extends AbstractAction { /** Verziószám. */ private final static long serialVersionUID = 15L; public DumpAction(String name) { super(name); } public void actionPerformed(ActionEvent e) { System.out.println("Classes:"); Component cs[] = canvas.getComponents(); for(int i = 0; i < cs.length; i++) { Class cClass = cs[i].getClass(); System.out.println(" " + cClass.getName()); } } } // DumpAction class CanvasLayout implements LayoutManager { public void addLayoutComponent(String name, Component comp) {} public void layoutContainer(Container parent) { } public Dimension minimumLayoutSize(Container parent) { Component[] components = parent.getComponents(); Dimension min = new Dimension(0, 0); Point location = new Point(); Dimension cmin; // calculating the minimum size using absolute coordinates for(int i = 0; i < components.length; i++) { cmin =components[i].getMinimumSize(); components[i].getLocation(location); if(cmin.width + location.x > min.width) { min.width = cmin.width + location.x; } if(cmin.height + location.y > min.height) { min.height = cmin.height + location.y; } } // end for return min; } public Dimension preferredLayoutSize(Container parent) { Component[] components = parent.getComponents(); Dimension pref = new Dimension(0, 0); Point location = new Point(); Dimension cpref; // calculating the preferred size using absolute coordinates for(int i = 0; i < components.length; i++) { cpref =components[i].getPreferredSize(); components[i].getLocation(location); if(cpref.width + location.x > pref.width) { pref.width = cpref.width + location.x; } if(cpref.height + location.y > pref.height) { pref.height = cpref.height + location.y; } } // end for return pref; } public void removeLayoutComponent(Component comp) {} } // CanvasLayout } // Shapes /** Alakzat. */ abstract class AShape extends JComponent { protected RectangularShape shape; protected Rectangle2D shapeBounds; public AShape(RectangularShape theShape) { shape = theShape; shapeBounds = shape.getBounds2D(); Rectangle intBounds = shape.getBounds(); super.setBounds(intBounds.x, intBounds.y, intBounds.width + 1, intBounds.height + 1); Dimension size = new Dimension(intBounds.width, intBounds.height); super.setMinimumSize(size); super.setPreferredSize(size); repaint(); } public boolean contains(int x, int y) { return shape.contains( shapeBounds.getMinX() + x, shapeBounds.getMinY() + y ); } public void setBounds(int x, int y, int w, int h) { super.setBounds(x, y, w, h); ((RectangularShape)shape).setFrame(x, y, w - 1, h - 1); shapeBounds = shape.getBounds2D(); repaint(); } // It assumes that the passed Graphics is an instance of Graphics 2D! protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setColor(getForeground()); g2.setBackground(getBackground()); g2.translate(-shapeBounds.getMinX(), -shapeBounds.getMinY()); if(!isOpaque()) { g2.fill(shape); } g2.draw(shape); } } // AShape /** Ellipszis. */ class AEllipse extends AShape { /** Verziószám. */ private final static long serialVersionUID = 15L; public AEllipse() { super(new Ellipse2D.Double(1, 1, 1, 1)); setForeground(Color.red); } } /** Téglalap. */ class ARectangle extends AShape { /** Verziószám. */ private final static long serialVersionUID = 15L; public ARectangle() { super(new Rectangle2D.Double(1, 1, 1, 1)); setForeground(Color.blue); } } /** Kereksarkú téglalap. */ class ARounded extends AShape { /** Verziószám. */ private final static long serialVersionUID = 15L; public ARounded() { super(new RoundRectangle2D.Double(1, 1, 1, 1, 5, 5)); setForeground(Color.yellow); } }