在图像上绘图

时间:2013-07-24 00:39:03

标签: java image swing paintcomponent

所以我有一个基本上像油漆的程序,但用Java制作,只能绘制几种颜色。默认情况下,程序的背景是白色的,但我想要做的是尝试加载图像,然后能够在该图像上绘制。我可以加载图像,但出于某种原因,当我尝试绘制它时,它不会显示线条。这是代码。

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

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("JavShot Edit");
    setSize(668, 600);
    setLocationRelativeTo(null);

    panel = new JPanel();
    drawPad = new PadDraw();

    panel.setPreferredSize(new Dimension(75, 68));

    //Creates a new container
    Container content = this.getContentPane();
    content.setLayout(new BorderLayout());

    //sets the panel to the left, padDraw in the center
    content.add(panel, BorderLayout.WEST);
    content.add(drawPad, BorderLayout.CENTER);

    //add the color buttons:
    makeColorButton(Color.BLUE);
    makeColorButton(Color.MAGENTA);
    makeColorButton(Color.RED);
    makeColorButton(Color.GREEN);
    makeColorButton(Color.BLACK);
    makeColorButton(Color.WHITE);

    //creates the clear button
    JButton clearButton = new JButton("Clear");

    clearButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            drawPad.clear();
        }
    });

    panel.add(clearButton);
}

/*
 * makes a button that changes the color
 * @param color the color used for the button
 */
public void makeColorButton(final Color color) {
    JButton tempButton = new JButton();
    tempButton.setBackground(color);
    tempButton.setPreferredSize(new Dimension(16, 16));
    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
        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();

            graphics2D.drawLine(oldX, oldY, currentX, currentY);
            graphics2D.drawLine(oldX + 1, oldY + 1, currentX + 1, currentY + 1);
            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 the value of Graphics as the image
//sets the rendering
//runs the clear() method
//then it draws the image
public void paintComponent(Graphics g) {

    try {
        image = ImageIO.read(new File("C:\\Users\\user\\Desktop\\Untitled.png"));
        graphics2D = (Graphics2D)image.getGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    } catch (IOException e) { }

    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 colors as white
//then it fills the window with white
//thin 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();
}
} 

我在这里加载图片:

image = ImageIO.read(new File("C:\\Users\\user\\Desktop\\Untitled.png"));
        graphics2D = (Graphics2D)image.getGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    } catch (IOException e) { }

有谁知道问题是什么?

1 个答案:

答案 0 :(得分:4)

绘画是一个破坏性的过程。也就是说,每次调用paintComponent时,您都需要重新绘制完全从头开始显示屏幕所需的内容。

您的方法存在(至少)两个直接问题

  1. 您没有调用super.paintComponent,这会影响绘制过程更新屏幕的方式,这非常重要,因为您从JComponent扩展到默认情况下是透明的并且可能会影响该功能使框架正常运行(在整个地方留下令人讨厌的油漆工件) - 还要记住,Graphics是一个共享资源。所有其他要绘制的组件都将共享此资源,这意味着在您离开屏幕之前,您可能会得到所绘的内容。
  2. 每次调用paintComponent时,您都会重新加载图片。这意味着,你画给graphics2D的东西将会丢失。
  3. 我不打扰setDoubleBuffered(false),因为这会影响组件的更新方式,并可能产生不良结果。

    我会将您要绘制的每个点添加到某种类型的List,并在paintComponent方法中绘制此列表。

    不要在任何paintXxx方法中加载资源。这些应该事先准备好。

    我可以看到你正在“尝试”执行某种双重缓冲,但这并不是真的应该如何完成。除了问题之外你几乎没有。首先从一个简单的解决方案开始。