借鉴架自己的缓冲图像 [英] drawing your own buffered image on frame
问题描述
我有我的帧大小缓冲图像:
公众的BufferedImage IMG;
公共静态INT WIDTH = 800;
公共静态INT HEIGHT = 600;
IMG =新的BufferedImage(宽度,高度,BufferedImage.TYPE_INT_RGB);
我怎样才能画,所以我能看到的只是一个黑色图像填充框架?不使用帆布
我想只用从图形drawImage方法不使用油漆或功能的paintComponent
如果有可能,我怎么能分配一个一维数组[宽*高]到形象?
简单地说:我想创建一个形象,从数组值转换为像素(0 =黑,999999999 = lightblue等),并将其绘制到屏幕
。编辑:
这是code作为预期不起作用(它应该是上有一个黑色的绘制图像的帧),但仅仅是一个空白frame.Why图像不TOT帧添加?
进口的javax.swing *。 进口java.awt.Canvas中;
进口java.awt.Graphics;
进口java.awt.image.BufferStrategy;
进口java.awt.image.BufferedImage中;
进口java.awt.image.DataBufferInt; 公共类测试扩展画布{公共静态的JFrame框架;
公共静态INT WIDTH = 800;
公共静态INT HEIGHT = 600;公开测试(){}公共静态无效的主要(字串[] A){ 测试T =新的测试();
帧=新的JFrame(窗口);
frame.add(T);
frame.pack();
frame.setVisible(真);
frame.setSize(宽,高);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.start();}公共无效的start(){ IMG的BufferedImage =新的BufferedImage(宽度,高度,BufferedImage.TYPE_INT_RGB);
INT []个像素=((DataBufferInt)img.getRaster()getDataBuffer()。)的getData();
布尔运行= TRUE;
而(运行){
BufferStrategy中BS = this.getBufferStrategy();
如果(BS == NULL){
createBufferStrategy(4);
返回;
}
的for(int i = 0; I<宽*高;我++)
像素[I] = 0; 图形G = bs.getDrawGraphics();
g.drawImage(IMG,0,0,宽度,高度,NULL);
g.dispose();
bs.show(); }
}}
据我了解你们的实现(这是'不是很多')什么,这可能会给你一些提示。框架和图像的建设似乎仍然凌乱的我,但看看这个了。
进口java.awt.Graphics;
进口java.awt.Image中;
进口java.awt.image.BufferStrategy;
进口java.awt.image.BufferedImage中;
进口java.awt.image.DataBufferInt;
进口的javax.swing *。公共类TestImageDraw { 公共静态的JFrame框架;
IMG的BufferedImage;
公共静态INT WIDTH = 800;
公共静态INT HEIGHT = 600; 公共TestImageDraw(){
} 公共静态无效的主要(字串[] A){ TestImageDraw T =新TestImageDraw(); 帧=新的JFrame(窗口);
frame.setVisible(真); t.start();
frame.add(新的JLabel(新的ImageIcon(t.getImage()))); frame.pack();
// frame.setSize(宽度,高度);
//优于EXIT处置
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} 公众形象的getImage(){
返回IMG;
} 公共无效的start(){ IMG =新的BufferedImage(宽度,高度,BufferedImage.TYPE_INT_RGB);
INT []个像素=((DataBufferInt)img.getRaster()getDataBuffer()。)的getData();
布尔运行= TRUE;
而(运行){
BufferStrategy中BS = frame.getBufferStrategy();
如果(BS == NULL){
frame.createBufferStrategy(4);
返回;
}
的for(int i = 0; I<宽*高;我++)
像素[I] = 0; 图形G = bs.getDrawGraphics();
g.drawImage(IMG,0,0,宽度,高度,NULL);
g.dispose();
bs.show(); }
}
}
一般提示
- 请使用code块一致性和逻辑性缩进。
- 请学会共同的Java命名约定(特别是情况下用于名称)类,方法和放大器;属性名和放大器;一直使用它。
- 给测试类一个有意义的名字,例如:
TestImageDraw
。 - 创建并在美国东部时间更新的Swing GUI的。
- 请不要混用秋千&安培; AWT组件没有很好的理由。
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
- 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.
- Give test classes a meaningful name e.g.
TestImageDraw
. - Create and update Swing GUIs on the EDT.
- Don't mix Swing & AWT components without good reason.
这篇关于借鉴架自己的缓冲图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!