简体   繁体   English

在帧上绘制自己的缓冲图像

[英]drawing your own buffered image on frame

I have a buffered image with the sizes of my frame: 我有一个缓冲的图像与我的框架的大小:

public BufferedImage img;
public static int WIDTH = 800;
public static int HEIGHT = 600;
img=new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);

How can I draw it so I can see just a black image filling the frame? 我怎么画它所以我只能看到填充框架的黑色图像? without using Canvas 不使用Canvas

I want to use only the drawImage function from graphics without using the paint or paintComponent functions 我想在不使用paint或paintComponent函数的情况下仅使用图形中的drawImage函数

If it is possible, how can i assign an 1D array [WIDTH*HEIGHT] to that image? 如果可能,我如何为该图像分配1D阵列[WIDTH * HEIGHT]?

SIMPLY: I want to create an image ,convert the values from an array to pixels (0=black,999999999=lightblue etc.) and draw it to the screen. 简单:我想创建一个图像,将数组中的值转换为像素(0 =黑色,999999999 =浅蓝色等)并将其绘制到屏幕上。

EDIT: 编辑:

This is the code that does not work as expected (it should be a frame with a black drawn image on it) but is just a blank frame.Why the image is not added tot the frame? 这是不能按预期工作的代码(它应该是一个带有黑色绘制图像的框架),但它只是一个空白框架。为什么图像没有添加到框架中?

在此输入图像描述

  import javax.swing.*;

  import java.awt.Canvas;
  import java.awt.Graphics;
  import java.awt.image.BufferStrategy;
  import java.awt.image.BufferedImage;
  import java.awt.image.DataBufferInt;

  public class test extends Canvas{

public static JFrame frame;
public static int WIDTH = 800;
public static int HEIGHT = 600;

public test(){

}

public static void main(String[] a){

        test t=new test();
        frame = new JFrame("WINDOW");
        frame.add(t);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.start();

}

public void start(){

    BufferedImage img = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
    int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
    boolean running=true;
    while(running){
        BufferStrategy bs=this.getBufferStrategy();
        if(bs==null){
            createBufferStrategy(4);
            return;
        }
        for (int i = 0; i < WIDTH * HEIGHT; i++)
            pixels[i] = 0;

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

    }
}}

As far as I understand what you are trying to achieve (which is 'not a lot'), this might give you some tips. 据我了解你想要实现的目标(这不是很多),这可能会给你一些提示。 The construction of the frame and image still seems untidy to me, but have a look over this. 框架和图像的构造对我来说似乎不整齐,但看看这个。

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.*;

public class TestImageDraw {

    public static JFrame frame;
    BufferedImage img;
    public static int WIDTH = 800;
    public static int HEIGHT = 600;

    public TestImageDraw() {
    }

    public static void main(String[] a){

        TestImageDraw t=new TestImageDraw();

        frame = new JFrame("WINDOW");
        frame.setVisible(true);

        t.start();
        frame.add(new JLabel(new ImageIcon(t.getImage())));

        frame.pack();
//      frame.setSize(WIDTH, HEIGHT);
        // Better to DISPOSE than EXIT
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public Image getImage() {
        return img;
    }

    public void start(){

        img = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
        int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
        boolean running=true;
        while(running){
            BufferStrategy bs=frame.getBufferStrategy();
            if(bs==null){
                frame.createBufferStrategy(4);
                return;
            }
            for (int i = 0; i < WIDTH * HEIGHT; i++)
                pixels[i] = 0;

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

        }
    }
}

General Tips 一般提示

  • Please use a consistent and logical indent for code blocks. 请对代码块使用一致的逻辑缩进。
  • Please learn common Java naming conventions (specifically the case used for the names) for class, method & attribute names & use it consistently. 请学习类,方法和属性名称的常见Java命名约定 (特别是用于名称的情况)并始终如一地使用它。
  • Give test classes a meaningful name eg TestImageDraw . 为测试类提供有意义的名称,例如TestImageDraw
  • Create and update Swing GUIs on the EDT. 在EDT上创建和更新Swing GUI。
  • Don't mix Swing & AWT components without good reason. 没有充分理由不要混合使用Swing和AWT组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM