Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  WritableRaster problems  (Read 806 times)
0 Members and 1 Guest are viewing this topic.
Offline ECJava

Junior Duke


Medals: 1



« Posted 2013-06-26 05:37:44 »

I don't know why but when I try to paint the manipulated image nothing has changed.



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
private void render(Graphics2D g2d){
       
        WritableRaster wr = lithosphere.getRaster();
       
        int lookup;
       
        double mapHeight;
       
        double[] k = {0,0,0}, data;
           
        for (int x = 0; x < WIDTH; x++) {

            for (int y = 0; y < HEIGHT; y++) {

                mapHeight = (map[x][y].getHeight() <= 650) ? 650 : map[x][y].getHeight();

                lookup = (int)(mapHeight / 650) - 1;

                data = heightmap_raster.getPixel(lookup, 0, k);

                wr.setPixel(x, y, data);

            }

        }
       
        g2d.drawImage(lithosphere, 0, 0, null);
       
    }


The raster is changing but when I go to draw the image nothing is drawn. The image stays the same. Any ideas?

Oh and I didn't know where this was supposed to go. And please do not give me a link to something. I've been on this for a while and still can't figure it out.
Offline philfrei
« Reply #1 - Posted 2013-06-26 07:11:37 »

Is there another drawImage(lithosphere, 0, 0, null); command somewhere in your code?

If so, maybethat other drawImage is the only one actually displaying. This could occur for a number of reasons, for example, if the EDT is "collapsing" draw commands, or if the Graphics2D parameter you are passing is somehow not connected to the displaying component (JPanel? JComponent? Canvas?) or that component is not getting a repaint() or equivalent.

One idea for testing, simply make a very different graphic and make your code thus:

1  
2  
3  
4  
5  
6  
7  
private void render(Graphics2D g2d){
       
        lithosphere = testImage; // testImage is an image that is obviously different
       
        g2d.drawImage(lithosphere, 0, 0, null);
       
    }


If the testImage displays, then you KNOW it is a problem with the WritableRaster code. But I'm starting with the assumption it is not, since you said you inspected the graphic and confirmed that it IS changing.

"It's after the end of the world! Don't you know that yet?"
Online pjt33
« Reply #2 - Posted 2013-06-26 08:28:39 »

The raster is changing but when I go to draw the image nothing is drawn. The image stays the same. Any ideas?
I don't see any obvious problems with the code. Can you produce a minimal self-contained (i.e. compilable and runnable with just the standard API for dependencies) program which demonstrates the same problem?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ECJava

Junior Duke


Medals: 1



« Reply #3 - Posted 2013-06-26 14:58:30 »

No, that is the only place in which the image is being rendered. Also the image is being drawn onto a JPanel. The Graphics2D device is passed by the JPanel's paintComponent(Graphics g) method. Which is shown below.

The funny thing about it is if I fill a rectangle onto the image in the start before any changes are made, say a red box, it renders that just fine. But no changes are being made to the raster.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
private class gtPanel extends JPanel {
       
        public void paintComponent(Graphics g) {
       
            Graphics2D g2d = (Graphics2D) g;

            super.paintComponent(g);

            render(g2d);

            g2d.dispose();

        }
       
}
Offline ECJava

Junior Duke


Medals: 1



« Reply #4 - Posted 2013-06-26 15:29:07 »

I have also tried this with no success either... The array K was deleted and data was used as the input array for the pixel. The array data shows a change when this is called,
1  
heightmap_raster.getPixel(lookup, 0, data);


However nothing still works. I don't understand why nothing is showing a change when the image is rendered.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
private void render(Graphics2D g2d){
       
        WritableRaster wr = lithosphere.getRaster().createCompatibleWritableRaster();
       
        int lookup;
       
        double mapHeight;
       
        double[] data = {0,0,0};
           
        for (int x = 0; x < WIDTH; x++) {

            for (int y = 0; y < HEIGHT; y++) {

                mapHeight = (map[x][y].getHeight() <= 650) ? 650 : map[x][y].getHeight();

                lookup = (int)(mapHeight / 650) - 1;

                heightmap_raster.getPixel(lookup, 0, data);

                wr.setPixel(x, y, data);

            }

        }
       
        g2d.drawImage(lithosphere, 0, 0, null);
       
}
Offline philfrei
« Reply #5 - Posted 2013-06-26 19:15:41 »

A couple of the issues that I mentioned could still be causing the problem, even if there is just the one imageDraw.

Here's another idea: put in a System.out.println() message every time render gets called, just to make sure it is getting called multiple times.

Also, can you show us the calling code and where you repaint() the JPanel? Changing the graphic does nothing unless you redisplay the JPanel. It may not be called, or it may be a call that is getting collapsed by the EDT.

"It's after the end of the world! Don't you know that yet?"
Offline ECJava

Junior Duke


Medals: 1



« Reply #6 - Posted 2013-06-26 20:51:29 »

I figured out the problem.

The bug was serveral things. The for-loops WIDTH and HEIGHT conditions were not set correctly and were giving false states. As for the WritableRaster, the code below is what now works. Strange, you have to set the data in the buffered image you are currently working on. Apparently the reference is a copy so I was not working directly on the image.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
private void render(Graphics2D g2d){
       
        WritableRaster wr = lithosphere.getRaster(); // <-------------- Not making a copy, just getting the reference
       
        Color colorRef;
        int lookup, color;
       
        double mapHeight;
       
        int[] data = {0,0,0}; //<----------- This was changed to int
           
        Graphics2D lg2d = lithosphere.createGraphics();
       
        for (int x = 0; x < JGeoTec.WIDTH; x++) { // <------------------ Bug here

            for (int y = 0; y < JGeoTec.HEIGHT; y++) { // <------------------ Bug here

                mapHeight = (map[x][y].getHeight() <= 650) ? 650 : map[x][y].getHeight();

                lookup = (int)(mapHeight / 650) - 1;

                heightmap_lookup_raster.getPixel(lookup, 0, data);
               
                wr.setPixel(x, y, data);

            }

        }
       
        renderFaults(wr);
       
        //lithosphere.setData(wr); //<-------- ONLY needed if you are creating a copy of a raster
       
        g2d.drawImage(lithosphere, null, null);
       
}


I am able to work on 1024x1024 pixels very quickly. Very useful stuff here if you are wanting to work on images right down to the pixels. setRGB methods are very slow...



****** EDIT *******

I was creating a new raster by calling createCompatiableWritableRaster or something. OTHERWISE the code above is completely correct.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (26 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (75 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!