?? rect.java
字號:
package org.merlin.step.nov;import java.awt.*;import java.awt.event.*;import java.util.*;class RectElement implements Element { Rectangle bounds; Color color; RectElement (int x, int y, int w, int h, Color c) { bounds = new Rectangle (x, y, w, h); color = c; } public Rectangle getBounds () { return bounds; } public Element getTranslated (int dX, int dY) { return new RectElement (bounds.x + dX, bounds.y + dY, bounds.width, bounds.height, color); } public void paint (Graphics g) { g.setColor (color); if ((bounds.width == 1) && (bounds.height == 1)) g.drawLine (bounds.x, bounds.y, bounds.x, bounds.y); else g.drawRect (bounds.x, bounds.y, bounds.width - 1, bounds.height - 1); }}public class Rect extends Component implements Tool, ItemListener { static Hashtable colorList = new Hashtable (); static { colorList.put ("white", Color.white); colorList.put ("light grey", Color.lightGray); colorList.put ("grey", Color.gray); colorList.put ("dark grey", Color.darkGray); colorList.put ("black", Color.black); colorList.put ("red", Color.red); colorList.put ("pink", Color.pink); colorList.put ("orange", Color.orange); colorList.put ("yellow", Color.yellow); colorList.put ("green", Color.green); colorList.put ("magenta", Color.magenta); colorList.put ("cyan", Color.cyan); colorList.put ("blue", Color.blue); } public Rect () { enableEvents (AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK); } ObservableList contents; public void setDisplayList (ObservableList l) { contents = l; } public Component getDisplay () { return this; } public Component getControls () { LWContainer controls = new LWContainer (); controls.setLayout (new BorderLayout ()); controls.add ("North", new Label ("Rectangle Tool", Label.CENTER)); Choice colorChoice = new Choice (); Enumeration e = colorList.keys (); while (e.hasMoreElements ()) colorChoice.addItem ((String) e.nextElement ()); colorChoice.select ("black"); controls.add ("Center", colorChoice); colorChoice.addItemListener (this); return controls; } public void dispose () { } public void itemStateChanged (ItemEvent e) { color = (Color) colorList.get (e.getItem ()); } boolean active; Color color = Color.black; int x0, y0, x1, y1; protected void processEvent (AWTEvent e) { super.processEvent (e); if (e instanceof MouseEvent) { MouseEvent m = (MouseEvent) e; switch (m.getID ()) { case MouseEvent.MOUSE_PRESSED: x0 = m.getX (); y0 = m.getY (); break; case MouseEvent.MOUSE_RELEASED: if (active) { x1 = m.getX (); y1 = m.getY (); active = false; contents.addElement (new RectElement (Math.min (x0, x1), Math.min (y0, y1), 1 + Math.abs (x0 - x1), 1 + Math.abs (y0 - y1), color)); } break; case MouseEvent.MOUSE_DRAGGED: int x2 = active ? x1 : x0, y2 = active ? y1 : y0; x1 = m.getX (); y1 = m.getY (); active = true; int xm = Math.min (Math.min (x0, x1), x2), xM = Math.max (Math.max (x0, x1), x2), ym = Math.min (Math.min (y0, y1), y2), yM = Math.max (Math.max (y0, y1), y2); repaint (xm, ym, 1 + xM - xm, 1 + yM - ym); break; } } } public void paint (Graphics g) { if (active) { g.setColor (color); g.drawRect (Math.min (x0, x1), Math.min (y0, y1), Math.abs (x0 - x1), Math.abs (y0 - y1)); } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -