Ionel Lupu
Ionel Lupu

Reputation: 2801

Jlabel ImageIcon is drawn in negative coordinates

I have this piece of code from my Graphics Engine library:

package WG;


import java.awt.*;
import java.awt.image.*;

import javax.swing.*;

public class window {

public static boolean running=true;

public static int WIDTH = 800, HEIGHT = 600;
public static String TITLE = "New Window";
public static JFrame frame;

public static int[] pixels;
public static BufferedImage img;
public static Thread thread;

public window(){}

public static void create() {

    img = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
    pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();

    frame = new JFrame("WINDOW");  
    //frame.setResizable(false);
    frame.setLayout(new FlowLayout(FlowLayout.LEADING,0,0));
    frame.add(new JLabel(new ImageIcon(img)));
    frame.pack();

    //frame.setSize(WIDTH, HEIGHT);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

public void render() {
        BufferStrategy bs=frame.getBufferStrategy();
        if(bs==null){
            frame.createBufferStrategy(2);
            return;
        }

        Graphics g= bs.getDrawGraphics();  
        g.drawImage(img, 0,0, WIDTH, HEIGHT, null);
        g.dispose();
        bs.show();
}

public static void clear_screen(){
    for (int i = 0; i < WIDTH * HEIGHT; i++)
        pixels[i] =0;

};
 }

and this piece of code in my main java file:

import WG.*;

public class Main_window{

private static boolean running = true;
public static window wind = new window();
public static Thread thread;

public static void main(String[] args) {
    window.create();
    start();
}
public static void start() {
    while (running) {
        window.clear_screen();
        Forms.drawRect(0, 0, 100, 100);//my own method
        wind.render();
    }
}
 }

I have 2 problems here:

1-->The image on the window is displayed on negative coordinates(the rectangle is not 100x100)

If I re-size the window the image is trying to be drawn at 0 0 coordinates but then again is drawn at negative coordinates.
enter image description here

2-->I get 2 different errors:

a)Component must be a valid peer at Graphics g= bs.getDrawGraphics(); b)Buffers have not been created at bs.show();

What are these problems?

I saw on YouTube on this channel he used Canvas and stuff but he is not getting any errors (I know about not mixing the swing with the awt)

EDIT

 //from graphics library
    @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
            g.dispose();
        }
    //from the main file
    public static void start() {
            while (running) {
                window.clear_screen();  
                Forms.drawRect(0, 0, 100, 100);//my own method
                wind.frame.repaint();
            }
        }

Upvotes: 1

Views: 679

Answers (2)

trashgod
trashgod

Reputation: 205795

Your Graphics context, g, is not valid until the host's peer component is extant. The exceptions are thrown because it would cause serious problems for a program to write into host memory or devices at will.

Instead, override paintComponent(), as shown here and here, where you have complete control over the component's geometry in local coordinates.

Upvotes: 4

toniedzwiedz
toniedzwiedz

Reputation: 18563

There's nothing wrong with coordinates. It seems you're using the wrong object. The square is exactly 100x100 if measured from the top of the entire JFrame so negative coordinates are not an issue. Add components to the JFrame's content pane and not to the JFrame itself.

Replace:

frame.add(new JLabel(new ImageIcon(img)));

with

frame.getContentPane().add(new JLabel(new ImageIcon(img)));

There might be something more to it but this is certainly the starting point. Consult the Javadoc in case of further problems

Upvotes: 2

Related Questions