0
我已經加載Java中的形象,但它看起來像它放大的邊緣去了圖像中Java的形象出現放大
有什麼加載 我曾嘗試許多方法試圖解決它。 我想在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");
}
}
}
}
不要在框架上塗漆。在JPanel上繪製(使用'paintComponent'而不是'paint')並將面板添加到框架。您的圖像正在被切斷,因爲0,0指的是實際框架的最左上角,而不是其內容視圖區域。同時重寫面板的'getPreferredSize()',並且'pack()'你的幀而不是設置它的大小。 – 2014-08-28 12:42:04