我正在上Java课程,这是我的第一门计算机科学课程。我目前正在从事的项目是我整理的一个小型 Paint 程序,因此我可以学习一些 GUI 编程(该课程实际上都是关于算法的)。我正在尝试更改画笔大小(我已经在我的代码中标记了)。不过,我无法弄清楚如何做到这一点。与此同时,我将努力工作。希望我可以实现一个菜单:)
我的文件 I/O 我也有部分工作,但我想先关注画笔大小。
预览其功能:http://youtu.be/Qgpol8_iKXQ
谢谢!
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Paint {
public static void main(String[] args) {
PaintWindow frame = new PaintWindow();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
}
class PaintWindow extends JFrame {
public PaintWindow() {
setTitle(" Paint");
setSize(700, 600);
panel = new JPanel();
drawPad = new PadDraw();
panel.setPreferredSize(new Dimension(100, 68));
//Creates a new container
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
//sets the panel to the left, padDraw in the center (To the right of it)
content.add(panel, BorderLayout.WEST);
content.add(drawPad, BorderLayout.CENTER);
//add the color buttons:
makeColorButton(Color.MAGENTA);
makeColorButton(Color.BLUE);
makeColorButton(Color.CYAN);
makeColorButton(Color.GREEN);
makeColorButton(Color.YELLOW);
makeColorButton(Color.ORANGE);
makeColorButton(Color.RED);
makeColorButton(Color.PINK);
makeColorButton(Color.LIGHT_GRAY);
makeColorButton(Color.GRAY);
makeColorButton(Color.DARK_GRAY);
makeColorButton(Color.BLACK);
//creates the eraser (Draws the background color which is white)
JButton eraserButton = new JButton("Eraser");
eraserButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawPad.changeColor(Color.white);
}
});
panel.add(eraserButton);
//creates the clear button
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawPad.clear();
}
});
panel.add(clearButton);
JButton brush1Button = new JButton("Brush 1");
brush1Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//here
}
});
panel.add(brush1Button);
JButton brush2Button = new JButton("Brush 2");
brush2Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//stuff here
}
});
panel.add(brush2Button);
JButton saveButton = new JButton("Save As");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BufferedImage image = new BufferedImage(drawPad.getWidth(), drawPad.getHeight(), BufferedImage.TYPE_INT_RGB);
//Graphics2D graphics2D = image.createGraphics();
//drawPad.paint(graphics2D);
try {
ImageIO.write(image, "png", new File("C:/.../Desktop/Example.png"));
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
panel.add(saveButton);
}
/*
* makes a button that changes the color
* the color used for the button
*/
public void makeColorButton(final Color color) {
JButton tempButton = new JButton();
tempButton.setBackground(color);
tempButton.setPreferredSize(new Dimension(20, 20));
panel.add(tempButton);
tempButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawPad.changeColor(color);
}
});
}
private JPanel panel;
private PadDraw drawPad;
}
class PadDraw extends JComponent {
//this is gonna be your image that you draw on
Image image;
//this is what we'll be using to draw on
Graphics2D graphics2D;
//these are gonna hold our mouse coordinates
int currentX, currentY, oldX, oldY;
public PadDraw() {
setDoubleBuffered(false);
addMouseListener(new MouseAdapter() {
//if the mouse is pressed it sets the oldX & oldY
//coordinates as the mouses x & y coordinates
//Thsi allows click and drag
public void mousePressed(MouseEvent e) {
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
//while the mouse is dragged it sets currentX & currentY as the mouses x and y
//then it draws a line at the coordinates
//it repaints it and sets oldX and oldY as currentX and currentY
public void mouseDragged(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
//draws a line
//graphics2D.drawLine(oldX, oldY, currentX, currentY);
//
//graphics2D.fillOval( (oldX-5), (oldY-5), 10, 10);
//repaint();
//variable brush size here
int nBsize;
nBsize = 14;
graphics2D.fillOval((oldX - (nBsize / 2)), (oldY - (nBsize / 2)), nBsize, nBsize);
repaint();
oldX = currentX;
oldY = currentY;
}
});
}
//this is the painting bit.
//if it has nothing on it then
//it creates an image the size of the window
//sets Graphics as the image
//sets the rendering
//then runs the clear() method
//then it draws the image
public void paintComponent(Graphics g) {
if (image == null) {
image = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D) image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 0, 0, null);
}
//this is the clear
//it sets the color as white
//then it fills the window with white
//then it sets the color back to black
public void clear() {
graphics2D.setPaint(Color.white);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
graphics2D.setPaint(Color.black);
repaint();
}
public void changeColor(Color theColor) {
graphics2D.setPaint(theColor);
repaint();
}
}
看看我是怎么做到
的我在类中创建了一个静态 int 变量stroke
PadDraw()
然后在 Bursh 1 和 Bursh 2 的操作中,我更改了它的值,并在底部将其添加到nBsize
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Paint {
public static void main(String[] args) {
PaintWindow frame = new PaintWindow();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
}
class PaintWindow extends JFrame {
public PaintWindow() {
setTitle(" Paint");
setSize(700, 600);
panel = new JPanel();
drawPad = new PadDraw();
panel.setPreferredSize(new Dimension(100, 68));
//Creates a new container
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
//sets the panel to the left, padDraw in the center (To the right of it)
content.add(panel, BorderLayout.WEST);
content.add(drawPad, BorderLayout.CENTER);
//add the color buttons:
makeColorButton(Color.MAGENTA);
makeColorButton(Color.BLUE);
makeColorButton(Color.CYAN);
makeColorButton(Color.GREEN);
makeColorButton(Color.YELLOW);
makeColorButton(Color.ORANGE);
makeColorButton(Color.RED);
makeColorButton(Color.PINK);
makeColorButton(Color.LIGHT_GRAY);
makeColorButton(Color.GRAY);
makeColorButton(Color.DARK_GRAY);
makeColorButton(Color.BLACK);
//creates the eraser (Draws the background color which is white)
JButton eraserButton = new JButton("Eraser");
eraserButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawPad.changeColor(Color.white);
}
});
panel.add(eraserButton);
//creates the clear button
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawPad.clear();
}
});
panel.add(clearButton);
JButton brush1Button = new JButton("Brush 1");
brush1Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//here
PadDraw.stroke = 10;
}
});
panel.add(brush1Button);
JButton brush2Button = new JButton("Brush 2");
brush2Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PadDraw.stroke = 15;
//stuff here
}
});
panel.add(brush2Button);
JButton saveButton = new JButton("Save As");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BufferedImage image = new BufferedImage(drawPad.getWidth(), drawPad.getHeight(), BufferedImage.TYPE_INT_RGB);
//Graphics2D graphics2D = image.createGraphics();
//drawPad.paint(graphics2D);
try {
ImageIO.write(image, "png", new File("C:/.../Desktop/Example.png"));
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
panel.add(saveButton);
}
/*
* makes a button that changes the color
* the color used for the button
*/
public void makeColorButton(final Color color) {
JButton tempButton = new JButton();
tempButton.setBackground(color);
tempButton.setPreferredSize(new Dimension(20, 20));
panel.add(tempButton);
tempButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawPad.changeColor(color);
}
});
}
private JPanel panel;
private PadDraw drawPad;
}
class PadDraw extends JComponent {
//default value of stroke is 5
static int stroke = 5;
//this is gonna be your image that you draw on
Image image;
//this is what we'll be using to draw on
Graphics2D graphics2D;
//these are gonna hold our mouse coordinates
int currentX, currentY, oldX, oldY;
public PadDraw() {
setDoubleBuffered(false);
addMouseListener(new MouseAdapter() {
//if the mouse is pressed it sets the oldX & oldY
//coordinates as the mouses x & y coordinates
//Thsi allows click and drag
public void mousePressed(MouseEvent e) {
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
//while the mouse is dragged it sets currentX & currentY as the mouses x and y
//then it draws a line at the coordinates
//it repaints it and sets oldX and oldY as currentX and currentY
public void mouseDragged(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
//draws a line
//graphics2D.drawLine(oldX, oldY, currentX, currentY);
//
//graphics2D.fillOval( (oldX-5), (oldY-5), 10, 10);
//repaint();
//variable brush size here
int nBsize;
nBsize = stroke;
graphics2D.fillOval((oldX - (nBsize / 2)), (oldY - (nBsize / 2)), nBsize, nBsize);
repaint();
oldX = currentX;
oldY = currentY;
}
});
}
//this is the painting bit.
//if it has nothing on it then
//it creates an image the size of the window
//sets Graphics as the image
//sets the rendering
//then runs the clear() method
//then it draws the image
public void paintComponent(Graphics g) {
if (image == null) {
image = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D) image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 0, 0, null);
}
//this is the clear
//it sets the color as white
//then it fills the window with white
//then it sets the color back to black
public void clear() {
graphics2D.setPaint(Color.white);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
graphics2D.setPaint(Color.black);
repaint();
}
public void changeColor(Color theColor) {
graphics2D.setPaint(theColor);
repaint();
}
}
有 // Bigger brush size
的线上,在鼠标位置绘制一个大小始终为 6 的椭圆。 然后,程序立即在同一位置绘制一个更大的椭圆。 这意味着您绘制的第一个椭圆会立即被删除。
要实现可变大小的画笔,您应该使用变量(例如 int brushSize
) 来跟踪当前大小。 然后替换一行,例如:
graphics2D.fillOval( (oldX-3), (oldY-3), 6, 6);
跟
graphics2D.fillOval((oldX-brushSize/2), (oldY-brushSize/2), brushSize, brushSize);
尝试从 paintComponent
方法以外的方法进行绘制将不起作用。您需要做的是让mouseXxxx
方法更改paintComponent
方法中也使用的类成员坐标,然后调用 repaint()
。像这样的东西
public class MyComp extends JComponent implements MouseMotionListener{
int x;
int y;
public void mouseMoved(MouseEvent e) {
Point p = e.getPoint();
x = p.getX();
y = p.getY();
repaint();
}
protected void paintComponent(Graphics g) {
g.fillRect(x, y, 100, 100);
}
}
你画的所有东西都应该以paintComponent
的方法进行。使用你用来绘制的全局变量,并在其他方法中调整这些变量并调用repaint()