/* * EventHandlingFrameNoLayout.java * similar to EventHandlingFrame.java, except lines indicated with *$* */ import java.awt.*; import java.awt.event.*; import javax.swing.*; class EventHandlingFrameNoLayout extends JFrame { DrawingPanel mPanel; JButton mResetButton, mQuitButton, mEnlargeButton; JComboBox mComboBox; EventHandlingFrameNoLayout(String title) { super(title); setSize(400, 425); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when cross at top right is pressed Container container = getContentPane(); container.setLayout(null); // *$* no layout mPanel = new DrawingPanel(); mPanel.setBounds(0, 0, 400, 300); // *$* for position & size container.add(mPanel); // *$* Color colors[] = {Color.red, Color.blue, Color.black, Color.yellow}; mComboBox = new JComboBox(colors); // the colors are shown by string descriptions mComboBox.setBounds(180, 310, 200, 30); // *$* container.add(mComboBox); // *$* mEnlargeButton = new JButton("Enlarge Figure"); mEnlargeButton.setBounds(30, 310, 120, 30); // *$* container.add(mEnlargeButton); // *$* mResetButton = new JButton("Reset"); mResetButton.setBounds(40, 350, 100, 30); // *$* container.add(mResetButton); // *$* mQuitButton = new JButton("Quit"); mQuitButton.setBounds(230, 350, 100, 30); // *$* container.add(mQuitButton); // *$* // EVENT HANDLING // ** Event Delegation Method 2a. ** mEnlargeButton.addMouseListener( new EnlargeButtonListener() ); // ** Event Delegation Method 2b. ** mResetButton.addMouseListener(mPanel); // the DrawingPanel acts as a listener // ** Event Delegation Method 2c. ** mComboBox.addActionListener(new ActionListener() { // instant listener object public void actionPerformed(ActionEvent e) { mPanel.SetColor( (Color) mComboBox.getSelectedItem() ); repaint(); } }); // ** Event Delegation Method 3a. ** mQuitButton.addMouseListener( new QuitButtonListener() ); // ** Event Delegation Method 3b. ** mResetButton.addMouseListener( new MouseAdapter(){ // instant adapter object public void mouseEntered(MouseEvent e){ System.out.println("Press to reset the panel..."); } }); } public static void main(String s[]) { EventHandlingFrameNoLayout frame = new EventHandlingFrameNoLayout("Eventhandling frame without layout"); // puts window in the middle of the screen Dimension frameSize = frame.getSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation(screenSize.width / 2 - frameSize.width / 2, screenSize.height / 2 - frameSize.height / 2); frame.show(); } // ** Event Delegation Method 2a. ** // Listener for mEnlargeButton class EnlargeButtonListener implements MouseListener{ public void mouseClicked(MouseEvent e) { mPanel.mSize += 10; System.out.println("Button "+e.getButton()+" clicked: new size = "+mPanel.mSize); } public void mouseEntered(MouseEvent e) {} // empty methods public void mouseExited(MouseEvent e) {} // but MUST be implemented public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e){} } // ** Event Delegation Method 3a. ** // Listener for mQuitButton class QuitButtonListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { System.exit(0); } } /* * DrawingPanel.java * */ class DrawingPanel extends JPanel implements MouseListener { int mX0, mY0, mSize; Color mColor = Color.red ; // default = red boolean mReset= true; public DrawingPanel(){ mX0 = 100; mY0 = 100; mSize = 40; setBackground(Color.white); enableEvents(AWTEvent.MOUSE_EVENT_MASK); // noodzakelijk om processMouseEvent mogelijk te maken } // ** Event Delegation Method 1. ** // I override the processMouseEvent of JPanel to handle the mouse events public void processMouseEvent(MouseEvent e) { if (e.getID() == MouseEvent.MOUSE_CLICKED) { // remember the coordinates mX0 = e.getX(); mY0 = e.getY(); repaint(); } } public void paintComponent(Graphics g) { super.paintComponent(g); if (mReset){ g.clearRect(0, 0, 400, 400); // clear screen mReset = false; } else { g.setColor(mColor); g.drawOval(mX0-mSize/2, mY0-mSize/2, mSize, mSize); } } void SetColor(Color c){ mColor = c; repaint(); } // ** Event Delegation Method 2b. ** // implementation of ALL MouseListener methods public void mouseClicked(MouseEvent e) { mReset = true; repaint(); } public void mouseEntered(MouseEvent e) {} // empty methods public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e){} } }