1

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();
            }
        }
Ionel Lupu
  • 2,695
  • 6
  • 29
  • 53
  • 5
    1) Remove all the `static` (they are not the cause of your problems but they are also unnecessary and the sign of a poor design). 2) Use Java conventions (Class start with UC, variables and methods start with an LC, package contain only LC, etc...) 3) Explain what you are trying to do because it seems like you are doing rather complex operations to get a rather simlpe result. You are mixing a bunch of different stuffs which results in tons of bugs. 4) Never perform graphical operations outside the EDT (Event Dispatching Thread)! – Guillaume Polet Jun 30 '12 at 15:52
  • I'm trying to make a graphics engine library by converting an array into an image(array to image) and draw it on the screen.It works at TheCherno. – Ionel Lupu Jun 30 '12 at 15:59
  • Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. – trashgod Jun 30 '12 at 18:27

2 Answers2

4

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Now I use the paintComponent() but when I want to repaint() it ,is flickering (I have the clear_screen() method).How can I clean the image and paint again on it? – Ionel Lupu Jun 30 '12 at 22:37
  • Please edit your question to include an [sscce](http://sscce.org/), otherwise we're just guessing. – trashgod Jul 01 '12 at 00:20
  • Still guessing, your `start()` appears to be [incorrectly synchronized](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jul 01 '12 at 13:09
2

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

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
  • But why do I get that rectangle thing instead of a square?I replaced that line but nothing changed. – Ionel Lupu Jun 30 '12 at 16:49
  • @boyd because you're referring to the Graphics object of JFrame. I can't rewrite your entire code. As I said, the main problem is that you're using the wrong object. Be careful when you get Graphics or BufferStrategy. BTW, once you have the JLabel, it's not necessary to draw the image yourself, as you do in your *render* method. Also, all the drawing should be handled by the frame itself. Don't do it from another thread. – toniedzwiedz Jun 30 '12 at 16:59
  • +1 for proposing `JComponent`; sorry I overlooked this earlier. @boyd: it look's like you're overriding `paint()`, so the frame's decorations hide some of the square. – trashgod Jul 01 '12 at 00:17