我正在修读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();
}
}
答案 0 :(得分:0)
在标有// Bigger brush size
的行上,您可以在鼠标位置绘制一个总长为6的椭圆。然后,您的程序立即在同一位置绘制一个更大的椭圆形。这意味着您绘制的第一个椭圆会立即被删除。
要实现可变大小的画笔,您应该使用变量(例如int brushSize
)来跟踪当前大小。然后替换如下行:
graphics2D.fillOval( (oldX-3), (oldY-3), 6, 6);
与
graphics2D.fillOval((oldX-brushSize/2), (oldY-brushSize/2), brushSize, brushSize);
答案 1 :(得分:0)
尝试从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()
答案 2 :(得分:0)
看看我是怎么做的
我在stroke
类中创建了一个静态int变量PadDraw()
,然后在Bursh 1和Bursh 2的操作中我更改了它的值,在底部我将它添加到nBsize
< / p>
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();
}
}