In this AWT Tutorial, we will perform rubber banding of Line and Rectangle drawing while dragging the mouse. We will also see setXORMode() and its usage while performing the rubber banding.
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
package AwtDemoPkg; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Checkbox; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Label; import java.awt.Panel; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //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; public FrameWin(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(600, 300); setLocation(100,100); addWindowListener(this); //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); //Sample 07: Register with Mouse Listener addMouseListener(this); //Sample 12: Register Mouse Motion Listener addMouseMotionListener(this); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } public void mouseDragged(MouseEvent e) { //Sample 14: Perform Free Hand Drawing if (DrawMode.compareTo("FreeH") == 0) { if (SecondPoint.x != 0 && SecondPoint.y != 0) { FirstPoint.x = SecondPoint.x; FirstPoint.y = SecondPoint.y; } SecondPoint.setLocation(e.getX(), e.getY()); paint(getGraphics()); } //Sample 15: Draw Line Rubber-Banding if(DrawMode.compareTo("LineD") == 0 && chkDragMode.getState() == true) { Graphics gr = getGraphics(); gr.setColor(Color.LIGHT_GRAY); //Sample 16: Resolve Rubber Banding Issue => if (SecondPoint.x != 0 && SecondPoint.y != 0) { gr.setXORMode(getBackground()); gr.drawLine( FirstPoint.x, FirstPoint.y, SecondPoint.x, SecondPoint.y); } //Sample 16 <= SecondPoint.setLocation(e.getX(), e.getY()); paint(gr); } //Sample 17: Draw Rect Rubber-Banding if(DrawMode.compareTo("RectD") == 0 && chkDragMode.getState() == true) { Graphics gr = getGraphics(); gr.setColor(Color.LIGHT_GRAY); if (SecondPoint.x != 0 && SecondPoint.y != 0) { gr.setXORMode(getBackground()); Point TopLeft = GetTopLeft(); int width = Math.abs(SecondPoint.x - FirstPoint.x); int height = Math.abs(SecondPoint.y - FirstPoint.y); gr.drawRect(TopLeft.x, TopLeft.y, width, height); } SecondPoint.setLocation(e.getX(), e.getY()); paint(gr); } } public void mouseMoved(MouseEvent e) { } public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) { //Sample 05: Set First Point FirstPoint.setLocation(0, 0); SecondPoint.setLocation(0, 0); FirstPoint.setLocation(e.getX(), e.getY()); } public void mouseReleased(MouseEvent e) { //Sample 06: Set Second Point //Sample 13: Introduce the Condition if (DrawMode.compareTo("FreeH") != 0) { SecondPoint.setLocation(e.getX(), e.getY()); repaint(); } } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} 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); } public void paint(Graphics g) { if (FirstPoint.equals(SecondPoint)) return; switch(DrawMode) { case "LineD": //Sample 08: Draw Line g.drawLine( FirstPoint.x, FirstPoint.y, SecondPoint.x, SecondPoint.y); break; case "RectD": //Sample 10: Draw Rectangle Point TopLeft = GetTopLeft(); int width = Math.abs(SecondPoint.x - FirstPoint.x); int height = Math.abs(SecondPoint.y - FirstPoint.y); g.drawRect(TopLeft.x, TopLeft.y, width, height); break; case "FreeH": //Sample 11: Free Hand Drawing g.drawLine( FirstPoint.x, FirstPoint.y, SecondPoint.x, SecondPoint.y); break; } } //Sample 09: Relocate Rectangle Start Point private Point GetTopLeft() { Point TopLeft = new Point(); TopLeft.x = Math.min(FirstPoint.x,SecondPoint.x ); TopLeft.y = Math.min(FirstPoint.y,SecondPoint.y ); return TopLeft; } } |
Categories: AWT-Tube