在java中渲染图像

时间:2016-04-16 10:50:57

标签: java awt rendering bufferstrategy

我试图在java JFrame中放置图片,但我想修改每个像素,所以我想逐个像素地加载它。我被困在应该将像素加载到屏幕的部分,但我认为没有效果。我错过了什么?

我很确定它有点小,但由于某些原因我无法看到它。

Display.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();


    }
}

Screem.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);
}

}

Render.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;
            }
        }
    }
}

}

Image.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);

    }
}
}

0 个答案:

没有答案