我知道要在 Composite 上绘图,您可以添加一个绘图监听器,但这会导致在子项下绘图。如果我想在 children 的上方画画怎么办?
下面画了一条线,但是 subc 画在了它上面。
Composite c = new Composite(shell, 0);
c.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLUE));
c.setBounds(100, 100, 800, 600);
c.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawLine(0, 0, 500, 1000);
}
});
final Composite subc = new Composite(c, 0);
subc.setLayout(null);
subc.setBounds(10, 10, 600, 400);
最佳答案
解决起来并不难:-)
您不仅需要将 SWT.Paint 监听器添加到 Composite,还需要将其添加到所有包含的子项(递归)。诀窍是为每个控件适本地映射坐标...
为了说明,我附上了我在许多项目中使用的一些代码。是的,我确实知道缺少类(class),但您或许可以从中得到启发。
/*******************************************************************************
* Copyright (c) 2007, 2011 The RCP Company and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* The RCP Company - initial API and implementation
*******************************************************************************/
package com.rcpcompany.uibindings.utils;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import com.rcpcompany.uibindings.IDisposable;
import com.rcpcompany.uibindings.internal.utils.PaintDecorationManager;
/**
* Support for arbitrary decorations for {@link Control controls}.
* <p>
* <p>
* Differs from {@link ControlDecoration} in a number of ways:
* <ul>
* <li>Support for cells in tables</li>
* <li>Vastly more efficient when there are many decorations</li>
* </ul>
*
* @author Tonny Madsen, The RCP Company
*/
public interface IPaintDecoration {
/**
* Factory for {@link IPaintDecoration}.
*/
final class Factory {
private Factory() {
}
/**
* Adds a new decoration.
* <p>
* The decoration is only added if the control of the decoration is non-<code>null</code>.
* <p>
* If the decoration supports the {@link IDisposable} interface, it will be notified when
* the decoration is no longer in use - e.g. when the decoration is removed with
* {@link #removeDecoration(IPaintDecoration)} or if the control is disposed.
*
* @param decoration the new decoration
*/
public static void addDecoration(IPaintDecoration decoration) {
PaintDecorationManager.addDecoration(decoration);
}
public static IPaintDecoration paintRectangle(final Control c, Rectangle rect, final Color color) {
final Rectangle r;
if (rect == null) {
final Point s = c.getSize();
r = new Rectangle(0, 0, s.x, s.y);
} else {
r = rect;
}
final IPaintDecoration pd = new IPaintDecoration() {
@Override
public void paint(Event event, Rectangle area) {
// LogUtils.debug(this, event.widget + ": clip=" + event.gc.getClipping() +
// " area=" + area);
final Color oldForeground = event.gc.getForeground();
event.gc.setForeground(color);
event.gc.drawRectangle(area.x, area.y, area.width - 1, area.height - 1);
event.gc.setForeground(oldForeground);
}
@Override
public Control getControl() {
return c;
}
@Override
public Rectangle getArea() {
return r;
}
};
addDecoration(pd);
return pd;
}
/**
* Removes an existing decoration.
*
* @param decoration the decoration to remove
*/
public static void removeDecoration(IPaintDecoration decoration) {
PaintDecorationManager.removeDecoration(decoration);
}
};
/**
* The control of this decoration.
* <p>
* The control of a specific decoration may not change during the lifetime of the decoration.
*
* @return the control
*/
Control getControl();
/**
* Returns the area of this decoration in relation to the control.
*
* @return the relative location
*/
Rectangle getArea();
/**
* Paints the decoration.
*
* @param area TODO
*/
void paint(Event event, Rectangle area);
}
和
/*******************************************************************************
* Copyright (c) 2007, 2011 The RCP Company and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* The RCP Company - initial API and implementation
*******************************************************************************/
package com.rcpcompany.uibindings.internal.utils;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.jface.util.Util;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import com.rcpcompany.uibindings.IDisposable;
import com.rcpcompany.uibindings.internal.Activator;
import com.rcpcompany.uibindings.utils.IPaintDecoration;
import com.rcpcompany.utils.logging.LogUtils;
/**
* Simple manager that can draw arbitrary "stuff".
* <p>
* A manager exists for each {@link Shell} of the application and is automatically disposed with the
* shell.
* <p>
* Each decoration of the manager is handled internally via an {@link DecorationData} object.
*
* @author Tonny Madsen, The RCP Company
*/
public final class PaintDecorationManager implements IDisposable, Listener {
/**
* The shell of this manager.
*/
private final Shell myShell;
/**
* Cached platform flag for dealing with platform-specific issue:
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=219326 : Shell with custom region and
* SWT.NO_TRIM still has border
*/
private static boolean MAC = Util.isMac();
/**
* Constructs and returns a new manager.
*
* @param shell the shell of the manager
*/
private PaintDecorationManager(Shell shell) {
myShell = shell;
theManagers.put(getShell(), this);
hookControl(getShell());
}
@Override
public void dispose() {
/*
* Unhook all controls. This is automatically remove all decorations.
*/
for (final Control c : myHookedControls.toArray(new Control[myHookedControls.size()])) {
unhookControl(c);
}
theManagers.remove(getShell());
}
public static void addDecoration(IPaintDecoration decoration) {
final PaintDecorationManager mng = getManager(decoration);
if (mng != null) {
mng.addADecoration(decoration);
}
}
public static void removeDecoration(IPaintDecoration decoration) {
final PaintDecorationManager mng = getManager(decoration);
if (mng != null) {
mng.removeADecoration(decoration);
}
}
/**
* Mapping of all decorations of this manager to internal data for the same decoration.
*/
private final Map<IPaintDecoration, DecorationData> myDecorations = new HashMap<IPaintDecoration, DecorationData>();
public void addADecoration(IPaintDecoration decoration) {
DecorationData dd = myDecorations.get(decoration);
if (dd == null) {
dd = new DecorationData(decoration);
}
dd.update();
}
public void removeADecoration(IPaintDecoration decoration) {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + decoration.getControl() + "@" + decoration.getControl().hashCode() + "/"
+ decoration.getArea());
}
final DecorationData dd = myDecorations.get(decoration);
if (dd == null) return;
dd.dispose();
}
/**
* Map with all defined managers indexed by the shell.
*/
private static Map<Shell, PaintDecorationManager> theManagers = new HashMap<Shell, PaintDecorationManager>();
/**
* Returns the shell of the manager.
*
* @return the shell
*/
private Shell getShell() {
return myShell;
}
/**
* Returns the manager for the specified decoration.
* <p>
* Creates a new manager if none exists
*
* @param decoration the decoration
* @return the manager for the shell of the decoration
*/
private static PaintDecorationManager getManager(IPaintDecoration decoration) {
final Control c = decoration.getControl();
if (c == null) return null;
final Shell shell = c.getShell();
PaintDecorationManager mng = theManagers.get(shell);
if (mng == null) {
mng = new PaintDecorationManager(shell);
}
return mng;
}
/**
* The hooked controls of this manager.
* <p>
* A control is hooked when first referred in a decoration or a parent...
* <p>
* It is not unhooked until the control or this manager is disposed.
*/
private final Set<Control> myHookedControls = new HashSet<Control>();
/**
* Hooks the specified control into this manager.
* <p>
* Also hooks all parent controls.
*
* @param control the control
*/
public void hookControl(Control control) {
if (myHookedControls.contains(control)) return;
myHookedControls.add(control);
control.addListener(SWT.Dispose, this);
control.addListener(SWT.Paint, this);
if (control != getShell()) {
hookControl(control.getParent());
}
}
/**
* Unhooks a specific control from the manager.
*
* @param control the control
*/
public void unhookControl(Control control) {
if (!myHookedControls.contains(control)) return;
myHookedControls.remove(control);
if (!control.isDisposed()) {
control.removeListener(SWT.Dispose, this);
control.removeListener(SWT.Paint, this);
}
for (final DecorationData dd : myDecorations.values()
.toArray(new DecorationData[myDecorations.values().size()])) {
if (dd.getControl() == control) {
dd.dispose();
}
}
}
@Override
public void handleEvent(Event event) {
// LogUtils.debug(this, ToStringUtils.toString(event));
switch (event.type) {
case SWT.Dispose:
handleDispose(event);
break;
case SWT.Paint:
handlePaint(event);
break;
default:
break;
}
}
/**
* Handles the dispose event.
*
* @param event the event
*/
private void handleDispose(Event event) {
if (event.widget == getShell()) {
dispose();
return;
}
unhookControl((Control) event.widget);
}
/**
* Handles the paint event.
*
* @param event the event
*/
private void handlePaint(Event event) {
final Control c = (Control) event.widget;
final Display display = c.getDisplay();
final Rectangle area = display.map(c, null, event.x, event.y, event.width, event.height);
for (final DecorationData dd : myDecorations.values()) {
if (dd.intersects(area)) {
dd.paint(event);
}
}
}
/**
* Manager internal decoration data for one decoration.
*/
protected class DecorationData implements IDisposable {
private final IPaintDecoration myDecoration;
/**
* The previous area painted by this decoration relative to the display.
*/
private Rectangle myPreviousArea = null;
/**
* Set to true when the decoration is disposed
*/
private boolean isDisposed = false;
/**
* Constructs and returns a new decoration data object
*
* @param decoration he base decoration
*/
protected DecorationData(IPaintDecoration decoration) {
myDecoration = decoration;
myDecorations.put(getDecoration(), this);
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + this);
}
getManager().hookControl(getDecoration().getControl());
}
/**
* Returns the control of the decoration
*
* @return the control
*/
public Control getControl() {
return getDecoration().getControl();
}
@Override
public void dispose() {
isDisposed = true;
update();
myDecorations.remove(getDecoration());
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + this);
}
}
/**
* Returns the manager of this decoration
*
* @return the manager
*/
public PaintDecorationManager getManager() {
return PaintDecorationManager.this;
}
/**
* Returns the decoration itself
*
* @return the decoration
*/
public IPaintDecoration getDecoration() {
return myDecoration;
}
/**
* Updates this decoration
*/
public void update() {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + this);
}
/*
* Calculate new area
*/
final Rectangle newArea = getDecorationRectangle(getShell());
/*
* Compare with last area and image
*/
if ((newArea == null ? myPreviousArea == null : newArea.equals(myPreviousArea))) {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "-- return");
}
return;
}
Rectangle r = null;
if (myPreviousArea != null) {
r = myPreviousArea;
if (newArea != null) {
r.add(newArea);
}
} else {
r = newArea;
}
myPreviousArea = newArea;
if (r != null) {
// LogUtils.debug(this, "redraw: " + r);
getShell().redraw(r.x, r.y, r.width, r.height, true);
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "redraw " + r);
}
}
}
/**
* Calculates the area taken by the image translated to a specified target control
*
* @param c the target control or null for the Display
*
* @return the {@link Rectangle} for the image or <code>null</code> if no image is specified
*/
private Rectangle getDecorationRectangle(Control c) {
final Control control = getDecoration().getControl();
final Rectangle b = getDecoration().getArea();
final Rectangle bounds = new Rectangle(b.x, b.y, b.width + 1, b.height + 1);
return getShell().getDisplay().map(control, c, bounds);
}
/**
* Paints this decoration.
*
* @param event the {@link SWT#Paint} event
*/
public void paint(Event event) {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "paint: " + event.widget);
}
// if (!shouldShowDecoration()) {
// return;
// }
final Rectangle rect = getDecorationRectangle((Control) event.widget);
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "paint: " + event.widget + "/" + event.widget.hashCode() + ": rect=" + rect);
}
getDecoration().paint(event, rect);
}
/**
* Returns whether this decoration intersects with specified rectangle.
*
* @param eventArea the area to check
* @return <code>true</code> if the decoration is visible and the area intersects
*/
public boolean intersects(Rectangle eventArea) {
if (isDisposed) return false;
if (!getControl().isVisible()) return false;
final Rectangle area = getDecorationRectangle(null);
if (area == null) return false;
if (!area.intersects(eventArea)) return false;
return true;
}
@Override
public String toString() {
return getControl() + "@" + getControl().hashCode() + " " + getDecoration().getArea() + " area "
+ getDecorationRectangle(null);
}
}
}
关于java - 如何在 SWT 中绘制 Composite 的子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6849216/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式rubyshell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R