📄 swtutils.java
字号:
tmpGC.setFont(tmpFont); JPanel DUMMY_PANEL = new JPanel(); java.awt.Font tmpAwtFont = new java.awt.Font(fontData.getName(), style, height); if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) { while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) { height--; tmpAwtFont = new java.awt.Font(fontData.getName(), style, height); } } else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) { while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) { height++; tmpAwtFont = new java.awt.Font(fontData.getName(), style, height); } } tmpFont.dispose(); tmpGC.dispose(); } return new java.awt.Font(fontData.getName(), style, height); } /** * Create an awt font by converting as much information * as possible from the provided swt <code>Font</code>. * * @param device The swt device to draw on (display or gc device). * @param font The swt font to convert. * @return An awt font converted from the provided swt font. */ public static java.awt.Font toAwtFont(Device device, Font font) { FontData fontData = font.getFontData()[0]; return toAwtFont(device, fontData, true); } /** * Creates an awt color instance to match the rgb values * of the specified swt color. * * @param color The swt color to match. * @return an awt color abject. */ public static java.awt.Color toAwtColor(Color color) { return new java.awt.Color(color.getRed(), color.getGreen(), color.getBlue()); } /** * Creates a swt color instance to match the rgb values * of the specified awt paint. For now, this method test * if the paint is a color and then return the adequate * swt color. Otherwise plain black is assumed. * * @param device The swt device to draw on (display or gc device). * @param paint The awt color to match. * @return a swt color object. */ public static Color toSwtColor(Device device, java.awt.Paint paint) { java.awt.Color color; if (paint instanceof java.awt.Color) { color = (java.awt.Color) paint; } else { try { throw new Exception("only color is supported at present... " + "setting paint to uniform black color" ); } catch (Exception e) { e.printStackTrace(); color = new java.awt.Color(0, 0, 0); } } return new org.eclipse.swt.graphics.Color(device, color.getRed(), color.getGreen(), color.getBlue()); } /** * Creates a swt color instance to match the rgb values * of the specified awt color. alpha channel is not supported. * Note that the dispose method will need to be called on the * returned object. * * @param device The swt device to draw on (display or gc device). * @param color The awt color to match. * @return a swt color object. */ public static Color toSwtColor(Device device, java.awt.Color color) { return new org.eclipse.swt.graphics.Color(device, color.getRed(), color.getGreen(), color.getBlue()); } /** * Transform an awt Rectangle2d instance into a swt one. * The coordinates are rounded to integer for the swt object. * @param rect2d The awt rectangle to map. * @return an swt <code>Rectangle</code> object. */ public static Rectangle toSwtRectangle(Rectangle2D rect2d) { return new Rectangle( (int) Math.round(rect2d.getMinX()), (int) Math.round(rect2d.getMinY()), (int) Math.round(rect2d.getWidth()), (int) Math.round(rect2d.getHeight()) ); } /** * Transform a swt Rectangle instance into an awt one. * @param rect the swt <code>Rectangle</code> * @return a Rectangle2D.Double instance with * the eappropriate location and size. */ public static Rectangle2D toAwtRectangle(Rectangle rect) { Rectangle2D rect2d = new Rectangle2D.Double(); rect2d.setRect(rect.x, rect.y, rect.width, rect.height); return rect2d; } /** * Returns an AWT point with the same coordinates as the specified * SWT point. * * @param p the SWT point (<code>null</code> not permitted). * * @return An AWT point with the same coordinates as <code>p</code>. * * @see #toSwtPoint(java.awt.Point) */ public static Point2D toAwtPoint(Point p) { return new java.awt.Point(p.x, p.y); } /** * Returns an SWT point with the same coordinates as the specified * AWT point. * * @param p the AWT point (<code>null</code> not permitted). * * @return An SWT point with the same coordinates as <code>p</code>. * * @see #toAwtPoint(Point) */ public static Point toSwtPoint(java.awt.Point p) { return new Point(p.x, p.y); } /** * Returns an SWT point with the same coordinates as the specified AWT * point (rounded to integer values). * * @param p the AWT point (<code>null</code> not permitted). * * @return An SWT point with the same coordinates as <code>p</code>. * * @see #toAwtPoint(Point) */ public static Point toSwtPoint(java.awt.geom.Point2D p) { return new Point((int) Math.round(p.getX()), (int) Math.round(p.getY())); } /** * Creates an AWT <code>MouseEvent</code> from a swt event. * This method helps passing SWT mouse event to awt components. * @param event The swt event. * @return A AWT mouse event based on the given SWT event. */ public static MouseEvent toAwtMouseEvent(org.eclipse.swt.events.MouseEvent event) { int button = MouseEvent.NOBUTTON; switch (event.button) { case 1: button = MouseEvent.BUTTON1; break; case 2: button = MouseEvent.BUTTON2; break; case 3: button = MouseEvent.BUTTON3; break; } int modifiers = 0; if ((event.stateMask & SWT.CTRL) != 0) { modifiers |= InputEvent.CTRL_DOWN_MASK; } if ((event.stateMask & SWT.SHIFT) != 0) { modifiers |= InputEvent.SHIFT_DOWN_MASK; } if ((event.stateMask & SWT.ALT) != 0) { modifiers |= InputEvent.ALT_DOWN_MASK; } MouseEvent awtMouseEvent = new MouseEvent(DUMMY_PANEL, event.hashCode(), event.time, modifiers, event.x, event.y, 1, false, button); return awtMouseEvent; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -