Note that there are some explanatory texts on larger screens.

plurals
  1. POJlabel ImageIcon is drawn in negative coordinates
    primarykey
    data
    text
    <p>I have this piece of code from my Graphics Engine library:</p> <pre><code>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 &lt; WIDTH * HEIGHT; i++) pixels[i] =0; }; } </code></pre> <p>and this piece of code in my main java file:</p> <pre><code>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(); } } } </code></pre> <p><strong>I have 2 problems here:</strong></p> <p>1-->The image on the window is displayed on negative coordinates(the rectangle is not 100x100)</p> <p>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.<br> <img src="https://i.stack.imgur.com/AFKxK.png" alt="enter image description here"></p> <p>2-->I get 2 different errors:</p> <p>a)Component must be a valid peer at <code>Graphics g= bs.getDrawGraphics();</code> b)Buffers have not been created at <code>bs.show();</code></p> <p>What are these problems?</p> <p>I saw on YouTube on <a href="http://www.youtube.com/playlist?list=ELmvNdIhZY1t4&amp;feature=plcp" rel="nofollow noreferrer">this channel</a> he used Canvas and stuff but he is not getting any errors (I know about not mixing the swing with the awt) </p> <p><strong>EDIT</strong></p> <pre><code> //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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload