如何使用BufferedImage在JPanel上绘制多条曲线?

3

最近,我学习了Java中的BufferedImage类,并应用该概念创建了一个简单的Java绘画应用程序。但是我无法在面板上绘制任何曲线。我查看了网上其他缓冲图像示例,但似乎都无法在我的代码上工作。我是否正确使用了BufferedImage?我该如何修复我的代码?

欢迎更改我的源代码。

PaintBoard类:

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

public class PaintBoard extends JPanel implements MouseMotionListener, MouseListener {
private BufferedImage canvas = new BufferedImage(600, 400, BufferedImage.TYPE_INT_RGB);

private boolean painting;
private int prevX, prevY, curX, curY;
Color canvasColour = Color.WHITE;
int brushSize = 6;
int brushType = 1;
Color currentColour = Color.BLACK;

public PaintBoard() {
    setSize(getWidth(), getHeight());
    addMouseMotionListener(this);
    addMouseListener(this);
}

@Override
public void paintComponent(Graphics board) {
    super.paintComponent(board);
    board.setColor(canvasColour);
    board.fillRect(0, 0, getWidth(), getHeight());
    board.drawImage(canvas, 600, 400, this);
}

private void updateBoard() {
    Graphics2D paintBrush = canvas.createGraphics();
    paintBrush.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    paintBrush.setPaint(currentColour);
    paintBrush.setStroke(new BasicStroke(brushSize, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    paintBrush.drawLine(prevX, prevY, curX, curY);
    repaint();
}

public void mouseDragged(MouseEvent e) {
    if (!painting)
        return;

    curX = e.getX();
    curY = e.getY();
    updateBoard();
    prevX = curX;
    prevY = curY;
}

public void mousePressed(MouseEvent e) {
    if (painting)
        return;

    prevX = e.getX();
    prevY = e.getY();
    painting = true;
}

public void mouseReleased(MouseEvent e) {
    if (!painting)
        return;

    curX = e.getX();
    curY = e.getY();
    painting = false;
}

public void mouseMoved(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}
}

绘画应用:

import javax.swing.*;

public class paintApp extends JApplet {
public void init() {
    this.setSize(600, 400);
    this.setContentPane(new PaintBoard());
}
}

1
小程序已经过时了。它们不再被浏览器、Oracle或社区支持。 - MadProgrammer
1个回答

2

board.drawImage(canvas, 600, 400, this); - 你正在屏幕外绘制图像(在面板的底部/右侧边缘)。请将其改为board.drawImage(canvas, 0, 0, this);

绘画是在组件的坐标空间内完成的,即上/左为0x0

但它只会使屏幕变黑

是的,那是默认的图像,你还没有用起始颜色填充它

添加类似于以下内容...

Graphics2D paintBrush = canvas.createGraphics();
paintBrush.setColor(canvasColour);
paintBrush.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
paintBrush.dispose();

在你的PaintBoard构造函数中添加以下内容:

可运行示例....

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new PaintBoard());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintBoard extends JPanel implements MouseMotionListener, MouseListener {

        private BufferedImage canvas = new BufferedImage(600, 400, BufferedImage.TYPE_INT_RGB);

        private int prevX, prevY, curX, curY;
        Color canvasColour = Color.WHITE;
        int brushSize = 6;
        int brushType = 1;
        Color currentColour = Color.BLACK;

        public PaintBoard() {
            setSize(getWidth(), getHeight());
            addMouseMotionListener(this);
            addMouseListener(this);
            Graphics2D paintBrush = canvas.createGraphics();
            paintBrush.setColor(canvasColour);
            paintBrush.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
            paintBrush.dispose();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 400);
        }

        @Override
        public void paintComponent(Graphics board) {
            super.paintComponent(board);
            board.drawImage(canvas, 0, 0, this);
        }

        private void updateBoard() {
            Graphics2D paintBrush = canvas.createGraphics();
            paintBrush.drawRect(0, 0, canvas.getWidth() - 1, canvas.getHeight() - 1);
            paintBrush.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            paintBrush.setPaint(currentColour);
            paintBrush.setStroke(new BasicStroke(brushSize, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            System.out.println(prevX + "x" + prevY + "-" + curX + "x" + curY);
            paintBrush.drawLine(prevX, prevY, curX, curY);
            paintBrush.dispose();
            repaint();
        }

        public void mouseDragged(MouseEvent e) {
            curX = e.getX();
            curY = e.getY();
            updateBoard();
            prevX = curX;
            prevY = curY;
        }

        public void mousePressed(MouseEvent e) {
            prevX = e.getX();
            prevY = e.getY();
        }

        public void mouseReleased(MouseEvent e) {
            curX = e.getX();
            curY = e.getY();
        }

        public void mouseMoved(MouseEvent e) {
        }

        public void mouseClicked(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接