小编max*_*dev的帖子

JScrollPane - 相对于鼠标位置进行缩放

我需要在放大图像时计算视口的新位置.

UI构建如下:

  • ImagePanel绘制图像
  • ImagePanelWrapper是围绕imagePanel的JPanel
  • JScrollPane包含ImagePanelWrapper

放大或缩小时,ImagePanel的缩放系数会发生变化,并且会重新计算ImagePanel的首选大小.因此,即使ImagePanel停留在相同的视口点,此面板上的图像也会移动.

当用户按住CTRL并使用鼠标滚轮时,将调用以下方法.给定的是MouseWheelListener提供的光标位置.利用这些方法中的功能,当放大或缩小时,图像已经停留在相同的左上位置.

问题是我只是想弄清楚如何相对于鼠标移动,例如Paint.NET.有任何想法吗?

/**
 * 
 */
public void zoomOut(Point point) {
    this.imagePanel.setZoom(this.imagePanel.getZoom() * 0.9f);
    Point pos = this.getViewport().getViewPosition();

    int newX = (int) (pos.x * 0.9f);
    int newY = (int) (pos.y * 0.9f);
    this.getViewport().setViewPosition(new Point(newX, newY));

    this.imagePanel.revalidate();
    this.imagePanel.repaint();
}

/**
 * 
 */
public void zoomIn(Point point) {
    this.imagePanel.setZoom(this.imagePanel.getZoom() * 1.1f);
    Point pos = this.getViewport().getViewPosition();

    int newX = (int) (pos.x * 1.1f);
    int newY = (int) (pos.y * 1.1f);
    this.getViewport().setViewPosition(new Point(newX, newY));

    this.imagePanel.revalidate();
    this.imagePanel.repaint();
}
Run Code Online (Sandbox Code Playgroud)

java mouse swing zoom jscrollpane

10
推荐指数
1
解决办法
9345
查看次数

传递函数的返回值作为参考

在以下情况中应该发生什么:

int functionA() {
    return 25;
}

void functionB(const int& ref) {
    cout << ref << endl;
}

void start() {
    functionB(functionA());
}
Run Code Online (Sandbox Code Playgroud)

编译此示例时,它会输出正确的值25.这是如何工作的?当仅使用对它的引用时,不应该删除堆栈上引用的返回值(从堆栈中删除),还是行为未定义?

c++ stack reference return-value pass-by-reference

9
推荐指数
2
解决办法
3243
查看次数

谁在S​​pring Security中创建了JSESSIONID cookie?

在我的web应用程序那里有每个区域(路径下可以访问的要求/de_DE/,/en_US/等等)分别有它自己的会话.我这样做是通过覆盖会话CookieGenerator以便为会话cookie设置路径,因此浏览器会为访问的语言环境发送正确的会话ID.

我现在遇到的问题是Spring Security在登录后更改会话ID并在某处生成新的会话cookie.这个cookie没有我想要的路径.我在哪里可以操纵Spring如何生成会话cookie?

java cookies session spring spring-security

9
推荐指数
1
解决办法
6679
查看次数

从基类的静态模板方法中调用继承类的受保护的ctor失败

我有一个组件类,它定义了Component一般应该如何创建的静态模板方法:

class Component {
protected:
    uint32_t id;

    Component(uint32_t id) :
            id(id) {
    }

    template<typename T, uint32_t C>
    static T* createComponent() {
        // content here not relevant
        return new T(someParameter);
    }

};
Run Code Online (Sandbox Code Playgroud)

然后有一个实现,例如a Button.不应该直接使用此类的构造函数,而是使用一个调用Component::createComponent模板函数的静态方法.

class Button: public Component {
protected:
    Button(uint32_t id) :
            Component(id) {
    }
public:
    static Button* create();
};
Run Code Online (Sandbox Code Playgroud)

实现看起来像这样,将类型传递给实例化,并在创建中使用常量:

Button* Button::create() {
    return createComponent<Button, UI_COMPONENT_BUTTON>();
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是,编译器抱怨"错误:'Button :: Button(uint32_t)'受到保护".根据我的理解,这个构造函数调用应该可以作为Button扩展Component,但这似乎是一个问题.

我怎么解决这个问题?

c++ inheritance templates protected

8
推荐指数
1
解决办法
360
查看次数

在Android手机中使用SIM卡插槽作为智能卡读卡器

我正在使用具有2个SIM卡插槽的Android手机.我想知道是否有办法使用其中一个插槽来读取其他类型的卡,比如JavaCards?

android kernel javacard sim-card

7
推荐指数
1
解决办法
1586
查看次数

使用cairo和freetype进行字体布局和渲染

我有一个只有freetype2和cairo库的系统.我想要实现的是:

  • 获取UTF-8文本的字形
  • 布局文本,存储位置信息(由我自己)
  • 获取每个字形的cairo路径以进行渲染

不幸的是,文档并没有真正解释它应该如何完成,因为他们希望使用像Pango这样的更高级别的库.

认为可能是正确的:创建一个缩放字体,cairo_scaled_font_create然后使用检索文本的字形cairo_scaled_font_text_to_glyphs.cairo_glyph_extents然后给出每个字形的范围.但是,我怎么能得到像字距调整和进步这样的东西呢?另外,我怎样才能获得每种字体的路径?

有关此主题的更多资源吗?这些功能是预期的方式吗?

c fonts freetype text-rendering cairo

7
推荐指数
1
解决办法
2703
查看次数

通过Camel Blueprint中的属性配置SQL数据源(在Karaf中)

给出了一个非常简单的Camel捆绑包,用于生成camel-archetype-blueprint,我希望添加一个通过属性而不是在属性中配置的数据源blueprint.xml.

我尝试以各种方式配置PropertiesComponent并访问propertyMySQL数据源的值中的属性,但似乎没有一个工作.记录消息时,可以访问属性.

如何使用属性文件中的参数值配置数据源?

我特别需要这个,为多个bundle使用相同的数据源配置,并区分生产/测试环境.我考虑过在构建过程中使用Maven编写属性,具体取决于目标环境.有关如何解决此数据源问题的其他最佳实践吗?

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
       http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
        <property name="location" value="classpath:config.properties" />
    </bean>

    <bean id="dataSourceMySQL" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
        <property name="url" value="jdbc:mysql://127.0.0.1/test_database" />
        <!-- This causes an error, as it tries to connect with
             `${mysqlUser}`@`localhost` without any evaluation -->
        <property name="user" value="${mysqlUser}" />
        <property name="password" value="${mysqlPassword}" />
    </bean>

    <service interface="javax.sql.DataSource" ref="dataSourceMySQL">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="jdbc/mysqlDatasource" />
        </service-properties>
    </service>
    <bean id="sql" class="org.apache.camel.component.sql.SqlComponent">
        <property name="dataSource" ref="dataSourceMySQL" />
    </bean> …
Run Code Online (Sandbox Code Playgroud)

java datasource apache-camel blueprint apache-karaf

7
推荐指数
1
解决办法
4604
查看次数

即时编译Java字节码

我们目前正在研究自己的Java虚拟机实现的JIT编译部分.我们现在的想法是将给定的Java字节码简单地转换为操作码,将它们写入可执行内存并直接调用方法的开头.

假设给定的Java代码是:

int a = 13372338;
int b = 32 * a;
return b;
Run Code Online (Sandbox Code Playgroud)

现在,进行了以下方法(假设给定的内存从0x1000开始,并且在eax中预期返回值):

0x1000: first local variable - accessible via [eip - 8]
0x1004: second local variable - accessible via [eip - 4]
0x1008: start of the code - accessible via [eip]

Java bytecode | Assembler code (NASM syntax)
--------------|------------------------------------------------------------------
              | // start
              | mov edx, eip
              | push ebx
              |         
              | // method content
ldc           | mov eax, 13372338
              | push eax
istore_0      | pop eax
              | …
Run Code Online (Sandbox Code Playgroud)

c++ assembly jit jvm vm-implementation

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

编码CALL指令以调用函数

我正在尝试创建一个能够在运行时编码指令的汇编程序(对于JIT编译器).对于长代码片段感到抱歉,但这是显示我的问题的最短可编译示例.

#include <stdint.h>
#include <iostream>

#include <windows.h>

typedef void (*function)();
uint8_t* instructionBuffer;
uint32_t pos;

/**
 * Creates the instruction buffer;
 */
void assembler_initialize() {
    instructionBuffer = (uint8_t*) VirtualAllocEx(GetCurrentProcess(), 0, 1024,
    MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    pos = 0;
}

/**
 * Writes a call to the given address to the instruction buffer
 */
void assembler_emit_call(uint32_t value) {
    // CALL opcode
    instructionBuffer[pos++] = 0xFF;

    // opcode extension 2, read a 32bit address
    instructionBuffer[pos++] = 0x15;

    // Address as little endian
    instructionBuffer[pos++] = (value >> …
Run Code Online (Sandbox Code Playgroud)

c x86 instruction-set

5
推荐指数
1
解决办法
1165
查看次数

当从内部类访问它的私有构造函数时,Reflection返回两个构造函数

public class Base {

    private Base instance;

    private Base() {
    }

    public static class BaseHelper {
        Base instance = new Base();
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我在基类中有一个无参构造函数.现在我列出这个类的构造函数,如下所示:

Constructor<?>[] constructors = Base.class.getDeclaredConstructors();
System.out.println(constructors);
Run Code Online (Sandbox Code Playgroud)

运行此代码时,我得到以下输出:

[private com.Base(), com.Base(com.Base)]
Run Code Online (Sandbox Code Playgroud)

这告诉我有两个构造函数:

  • 我声明的私有构造函数
  • 公共默认构造函数

为什么是这样?

java reflection

4
推荐指数
1
解决办法
141
查看次数