/* * 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; JLabel mLabel1, mLabel2; JComboBox mComboBox; JTextField mTextfield; // Constructor public FigureWindow(){ super(new BorderLayout()); // calls the constructor of JPanel with BorderLayout // add components to FigureWindow's container Box box = Box.createHorizontalBox(); mLabel1 = new JLabel("Kies figuur: "); box.add(mLabel1); String types[] = {"Vierkant", "Rechthoek", "Circel", "Driehoek"}; mComboBox = new JComboBox(types); box.add(mComboBox); mLabel2 = new JLabel("Grootte: "); box.add(mLabel2); mTextfield = new JTextField("10", 5); box.add(mTextfield); add( box, BorderLayout.NORTH); 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); Triangle t = new Triangle(200, 200, 60); window.mFigurePanel.AddFigure(t); 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; // constante 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){ if (mNbrFigures < MAX_NBR_FIGURES){ mFigures[mNbrFigures] = figure; mNbrFigures++; // + 1 } else { JOptionPane.showMessageDialog(this, "Maximum number of figures ("+MAX_NBR_FIGURES+") reached!", "Error adding figures", JOptionPane.ERROR_MESSAGE); } } // 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 + ")"); for (int i = 0; i < mNbrFigures; i++) { if (mFigures[i].IsPointInFigure(x,y)) mFigures[i].mColor = Color.blue; } String item = (String) mComboBox.getSelectedItem(); // cast van Object naar String String text = mTextfield.getText(); int size; try{ size = Integer.parseInt(text); // omzetten van text naar integer } catch (NumberFormatException exception){ JOptionPane.showMessageDialog(this, "Please give a number!", "Error with size", JOptionPane.ERROR_MESSAGE); return; } System.out.println("Selected Item: "+item + "Size: "+size); if (item == "Vierkant"){ Quadrangle q = new Quadrangle(x, y, size); AddFigure(q); } else if (item == "Rechthoek"){ Rectangle r = new Rectangle(x, y, 2*size, size); AddFigure(r); } else if (item == "Circel"){ Circle c = new Circle(x, y, size); AddFigure(c); } else if (item == "Driehoek"){ Triangle t = new Triangle(x, y, size); AddFigure(t); } repaint(); // om het geheel te hertekenen } } public void paintComponent(Graphics g) { super.paintComponent(g); // to draw background colors etc g.clearRect(0, 0, 400, 400); // clear screen for (int i = 0; i < mNbrFigures; i++) mFigures[i].Draw(g); } } }