Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

drawing to buffer + flip to window

Posted on 2001-06-15
11
352 Views
Last Modified: 2012-05-04
any sourcecodes for the following problem :
- i want to draw to a 2d array (24bit if possible)
- flip the array to the screen (to a size-fixed window)
- hell-fast (if possible)

do you have any sources for this. inclusive creating the size-fixed window and everything else. i want to calc some graphic effects (plasmas, etc.). if you've got any additional questions, feel free to ask.

thank you very much. i appreciate every comment.
0
Comment
Question by:columbo666
11 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 6195443
>i want to draw to a 2d array
You can do that only since JDK1.2 by using BufferedImage.getGraphics() and then either using PixelGrabber or calling Raster.getPixels() on the BufferedImage's Raster to obtain the pixel values in the form of an int array (32 bits).
>flip the array to the screen (to a size-fixed window)
Just draw the BufferedImage on a showing Window.
>hell-fast (if possible)
Well, you don't really control it using the method I described.

Alternatively, since JDK1.0, you can implement your own drawing procedures, which would draw into an int array. You would then convert the array into an Image using Toolkit.createImage(ImageProducer) and MemoryImageSource.

To make the drawing as fast as possible, I recomment creating an offscreen Image using Component.createImage(int, int) - painting the actual image on it once and then painting the offscreen image on the screen every time (instead of the actual image).

Sasha Maryanovsky.
0
 
LVL 1

Author Comment

by:columbo666
ID: 6196087
very good. any code ? i tried to use these createImage-things (and others), but i couldn't get them working.
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 6196096
For which one of the ways? 1.1 or 1.2 compatible?

Sasha Maryanovsky.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 1

Author Comment

by:columbo666
ID: 6198266
i've got 1.2, but you can do it also for 1.1. your decision
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 6198428
Well, 1.2 it's pretty easy:

int width = 300;
int height = 300;
BufferedImage img = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
// Do various painting on the Graphics here.
int [] pixels = img.getRGB(0,0,width,height,null,0,width);

After this, "pixels" contains all the pixel values in aRGB format.

For 1.1, you will have to implement your own drawing routines, which is quite difficult, and I'm not going to explain this here :-)

Sasha Maryanovsky.
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 6198429
Umm, note that I didn't test this code, so it may be wrong :-)

Sasha Maryanovsky.
0
 
LVL 3

Expert Comment

by:Sendoh
ID: 6201259
hi !
guess this website may help u...

http://java.sun.com/docs/books/tutorial/2d/images/index.html

hope it helps..:)
0
 
LVL 1

Author Comment

by:columbo666
ID: 6202809
i can really use that. thanks. a small detail : i want to draw only single pixels (and fill the whole buffer with them). any fast possibilities ?
0
 
LVL 1

Author Comment

by:columbo666
ID: 6224612
sorry, finally i can't really use that. i just want a complete piece of code, which creates a non-resizable window and draws an (pixel based & flickerless) animation within.
0
 

Accepted Solution

by:
benutzername earned 300 total points
ID: 6737333
Hi Columbo! I've got the same problem - I want to render fast realtime fractal animations onto a fullscreen window. I worked myself through the JAVA 1.4 API and created this sourcecode out of some examples - it renders randomly colored pixels onto your screen.
The code is far from being optimzed, but it works. I'm still working on the code, because I need it myself, so maybe we could work together to get it running fast.

/**
 * This demo shows how to render single pixels in a fullscreen window using double buffering
 */

import java.awt.*;
import java.awt.image.*;
import java.util.Random;

public class MultiBufferTest extends Component{
   
    int width = 640;
    int height = 480;
    int size = width * height;
    int pixels[] = new int[size];
    int red;
    int green;
    int blue;
    int alpha = 255;
    MemoryImageSource source = new MemoryImageSource(width, height, pixels, 0, width);  
    Graphics g;
    Random r = new Random();
   
    /** constructor
     */
    public MultiBufferTest() {
        // setup display
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getDefaultScreenDevice();          
        GraphicsConfiguration gc = device.getDefaultConfiguration();

        // create frame
        Frame mainFrame = new Frame(gc);
        mainFrame.setUndecorated(true);
        mainFrame.setIgnoreRepaint(true);
       
        // start fullscreen
        device.setFullScreenWindow(mainFrame);        
        device.setDisplayMode(new DisplayMode(width, height, 16, 85));
       
        // setup doubelbuffering
        mainFrame.createBufferStrategy(2);        
        BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();
       
        // setup image source
        source.setAnimated(true);
        Image image = createImage(source);

        // loop for 50 times and draw random pixels
        for (int t = 0; t < 50; t++) {
            // get offscreen image
            g = bufferStrategy.getDrawGraphics();
            for (int j = 0; j < size; j++) {
                // randomize colors
                red = Math.abs(r.nextInt()) % 255;
                green = Math.abs(r.nextInt()) % 255;
                blue = Math.abs(r.nextInt()) % 255;
                // set pixel
                pixels[j] = (alpha << 24) | (red << 16) | (green << 8 ) | blue;
            }
            // draw new pixels
            g.drawImage(createImage(source), 0,0, this);
            // flip offscreen image to screen
            bufferStrategy.show();
            // release offscreen image
            g.dispose();
        }
       
        // exit
        device.setFullScreenWindow(null);
        System.exit(0);
    }
   

    public static void main(String[] args) {
        MultiBufferTest test = new MultiBufferTest();
    }
}
0
 
LVL 1

Author Comment

by:columbo666
ID: 6738746
dunno, the project was done now without this and i can code java a bit now...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 65
oracle 11g 23 107
jmss example java 2 23
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question