1

I'm trying to put a picture in java JFrame, but i want to modify every pixel, so i want to load it pixel by pixel. I'm stuck at part where pixels should be loaded to the screen but i see no effect. What did i miss?

I'm pretty sure it's something small but for some reason i cant see it.

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

    }
}
}
Akka Jaworek
  • 1,970
  • 4
  • 21
  • 47
  • You're not saving the value returned from `image.getRGB`, so the `Render` object that you return from `loadBitmap` doesn't have any pixel information. – Cethy Apr 16 '16 at 11:01
  • Where are you actually performing any sort of painting operation? – MadProgrammer Apr 16 '16 at 11:08
  • @Cethy, ''result.pixels'' array is not null, so all RGB are suposed to be written into this array. – Akka Jaworek Apr 16 '16 at 11:11
  • Ok. But not being "null" and actually containing relevant data are two different things. Have you tried iterating over the array? – Cethy Apr 16 '16 at 11:14
  • @Cethy yes, i have, its full of data. – Akka Jaworek Apr 16 '16 at 11:15
  • When I run your code (with another image, of course) all the pixels in the array have the value 0, since you essentially just initialize an array of 0's in Render. Shouldn't the pixel information from the image be stored there? – Cethy Apr 16 '16 at 11:27
  • @MadProgrammer im calling ''bs.show'' to change the display pointer. – Akka Jaworek Apr 16 '16 at 11:28
  • 1
    @AkkaJaworek But you're not painting anything to it?! For [example](http://stackoverflow.com/questions/34689176/bufferstrategy-not-showing-on-java-awt-frame/34689291#34689291) – MadProgrammer Apr 16 '16 at 11:29
  • @MadProgrammer omg... how could i have missed that. ok fixed it, but now its painting everything black. – Akka Jaworek Apr 16 '16 at 11:32
  • @AkkaJaworek You need to update the code to show how and what you're painting – MadProgrammer Apr 16 '16 at 11:33
  • @MadProgrammer was making update in that second – Akka Jaworek Apr 16 '16 at 11:34
  • 1
    @AkkaJaworek As far as I can tell `img` is blank – MadProgrammer Apr 16 '16 at 11:41
  • ok ,i understood my mistake, i rendered whole screen black (but thats good) now i need to render my picture onto that black background. – Akka Jaworek Apr 16 '16 at 12:03
  • @MadProgrammer one more question, in ``Display.jav``' im assaiginig ``pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();`` so if i understand correctly ``pixels`` are basicly a pointer to data array of ``img`` is that correct? so if i change something in ``pixels`` this should also be changer in img? – Akka Jaworek Apr 16 '16 at 13:18
  • @AkkaJaworek I'm not sure if it's copy of or a pointer to the actual image data, I'd need to read the docs to be sure, personally, I just use `Graphics2D` to manipulate the image/graphics context – MadProgrammer Apr 16 '16 at 21:25

0 Answers0