1. Member
Join Date
Jan 2012
Posts
4
Rep Power
0

## 2D graphics acceleration

I have made a java code which displays a blue sphere and lets you control with the mouse where its illumination comes from. (its actually a circle and some parts are painted white/blue/black depending on the mouse position to make it look 3D)

here is the code

Java Code:
```import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;

public class esfera extends JFrame{

double azimuth=3*Math.PI/4, inclination=Math.PI/4; //angle coordinates for the 'light source'

private BufferedImage image = new BufferedImage(800,800,BufferedImage.TYPE_INT_RGB); //the image storing the sphere drawing

public esfera(){ //initializer adding a listener to mouse motion
public void mouseDragged(MouseEvent e){
azimuth=((800-e.getX())/800.00)*Math.PI;
inclination=(e.getY()/800.00)*Math.PI;
repaint();
}
});}

public void paint(Graphics g){ //does the whole painting

g.drawImage(image,0,0,null); //paint the current image to the frame

for(int i=0;i<800;i++){ //make next image, pixel by pixel
for(int j=0;j<800;j++){
image.setRGB(i,j,color((i-400)/200.00,(400-j)/200.00));
}
}
}

public int color(double x, double y){ //this is what color each pixel should be painted in order to make the picture look like a sphere
double clr;
double z = Math.sqrt(1-(x*x+y*y));
double angle = Math.acos(Math.cos(inclination)*y+Math.sin(inclina tion)*(Math.sin(azimuth)*z+Math.cos(azimuth)*x));

if(x*x+y*y>1) clr=0;
else if (angle>Math.PI/2) clr=0;
else if (angle>Math.PI/4)clr=255*(2-angle/(Math.PI/4));
else{ int white =((int)(255*(1-angle/(Math.PI/4)))<<8)+((int)(255*(1-angle/(Math.PI/4)))<<16);
clr=white+255;}

return (int)clr;

}

public static void main(String[] args){ //the main method of course

esfera frame = new esfera();
frame.setSize(800,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

}```
it works ok. the only problem is that when you move the mouse it takes too long to update the screen with the next picture.(so the 'light source' seems to be jumping from one place to another if you move the mouse too fast)

the question is: how can I fix this problem?

i have been reading about VolatileImage but it seems that you can't write a VolatileImage pixel by pixel, so it's not what i need.

i have also read about BufferStrategy, but i'm not sure if it could help me or not, (didn't completely understand how to use it neither)

could you help me please??? i've been trying to solve it all day...

2. ## Re: 2D graphics acceleration

Cross posted at graphics acceleration

Try timing how long it takes to build the new image. I get times in the 500-600ms range.

3. Member
Join Date
Jan 2012
Posts
4
Rep Power
0

## Re: 2D graphics acceleration

yes, it takes 500-600ms for me too....
so, there is no solution then....?

4. ## Re: 2D graphics acceleration

Can you reuse the image? Build it once and then display it rotated?

5. Member
Join Date
Jan 2012
Posts
4
Rep Power
0

## Re: 2D graphics acceleration

not really.... however, someone suggested rewriting the 'color' function so that it didn't calculate so many sines and cosines, and now it takes only ~100ms to build the image :O
here's the link Graphics acceleration in java

thanks for your help and interest :)

6. ## Re: 2D graphics acceleration

Ah yes, Higher math and better algorithms. I'm only a poor coder.

7. Member
Join Date
Jan 2012
Posts
4
Rep Power
0

## Re: 2D graphics acceleration

ahah :) ey! now there's something else i desperately need help about.
i'm trying to learn opengl with java (jogl binding), but there is no way i can set up things to work appropiately. i've spent hours trying to find the right files to download, and put them in the right folders and set the 'path' variables and so on............. but i couldn't get a success in hours, it's getting me dizzy. could you help me?

8. ## Re: 2D graphics acceleration

Can you start a new thread with the new problem?
Give specific details on the errors you are getting and the jar files etc you are working with.

9. Member
Join Date
Jan 2012
Posts
14
Rep Power
0

## Re: 2D graphics acceleration

Platform - dependent issue, at the end of "pipeline" all Math.sqrt(),Math.sin() and Math.cos() methods are translated to native calls which are implemented as raw assembly instructions : fsin,fcos and fsqrt .Fsin and fcos have each of them ~27 cycles of latency and fsqrt has ~60cycles (measured in cpu's cpi ) multiply sum of instructions latency by the workload measured in pixels to be drawn(also depends on shading algorithm used by the java and by the gdi library and frequent context switching between user32.dll gdi.dll and win32k.sys) and it is not surprise that you got so long drawing latency.You should gain some acceleration of rendering if the display driver could hook rendering calls(like alpha blending) coming from your app and perform rendering in the hardware.As far as i know display drivers in windows are capable of doing this.
Last edited by iliyapolak; 01-18-2012 at 09:56 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•