要绘制的Graphics2D绘制线以创建矩形

时间:2013-03-09 20:51:00

标签: java scale graphics2d lines rect

我正在尝试通过存储需要绘制的线来制作填充铲斗工具。我有那个部分运作良好,但现在当我尝试扩展它时,我遇到了一些问题,我有一些区域之间没有绘制区域。有没有办法解决这个问题,或者更好的方法来解决这个问题?

此代码将演示我遇到的问题。第一个方块没有缩放,看起来很好,但是当我缩放第二个方框时,某些线条之间有空间。

import java.awt.*;
import javax.swing.*;


public class LineWidth extends JPanel{
    public static void main(String[] args) {
        LineWidth l = new LineWidth();
        l.setPreferredSize(new Dimension(500, 500));
        l.setBackground(Color.CYAN);

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(l);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

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

        Graphics2D g2 = (Graphics2D) g.create();

        g2.setColor(Color.BLACK);

        for(int i=10; i<110; i++) {
            g2.drawLine(10, i, 190, i);
        }

        Graphics2D g22 = (Graphics2D) g.create();
        g22.setColor(Color.BLACK);
        g22.scale(2.1, 2.1);
        for(int i=120; i<220; i++) {
            g22.drawLine(10, i, 190, i);
        }
    }
}

对于这个例子,绘制矩形而不是线条会容易得多,但对于我的填充桶,我看不到这样做的方法。

感谢。

1 个答案:

答案 0 :(得分:0)

我最后将线条绘制到BufferedImage,而不是缩放BufferedImage而不是我的线条。这可能不是最有效的方法,但它工作得很好,而且运行速度足够快。感谢您的建议。

相关问题