2015-12-24 108 views
0

我一直試圖在畫布上畫,但我不能讓它工作,我可以看到JFrame,但它似乎並沒有調用添加Mover()對象時的繪畫方法到它。這是第一次使用畫布,所以我不知道我錯過了什麼。這裏是代碼:Can not canvas on canvas

import java.awt.image.BufferedImage; 
import java.awt.image.DataBufferByte; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.*; 
import java.util.*; 
import java.awt.*; 
import java.io.File; 

public class Move extends Canvas 
{ 

    private static int [][]imgRGB; 

    public Move() 
    { 
     try 
     { 
      BufferedImage hugeImage = ImageIO.read(new File("C:/Users/pc/Pictures/Nave.gif")); 
      imgRGB = convertToRGB(hugeImage); 

     } 
     catch(IOException e) 
     { 
      System.out.println(e); 
     } 
    } 

    public void Paint(Graphics g) 
    { 
     super.paint(g); 

     for(int i=0 ; i<imgRGB.length ; i++) 
     { 
      for(int j=0 ; j<imgRGB[i].length; j++) 
      { 
       g.setColor(new Color(imgRGB[i][j])); 
       g.drawLine(i,j,i,j); 
      } 
     } 
    } 

    private static int[][] convertToRGB(BufferedImage image) { 

     final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 
     final int width = image.getWidth(); 
     final int height = image.getHeight(); 
     final boolean hasAlphaChannel = image.getAlphaRaster() != null; 

     int[][] result = new int[height][width]; 
     if (hasAlphaChannel) { 
     final int pixelLength = 4; 
     for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) { 
      int argb = 0; 
      argb += (((int) pixels[pixel] & 0xff) << 24); // alpha 
      argb += ((int) pixels[pixel + 1] & 0xff); // blue 
      argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green 
      argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red 
      result[row][col] = argb; 
      col++; 
      if (col == width) { 
       col = 0; 
       row++; 
      } 
     } 
     } else { 
     final int pixelLength = 3; 
     for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) { 
      int argb = 0; 
      argb += -16777216; // 255 alpha 
      argb += ((int) pixels[pixel] & 0xff); // blue 
      argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green 
      argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red 
      result[row][col] = argb; 
      col++; 
      if (col == width) { 
       col = 0; 
       row++; 
      } 
     } 
     } 

     return result; 
    } 

    public static void main(String[] args) 
    { 
     JFrame container = new JFrame("pixel"); 
     container.add(new Move()); 
     container.setSize(400,400); 
     container.setVisible(true); 
     container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    } 

} 

回答

1
public void Paint(Graphics g) 

方法名稱區分大小寫。您應該覆蓋paint(...)

始終使用@Override註釋,當你試圖重寫不存在的方法,編譯器會通知你:

@Override 
public void paint(Graphics g) 
{ 
... 
} 

但是,你不應該在一個Swing應用程序覆蓋帆布。

相反,您應該擴展JPanel,然後您應該覆蓋paintComponent(...)方法。

閱讀從Custom Painting Swing的教程部分獲取更多信息和工作的例子。

+0

我正在做一些實驗,在同一時間繪製多個對象。我應該堅持畫布還是使用JPanel? –