小编riw*_*iwi的帖子

简化Bresenham的线算法:它*完全*做什么?

基于维基百科关于Bresenham线路算法的文章,我已经实现了那里描述的简化版本,我的Java实现看起来像这样:

int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);

int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;

int err = dx - dy;

while (true) {
    framebuffer.setPixel(x1, y1, Vec3.one);

    if (x1 == x2 && y1 == y2) {
        break;
    }

    int e2 = 2 * err;

    if (e2 > -dy) {
        err = err - dy;
        x1 = x1 + sx;
    } …
Run Code Online (Sandbox Code Playgroud)

java algorithm graphics bresenham

12
推荐指数
1
解决办法
1万
查看次数

如何在没有*忙等待的情况下读取SwingWorker的结果*?

我正在编写一个使用执行文件菜单操作的应用程序SwingWorker.每个被调用的方法都返回一个boolean值,该值告诉操作是否成功执行.

目前我忙于等待结果,如下所示:

public boolean executeOperation() {
    final SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
        @Override
        protected Boolean doInBackground() throws Exception {
            // ..

            if (aborted) {
                return false;
            }

            // ..

            return true;
        }
    };

    worker.execute();

    // busy wait
    while (!worker.isDone())
        ;

    try {
        return worker.get().booleanValue();
    } catch (Exception e) {
        // handle exceptions ..
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有一种较少投票的方式来解决这个问题?

worker.get()直接使用不起作用,因为它阻止了EDT,等待任务完成 - 这意味着即使我在内部打开的对话框SwingWorker也不会被绘制.

编辑:如果可能的话,我想避免方法(或工人)异步地传达他们的结果.我正在实现几个相互依赖的简短方法(文件 - >打开,新建,关闭,保存,另存为,退出)(即当试图退出,退出呼叫关闭,关闭可能会调用保存,保存可能会调用保存如).异步解决这个问题似乎使代码变得更加复杂.

java swing swingworker

6
推荐指数
1
解决办法
5710
查看次数

XmlAdapter到JAXB绑定Joda时间间隔?

在我的JAXB绑定中遇到Web服务问题我已经被困了几个小时:

为了准备一个必须返回Joda Time类实例(即时,持续时间,间隔等)的更大的Web服务,我开始使用只有一个返回Interval的方法的Web服务:

package jodaws;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

import org.joda.time.Interval;

@WebService(name = "JodaWS")
public class JodaWebService {

    public Interval readInterval() {
        return new Interval(30, 40);
    }

    public static void main(String[] args) {
        Endpoint.publish("http://localhost:10100/JodaWS", new JodaWebService());
    }
}
Run Code Online (Sandbox Code Playgroud)

发布此Web服务我收到一个异常,指出"org.joda.time.Interval没有no-arg默认构造函数":

29.05.2011 17:24:07 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
INFO: Dynamically creating request wrapper Class jodaws.jaxws.ReadInterval
29.05.2011 17:24:07 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
INFO: Dynamically creating response wrapper bean Class jodaws.jaxws.ReadIntervalResponse
Exception in thread "main" javax.xml.ws.WebServiceException: Unable to create JAXBContext
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:153)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.postProcess(AbstractSEIModelImpl.java:83)
    at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:244)
    at com.sun.xml.internal.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:312)
    at …
Run Code Online (Sandbox Code Playgroud)

java web-services jaxb jodatime

3
推荐指数
1
解决办法
4277
查看次数

JPA 2.0作为XML的唯一约束

我正在使用JPA 2.0并希望使用XML而不是注释创建唯一约束.

带注释的类看起来像这样:

@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;
    @Column(unique=true)
    private String name;

    // ..
}
Run Code Online (Sandbox Code Playgroud)

而这样的orm.xml文件 - 虽然它缺少唯一约束:

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
    version="2.0">
    <entity class="kiosk.model.Person">
        <attributes>
            <id name="id">
                <generated-value strategy="AUTO" />
            </id>
            <basic name="name" />

            <!-- .. -->
        </attributes>
    </entity>
</entity-mappings>
Run Code Online (Sandbox Code Playgroud)

如何使用XML向JPA 2.0类添加唯一约束?

java jpa jpa-2.0

3
推荐指数
1
解决办法
1035
查看次数