2013-07-24 46 views
1

所以我有一個基本上像繪畫但用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) { } 

有誰知道問題是什麼?

+0

在你的發佈代碼中你有幾件永不發生的事件:你不應該*加載文件或者在'paintComponent'中做任何耗時的事情。你應該永遠不要忽視例外,因爲你正在做。您應該從不*在'paintComponent(...)'內調用'repaint()'。考慮先閱讀一些教程,因爲看起來你可能猜測的東西不應該被猜到。 –

回答

4

繪畫是一個破壞性的過程。也就是說,每次調用paintComponent時,都需要重新繪製完成從頭開始顯示屏幕所需的任何內容。

有(至少)與你的方法有兩個直接的問題

  1. 你是不是叫super.paintComponent,這將影響在油漆工藝更新屏幕的方式,這非常重要,因爲你已經從延長JComponent默認情況下它是透明的,並且可能會損害框架正常運行的能力(在整個位置留下令人討厭的油漆痕跡) - 還記得,Graphics是一個共享資源。所有其他將被繪製的組件都將共享此資源,這意味着您可以結束在屏幕上留下的任何東西。
  2. 您正在重新載入圖片每次調用paintComponent。這意味着,你所畫的graphics2D將會丟失。

我不打擾setDoubleBuffered(false),因爲這會影響組件更新的方式,並可能產生不良結果。

我會將每個要繪製的點添加到某種List並在paintComponent方法中繪製該列表。

請勿在任何paintXxx方法中加載資源。這些應該事先準備好。

我可以看到你正在「嘗試」執行某種雙緩衝,但這不是應該如何完成的。除了那些問題之外,你從中得不到什麼。首先從簡單的解決方案開始。