/* * FigureWindow.java * */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.Graphics.*; public class FigureWindow extends JPanel{ // components of the panel FigurePanel mFigurePanel; // Constructor public FigureWindow(){ super(new BorderLayout()); // calls the constructor of JPanel with BorderLayout // add components to the FigureWindow container mFigurePanel = new FigurePanel(); add( mFigurePanel, BorderLayout.CENTER); } // HOOFDPROGRAMMA public static void main( String[] args) { // called at program start JFrame frame = new JFrame("FigureWindow"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when cross at top right is pressed FigureWindow window = new FigureWindow(); frame.setContentPane(window); // figuren toevoegen aan de panel Quadrangle q = new Quadrangle(50, 50, 30); window.mFigurePanel.AddFigure(q); Rectangle r = new Rectangle(150, 100, 50, 30); window.mFigurePanel.AddFigure(r); // Circle c = new Circle(50, 80, 30); // window.mFigurePanel.AddFigure(c); frame.pack(); // berekent grootte vd frame adhv groottes vd componenten (preferred sizes) frame.show(); // should be the last operation } // inner class class FigurePanel extends JPanel { Figure mFigures[];// array final int MAX_NBR_FIGURES = 20; int mNbrFigures; FigurePanel() { setBackground(Color.white); setPreferredSize( new Dimension(400, 400)); // constructor: Dimension(int width, int height); mNbrFigures = 0; mFigures = new Figure[MAX_NBR_FIGURES]; // aanmaken array enableEvents(AWTEvent.MOUSE_EVENT_MASK); // noodzakelijk om processMouseEvent mogelijk te maken } void AddFigure(Figure figure){ mFigures[mNbrFigures] = figure; mNbrFigures++; // + 1 } // overriding the processMouseEvent of JPanel to handle the mouse events public void processMouseEvent(MouseEvent e) { if (e.getID() == MouseEvent.MOUSE_CLICKED) { int x = e.getX(); int y = e.getY(); System.out.println("Button " + e.getButton() + " clicked in panel at point (" + x + ", " + y + ") mod:"+e.getModifiers() +" BUTTON3_MASK "+MouseEvent.BUTTON3_MASK ); repaint(); // om het geheel te hertekenen } } public void paintComponent(Graphics g) { g.clearRect(0, 0, 400, 400); // clear screen super.paintComponent(g); // to draw background colors etc for (int i = 0; i < mNbrFigures; i++) mFigures[i].Draw(g); } } }