0

I've loaded an image in java but it looks like it zoomed in as the edges have gone in the imagethe image

the java view

there is what is loading I have tried many methods of trying to fix it. I'm trying to make a windows 95 text based adventure in java, all is going well but the said image loading issue.

Here's the code, please ignore the messiness

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

}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 2
    Don't paint on the frame. Paint on a JPanel (with `paintComponent` not `paint`) and add the panel to the frame. You're image is getting cut off because 0,0 refers to the very top left corner of the actual frame and not its content view area. Also override `getPreferredSize()` of the panel, and `pack()` your frame instead of setting its size. – Paul Samsotha Aug 28 '14 at 12:42

1 Answers1

1

You have to do two tricks

  • draw to ContentPane and not to frame directly
  • resize the ContentPane and not the frame

    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);
        }
    }
    
j123b567
  • 3,110
  • 1
  • 23
  • 32
  • 1
    [Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod Aug 28 '14 at 19:40