Jlabel ImageIcon以负坐标绘制

时间:2012-06-30 15:14:31

标签: java swing graphics jlabel imageicon

我的图形引擎库中有这段代码:

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;

};
 }

和我的主java文件中的这段代码:

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

我在这里有两个问题:

1 - &gt;窗口上的图像显示在负坐标上(矩形不是100x100)

如果我重新调整窗口大小,图像将尝试在0 0坐标处绘制,然后再次以负坐标绘制。
enter image description here

2 - &gt;我得到2个不同的错误:

a)组件必须是Graphics g= bs.getDrawGraphics();的有效同行    b)尚未在bs.show();

创建缓冲区

这些问题是什么?

我在YouTube上this channel看到他使用了Canvas和东西,但他没有收到任何错误(我知道不会把挥杆与awt混合)

修改

 //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();
            }
        }

2 个答案:

答案 0 :(得分:4)

您的Graphics上下文g在主机的对等组件存在之前无效。抛出异常是因为它会导致程序严重问题,无法随意写入主机内存或设备。

相反,覆盖paintComponent(),如herehere所示,您可以在本地坐标中完全控制组件的几何体。

答案 1 :(得分:2)

坐标没有问题。看来你正在使用错误的对象。如果从整个JFrame的顶部测量,则正方形正好为100x100,因此负坐标不是问题。将组件添加到JFrame的内容窗格中,而不是添加到JFrame本身。

替换:

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

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

可能还有更多内容,但这肯定是起点。如果出现其他问题,请咨询Javadoc