最近,我学会了一个BufferedImage Java类。我应用这个概念在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) {}
}
paintApp:
import javax.swing.*;
public class paintApp extends JApplet {
public void init() {
this.setSize(600, 400);
this.setContentPane(new PaintBoard());
}
}
答案 0 :(得分: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) {
}
}
}