Java图像出现了放大

时间:2014-08-28 12:20:56

标签: java image swing graphics

我已经在java中加载了一个图片但看起来它已经放大了,因为边缘已经出现在图像中the image

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");
        }
    }
}

}

1 个答案:

答案 0 :(得分: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);
        }
    }