In this AWT Video, we will prepare our frame window with the controls. Then we will handle the button clicks. Here we will set the Drawing Modes (Line Draw/ Rect Draw/Free-Hand).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
//Sample 01: Implement the Listeneres public class FrameWin extends Frame implements WindowListener, ActionListener, MouseListener, MouseMotionListener { //Sample 02: Member Variables String DrawMode = ""; Point FirstPoint = new Point(0,0); Point SecondPoint = new Point(0,0); Label lblModeDisplay; Checkbox chkDragMode; //Sample 03: Prepare Command Panel Panel CommandPanel = new Panel(new GridLayout(6,1)); CommandPanel.setBackground(new Color(240, 240, 240)); //3.1 Prepare Button & Checkbox Button btnDrawLine = new Button("Draw Line"); Button btnDrawRect = new Button("Draw Rect"); Button btnFreeHand = new Button("Free Hand"); btnDrawLine.setActionCommand("LineD"); btnDrawRect.setActionCommand("RectD"); btnFreeHand.setActionCommand("FreeH"); btnDrawLine.addActionListener(this); btnDrawRect.addActionListener(this); btnFreeHand.addActionListener(this); chkDragMode = new Checkbox("Rubber Band"); //3.2 Prepare Labels Label lblMode = new Label("Mode"); lblModeDisplay = new Label(); Font f1 = new Font("Verdana", Font.PLAIN, 20); lblMode.setFont(f1); lblModeDisplay.setFont(f1); lblModeDisplay.setForeground(Color.BLUE); //3.3 Pack the Panel & give to Frame CommandPanel.add(btnDrawLine); CommandPanel.add(btnDrawRect); CommandPanel.add(btnFreeHand); CommandPanel.add(chkDragMode); CommandPanel.add(lblMode); CommandPanel.add(lblModeDisplay); add(BorderLayout.EAST, CommandPanel); } |
1 2 3 4 5 6 7 |
public void actionPerformed(ActionEvent e) { //Sample 04: Set the Draw Mode DrawMode = e.getActionCommand(); lblModeDisplay.setText(DrawMode); FirstPoint.setLocation(0, 0); SecondPoint.setLocation(0, 0); } |
- AWT Drawing – Part 0 – Prepare Frame Window #22
- AWT Drawing – Part 2 – Perform Line Drawing with Mouse #24
Categories: AWT-Tube