Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: graphics acceleration

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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
    		addMouseMotionListener(new MouseMotionAdapter(){
    			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(inclination)*(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_CLOSE);
    		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. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: graphics acceleration

    You could look at double buffering; it helps with the flickering. Also, I would suggest you put the calculations in a separate thread. This being said; I doubt it will help. You are performing 640 000 of those 'color' calculations every 'frame'. It's unlikely that this will run smooth enough for the human eye, even with modern computers.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: graphics acceleration

    Cross posted at
    2D graphics acceleration

Similar Threads

  1. how to use graphics g
    By steel55677 in forum AWT / Java Swing
    Replies: 11
    Last Post: November 21st, 2011, 06:35 PM
  2. Graphics class NullPointerException Initialize Graphics Class??
    By bglueck in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 13th, 2011, 11:13 PM
  3. Java Graphics help
    By Stockholm Syndrome in forum AWT / Java Swing
    Replies: 9
    Last Post: November 4th, 2010, 05:23 PM
  4. Help about Graphics
    By mamech in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 9th, 2010, 03:20 PM
  5. graphics in job?
    By SweetyStacey in forum The Cafe
    Replies: 10
    Last Post: May 3rd, 2010, 03:29 PM