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

发布于 2024-12-21 06:09:33 字数 1860 浏览 2 评论 0原文

我正在创建一个矢量图形编辑器,并成功创建了画笔和一些基本的 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();
}

Line、Ellipse 等类非常简单,它们只是获取点并稍后在重绘中绘制它们。临时存储在 currentShape 中的所有 Shape 稍后都会附加到 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

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

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

I am creating a Vector Graphics editor and have successfully created the paint-brush and some basic 2d shapes like Rectange, Ellipse , Line etc. Now what's bothering me is the Curved Line or Curve tool. I created the others by getting the original point from mousePressed and then updating the second point on each mouseDragged. Here's part of the code:

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();
}

The Line,Ellipse etc. classes are quite simple, they just take the points and draw them later in the repaint. All the Shapes temporarily stored in currentShape are then later appended to shapeSet , which is an ArrayList of the abstract class Shape.

@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 is a BufferedImage and gfx is img's Graphics.

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

Now how exactly would I go about creating a Curve Tool?

P.S If you want the code of the Shape (Line,Ellipse etc.) classes, just comment and I'll post it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷雾森÷林ヴ 2024-12-28 06:09:33

这是一个 Java 中的贝塞尔曲线编辑器可能会给你一些想法,但我不确定你如何将它集成到你的程序中。

Here's a Bézier Curve Editor in Java that might give you some ideas, but I'm not sure how you'd integrate it into your program.

王权女流氓 2024-12-28 06:09:33

使用 http://docs.oracle。 com/javase/1.4.2/docs/api/java/awt/geom/PathIterator.html

您必须定义 SEG_QUADTO 或 SEG_CUBICTO。只需添加一个具有适当双坐标的新段即可。

Use http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/PathIterator.html

You have to define SEG_QUADTO or SEG_CUBICTO. Just add a new segment with propr double coordinates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文