2015-04-23 79 views
1

我真的不知道我做錯了什麼。 (儘管我確信它有點不同)。我一直在努力調整我的代碼幾個小時,但無濟於事。圖像沒有出現在Swing中?

我試圖剪下一張圖片並顯示它們。之後,我會隨機分配他們所在的位置,並嘗試將它們放在正確的位置。但是,現在我遇到的問題是我的JPanel上出現了任何問題。有一次,我的測試代碼顯示了圖像。但是,現在,相同的測試代碼不起作用。

如果你能看到我做錯了/問題在哪裏,請告訴我。希望這是簡單而愚蠢的。

import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.geom.*; 
import java.awt.image.BufferedImage; 
import java.io.*; 
import java.util.*; 
import javax.imageio.*; 
import javax.swing.*; 

public class Lab10 { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     MyFrame frame = new MyFrame(); 
     MyPanel panel = new MyPanel(); 
     frame.add(panel); 
     panel.setFocusable(true); 


    } 

} 

class MyFrame extends JFrame{ 
    MyFrame(){ 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
     setSize(800,800); 
     setResizable(false); 
    } 
} 

class MyPanel extends JPanel{ 
    public static final int SIZE = 4; 
    private int oldx = -1; 
    private int oldy = -1; 
    private final String fileName = "houseTyrell.png"; 

    public ImageArray imageArray = new ImageArray(fileName, SIZE); 

    MyPanel(){ 

     addMouseListener(new MouseAdapter(){ 

      public void mousePressed(MouseEvent e){ 
       if(e.getButton()==1){ 
        if(oldx == -1){ 
         oldx = e.getX(); 
         oldy = e.getY(); 
        }else{ 
         //imageArray.swapPoints(oldx, oldy, e.getX(), e.getY()); 
         oldx = -1; 
         oldy = -1; 
         repaint(); 
        } 
       } 
      } 
     }); 
    } 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g; 
     imageArray.draw(g2); 

     /*Image image2; 
     try { 
      image2 = ImageIO.read(new File("houseTyrell.png")); 
      System.out.println("I should print"); 

      try{ 
       g2.drawImage(image2, 0, 0, null); 
      } catch(Exception my){ 
       System.out.println("Drawing issue"); 
      } 

     } catch (IOException ex) { 
      System.out.println("Reading issue"); 

     }*/ 

    } 
} 


class ImageArray{ 
    private Square[][] squares; 
    private String fileName; 
    private int size; 
    private int w; 
    private int h; 

    public ImageArray(String fileName, int size) { 
     this.size = size; 
     this.fileName= fileName; 
     squares = new Square[size][size]; 
     try { 
      BufferedImage image = ImageIO.read(new File(fileName)); 
      w = image.getWidth()/size; 
      h = image.getHeight()/size; 
      for (int row = 0; row < size; row++) { 
       for (int col = 0; col < size; col++) { 
        squares[row][col] = new Square(
        image.getSubimage(row * w , col * h , w, h), row, col, w, h); 
       } 
      } 

     } catch (Exception e) { 
      System.out.println("Can't open file!"); 
     } 
     shuffle(); 
     } 

    //TODO 
    public void shuffle(){ 
     for(int i = 0; i < size * size; i++){ 

     } 
    } 

    //TODO 
    public void swapPoints(int oldx, int oldy, int newx, int newy){ 

    } 

    public void draw(Graphics2D g2){ 
     for(int i = 0; i < squares.length; i++){ 
      for(int j = 0; j < squares[0].length; j++){ 
       Square square = squares[i][j]; 
       square.draw(g2); 

      } 
     } 
    } 

} 

class Square{ 
    private BufferedImage image; 
    private int row; 
    private int col; 
    private int x,y; 
    private int w; 
    private int h; 

    Square(BufferedImage image, int row, int col, int w, int h){ 
     this.image = image; 
     this.row = row; 
     this.col = col; 
     this.x = row * w; 
     this.y = col * h; 
     this.w = w; 
     this.h = h; 
    } 

    public void draw(Graphics2D g2){ 
     try{ 
      g2.drawImage(image, x, y, null); 
     } catch (Exception my){ 
      System.out.println("Square issue"); 
     } 
    } 

    public BufferedImage getImage(){ 
     return image; 
    } 

    public int getRow(){ 
     return row; 
    } 

    public int getCol(){ 
     return col; 
    } 
} 
+0

你閱讀圖像的部分被評論....嘗試後首先取消註釋 – CoderNeji

+0

哈哈,是的,我知道。即使沒有註釋,它也不適用於我。我剛剛評論它,因爲它不是我的主要焦點。 –

+0

kk當然,我正在調查,然後 – CoderNeji

回答

3

您正在幀可見之前你已經完成建立UI,嘗試在框架上調用invalidatevalidaterepaint後,你已經添加了panel ...

public static void main(String[] args) { 
    MyFrame frame = new MyFrame(); 
    MyPanel panel = new MyPanel(); 
    frame.add(panel); 
    panel.setFocusable(true); 
    frame.invalidate(); 
    frame.validate(); 
    frame.repaint(); 

} 

坦率地說,你的MyFrame課是做的很少(其他然後讓你的生活更加困難),我會考慮擺脫它,也許用一個建造者的方法來代替它,它返回一個不可見的框架......

您還應該從Event Dispatching Thread的上下文中創建UI,這可以幫助解決其他「奇怪」問題。

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       ex.printStackTrace(); 
      } 

      MyFrame frame = new MyFrame(); 
      MyPanel panel = new MyPanel(); 
      frame.add(panel); 
      panel.setFocusable(true); 
      frame.invalidate(); 
      frame.validate(); 
      frame.repaint(); 
     } 
    }); 
} 

更多細節

+0

謝謝,這工作!你介意解釋什麼是無效/驗證方法嗎?我還沒有看到他們。 我會嘗試擺脫MyFrame類。感謝您的建議! –

+1

他們幾乎做他們聽起來像,本質上,無效和驗證佈局遏制層次,這通常導致所有組件(和子組件)重新佈局 – MadProgrammer

+0

真棒,感謝您的幫助。我不能相信這是很容易解決的,但我很高興它是。 –

1

課堂Lab10的主要方法見Initial Threads,JFrame中前初始化的JPanel,它應該工作。 PS:確保存在引用「houseTyrell.png」的圖片或臨時使用完全限定路徑進行測試(即c://test//houseTyrell.png)。

+0

謝謝。我混淆了一段時間的形象。我想我應該剛剛使用了一條道路,哈哈。 –

+0

不客氣:) – m3nation