java - 使用 Java2D 和 Swing 为我的 Java vector 图形编辑器创建曲线工具

标签 java swing graphics vector java-2d

我正在创建一个 vector 图形编辑器,并已成功创建画笔和一些基本的 2d 形状,如矩形、椭圆形、直线等。现在困扰我的是曲线或曲线工具。我通过从 mousePressed 获取原始点,然后更新每个 mouseDragged 上的第二个点来创建其他点。这是部分代码:

public void mousePressed(MouseEvent e){
    gfx.setColor(Tool.currentColor);
    pos1 = e.getPoint(); //Get the First Point
}
public void mouseReleased(MouseEvent e){
    if(!e.isAltDown() && !e.isMetaDown())

    if(currentShape != null) shapeSet.add(currentShape);
    currentShape = null;
}
public void mouseDragged(MouseEvent e){
    if(e.isAltDown() || e.isMetaDown()) return;
    //the mouse was dragged, so begin painting
    pos2 = e.getPoint();

    int ctool = Tool.currentTool;

    if(ctool == Tool.LINE){
        currentShape = new Line(pos1,pos2,Tool.currentColor,null);
    }else if(ctool == Tool.ELLIPSE){
        currentShape = new Ellipse(pos1,pos2,Tool.currentColor,null);
    }else if(ctool == Tool.RECTANGLE){
        currentShape = new Rectangle(pos1,pos2,Tool.currentColor,null);
    }

    this.repaint();
}

直线、椭圆等类非常简单,它们只是获取点并在稍后重绘中绘制它们。临时存储在 currentShape 中的所有形状稍后都会附加到 shapeSet 中,它是抽象类 Shape 的 ArrayList。

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);

    gfx.setBackground(Color.WHITE);

    gfx.clearRect(0, 0, img.getWidth(), img.getHeight());
    for(int i=0; i< shapeSet.size(); i++){
        shapeSet.get(i).draw(gfx);
    }
    if(currentShape != null){
        currentShape.draw(gfx);
    }

    g.drawImage(img, 0, 0, null);
} 

img 是一个BufferedImage,gfx 是img 的图形

img = new BufferedImage(640,480,BufferedImage.TYPE_INT_RGB);
gfx = img.createGraphics(); //Graphics2D

现在我到底该如何创建曲线工具

P.S如果您想要形状(直线、椭圆等)类的代码,只需评论,我会发布它。

最佳答案

这是一个Bézier Curve Editor in Java这可能会给您一些想法,但我不确定您如何将其集成到您的程序中。

关于java - 使用 Java2D 和 Swing 为我的 Java vector 图形编辑器创建曲线工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8500869/

相关文章:

java - GUI 对象不绘制

java - 将文本从一件事更改为另一件事的 JLabel

c++ - 如何通过折线切割二维三角形网格?

java - JNI "jobject thiz"与jboolean参数关系

java - 这个java程序的输出有点偏差?

java - Java中PriorityQueue类的加法操作

matlab - 将 Matlab 图导出为 PNG?

java - 查找适合区域的最大字体大小 - Java

java - 如何向背景 Jpanel 添加更多图像?

unity3d - 编辑器中的 Unity 模糊和像素化 Sprite (无像素艺术)