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
I want to use only the drawImage function from graphics without using the paint or paintComponent functions
If it is possible, how can i assign an 1D array [WIDTH*HEIGHT] to that image?
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.
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
TestImageDraw
. 上一篇: 使用openCV在avi视频上绘制盒子
下一篇: 在框架上绘制自己的缓冲图像