2014-08-28 45 views
0

我已經加載Java中的形象,但它看起來像它放大的邊緣去了圖像中the imageJava的形象出現放大

the java view

有什麼加載 我曾嘗試許多方法試圖解決它。 我想在java中做一個基於Windows 95文本的冒險,一切都很順利,但所述的圖像加載問題。

下面的代碼,請忽略雜亂

package com.TTG1.screen; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.image.BufferedImage; 

import javax.imageio.ImageIO; 
import javax.swing.JFrame; 

public class Game extends JFrame{ 

Image dbImage; 
Graphics dbg; 

double startTime; 
double tempTime; 

Thread thread = new Thread(); 

public Color WIN95GREEN = new Color(33, 135, 99); 

public int WIDTH = 320; 
public int HEIGHT = 240; 
public int SCALE = 2; 

public boolean boot = true; 

BufferedImage bu; 

BufferedImage tb; 

BufferedImage d; 

public Game() { 
    setSize(WIDTH * SCALE, HEIGHT * SCALE); 
    setVisible(true); 
    setBackground(Color.white); 
    setTitle("Swodniw 21"); 
    setResizable(false); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLocationRelativeTo(null); 
    setStartTime(); 
    loopTime(); 

} 

public void paint(Graphics g){ 
    dbImage = createImage(getWidth(), getHeight()); 
    dbg = dbImage.getGraphics(); 
    draw(dbg); 
    g.drawImage(dbImage, 0, 0, this); 
} 

public void setStartTime(){ 
    if(boot){ 
    startTime = System.currentTimeMillis(); 
    System.out.println("Time Set!"); 
    } 
} 

public void draw(Graphics g){ 
    if(boot){ 
     try{ 
      bu = ImageIO.read(getClass().getResource("/images/load.png")); 
      d = ImageIO.read(getClass().getResource("/images/desktop.png")); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    g.drawImage(bu, 0, 0, null); 
    } 
    else{ 
     g.drawImage(d, 0, 0, null); 

    } 
    repaint(); 
} 

public static void main(String[] args){ 
    Game game = new Game(); 
} 

public void loopTime(){ 
    while(boot){ 
     System.out.println(tempTime - startTime); 
     tempTime = System.currentTimeMillis(); 
     if(tempTime - startTime > 5000){ 
      boot = false; 
      System.out.print("boot set to false"); 
     } 
    } 
} 

} 
+2

不要在框架上塗漆。在JPanel上繪製(使用'paintComponent'而不是'paint')並將面板添加到框架。您的圖像正在被切斷,因爲0,0指的是實際框架的最左上角,而不是其內容視圖區域。同時重寫面板的'getPreferredSize()',並且'pack()'你的幀而不是設置它的大小。 – 2014-08-28 12:42:04

回答

1

你要做的兩個技巧

  • 平局的contentPane,而不是框架直接
  • 調整的contentPane,而不是框架

    public Game() { 
        setContentPane(new DrawPanel()); 
        getContentPane().setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 
        pack(); 
    
        ... 
        //setSize(WIDTH * SCALE, HEIGHT * SCALE); 
        ... 
    } 
    
    class DrawPanel extends JPanel { 
    
        @Override 
        public void paintComponent(Graphics g) { 
         dbImage = createImage(getWidth(), getHeight()); 
         dbg = dbImage.getGraphics(); 
         draw(dbg); 
         g.drawImage(dbImage, 0, 0, this); 
        } 
    } 
    
+1

[當您確實想要覆蓋'getPreferredSize()'](http://stackoverflow.com/q/7229226/230513)時,不要使用'setPreferredSize()'。 – trashgod 2014-08-28 19:40:32