2015-04-12 76 views
0

我當前想要使用打開按鈕來打開JFileChooser並選擇一個圖像,然後將它繪製在applet左側的JPanel上,我知道文件正在檢索,但是當我去重新繪製圖形上下文沒有任何反應。提前致謝。repaint()方法不爲我的程序調用paintComponent()

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.JFrame; 

public class FinalProject390 { 

    public static void main(String[] args) { 
     createAndShowGUI(); 

    } 

    private static void createAndShowGUI() { 
     JFrame f = new JFrame("Final Project"); 

     f.setSize(1025, 520); 

     f.add(new GraphicsPanel()); 
     f.add(new MainPanel()); 
     f.setVisible(true); 

    } 
} 

class MainPanel extends JPanel implements ActionListener { 
    JButton openButton = new JButton("Open"); 
    JButton newButton = new JButton("New"); 
    JButton saveButton = new JButton("Save"); 
    JButton saveAsButton = new JButton("Save As"); 
    JButton drawLineButton = new JButton("Draw Line"); 
    JButton drawRectangleButton = new JButton("Draw Rectangle"); 
    JButton drawOvalButton = new JButton("Draw Oval"); 
    JButton drawArcButton = new JButton("Draw Arc"); 
    JButton extractPixelDataButton = new JButton("Extract Pixel Data"); 
    JButton convoluteButton = new JButton("Convolute"); 
    JTextField xPosStartField = new JTextField("x-Position Start"); 
    JTextField yPosStartField = new JTextField("y-Position Start"); 
    JTextField xPosEndField = new JTextField("x-Position End"); 
    JTextField yPosEndField = new JTextField("y-Position End"); 
    JTextField angleStartField = new JTextField("Angle Start"); 
    JTextField angleSizeField = new JTextField("Angle Size"); 

    public MainPanel() { 

     openButton.setBounds(660, 50, 100, 30); 
     openButton.addActionListener(this); 
     newButton.setBounds(780, 50, 100, 30); 
     saveButton.setBounds(900, 50, 100, 30); 
     drawLineButton.setBounds(660, 110, 160, 30); 
     drawRectangleButton.setBounds(840, 110, 160, 30); 
     drawOvalButton.setBounds(660, 155, 160, 30); 
     drawArcButton.setBounds(840, 155, 160, 30); 
     extractPixelDataButton.setBounds(660, 220, 160, 30); 
     convoluteButton.setBounds(840, 220, 160, 30); 

     xPosStartField.setBounds(660, 280, 100, 30); 
     yPosStartField.setBounds(660, 320, 100, 30); 
     xPosEndField.setBounds(660, 380, 100, 30); 
     yPosEndField.setBounds(660, 420, 100, 30); 
     angleStartField.setBounds(900, 280, 100, 30); 
     angleSizeField.setBounds(900, 320, 100, 30); 

     setLayout(null); 
     setBackground(Color.green); 
     setBounds(641, 0, 370, 480); 

     add(openButton); 
     add(newButton); 
     add(saveButton); 
     add(drawLineButton); 
     add(drawRectangleButton); 
     add(drawOvalButton); 
     add(drawArcButton); 
     add(extractPixelDataButton); 
     add(convoluteButton); 
     add(xPosStartField); 
     add(yPosStartField); 
     add(xPosEndField); 
     add(yPosEndField); 
     add(angleStartField); 
     add(angleSizeField); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     javax.swing.JFileChooser fileChooser = new JFileChooser(); 
     BufferedImage bin = null, bi = null; 
     GraphicsPanel gPanel = new GraphicsPanel(); 

     fileChooser.setCurrentDirectory(new File(System 
       .getProperty("user.home"))); 

     int result = fileChooser.showOpenDialog(null); 

     if (result == javax.swing.JFileChooser.APPROVE_OPTION) { 
      File selectedFile = fileChooser.getSelectedFile(); 
      System.out.println("Selected file: " 
        + selectedFile.getAbsolutePath()); 
      try { 
       bin = ImageIO.read(new File(selectedFile.getAbsolutePath())); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 

      gPanel.setImg(bin); 

     } 

    } 
} 
    class GraphicsPanel extends JPanel { 
     BufferedImage bi = null, bin = null; 

     public GraphicsPanel() { 
      setLayout(null); 
      setBackground(Color.blue); 
      setSize(640, 480); 
      setLocation(0, 0); 

     } 

     public void setImg(BufferedImage b) { 
      this.bi = b; 
      repaint(); 

     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      g.drawImage(bi, 0, 0, null); 

     } 
    } 

回答

0

這看起來像一個家庭作業,因此而不是代碼直接回答,而是請考慮以下因素:

  • 在你的方法createAndShowGUI()你實例化一個GraphicsPanel對象,並將其添加到JFrame
  • 在你actionPerformed()方法,實例化另一個GraphicsPanel對象(這是從未被添加到JFrame),並調用setImage()

您的圖片不顯示,因爲已被添加到您的JFrame控制GraphicsPanel是不是您所設置的圖像同樣GraphicsPanel控制。

我希望這足以幫助您修復代碼。

+0

你是對的這是一個家庭作業,謝謝你沒有明確說明需要做什麼,並給我必要的信息弄清楚。 –

相關問題