我试图在java JFrame中放一张图片,但我想修改每个像素,所以我想逐个像素地加载它。我被困在应该将像素加载到屏幕上的部分,但我看不到任何效果。我错过了什么?
我很确定它很小,但由于某种原因我看不到它。
显示.java
public class Display extends Canvas implements Runnable{
public static int WIDTH=1280;
public static int HEIGHT=800;
public static final String TITLE = "rainbow effect";
private Thread thread;
private boolean running = false;
private Screen screen;
private BufferedImage img;
private int pixels[];
public Display(){
Dimension size = new Dimension(WIDTH,HEIGHT);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
screen = new Screen(WIDTH, HEIGHT);
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
}
@Override
public void run() {
while(running){
requestFocus();
render();
}
}
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
//System.out.println("working...");
}
public synchronized void stop(){
if(!running)
return;
running = false;
try{
thread.join();
} catch(Exception e){
e.printStackTrace();
System.exit(0);
}
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
createBufferStrategy(3);
return;
}
screen.render();
for(int i=0; i<WIDTH*HEIGHT; i++)
{
pixels[i] = screen.pixels[i];
}
//-------------------EDIT------------//
Graphics g = bs.getDrawGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
g.dispose();
//--------------END OF EDIT-----------//
bs.show();
}
public static void main(String[] args){
//BufferedImage cursor = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
//Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(0,0), "blank");
Display game = new Display();
JFrame frame = new JFrame();
frame.add(game);
frame.setTitle(TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
//frame.getContentPane().setCursor(blank);
frame.setVisible(true);
System.out.println("running ...");
game.start();
}
}
屏幕截图.java
public class Screen extends Render {
private Render test;
public Screen(int width, int height) {
super(width, height);
Random random = new Random();
//test = new Render(width, height);
test = new Render(width, height);
for(int i=0; i<width*height; i++){
test.pixels[i] = Image.img.pixels[i];
}
}
public void render(){
for(int i =0 ; i <width*height;i++){
pixels[i]=0;
}
test.draw(test, 0, 0);
}
}
渲染.java
public class Render {
public final int width, height;
public final int [] pixels;
public Render(int width, int height){
this.width = width;
this.height = height;
pixels = new int[width * height];
}
public void draw(Render render, int xOffset, int yOffset){
for(int y=0; y<render.height; y++){
int yPix = y+yOffset;
if(yPix<0 || yPix >=height){
continue;
}
for(int x=0; x < render.width; x++){
int xPix = x+xOffset;
if(xPix < 0 ||yPix >=width){
continue;
}
int alpha = render.pixels[x+y*render.width];
if(alpha >0)
{
pixels[xPix+yPix*width]=alpha;
}
}
}
}
}
图像.java
public class Image {
public static Render img = loadBitmap("/images/sample2.jpg");
public static Render loadBitmap(String fileName){
try{
BufferedImage image = ImageIO.read(Image.class.getResource(fileName));
int width = image.getWidth();
int height = image.getHeight();
Render result = new Render(width, height);
image.getRGB(0, 0, width, height, result.pixels, 0 ,width);
return result;
}catch(Exception e){
System.out.println("crash");
throw new RuntimeException(e);
}
}
}