Results 1 to 3 of 3

Thread: Drawig images.

  1. #1
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Drawig images.

    I followed this Draw textured spheres | JavaWorld article and built the code that is written there.
    But it does not generate a sphere or ball. Am i using it wrong, or am i missing something?
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.JFrame;
    
    public class Graafika extends JFrame{
    	public Graafika(){
    		int width = 200;
    		int height = 200;
    		Sphere obj = new Sphere(100, 20, new SineTexture(5, 100, 5));
    		int[] imageData = new int[width * height];
    		
    		int sup = 2;
    		double supInv = 1.0 / sup;
    		for (int j = 0; j < height; ++ j) {
    			for (int i = 0; i < width; ++ i) {
    				RGB pixel = new RGB (0.0, 0.0, 0.0);
    				for (int k = 0; k < sup; ++ k) {
    					for (int l = 0; l < sup; ++ l) {
    						Vec ray = new Vec ((i * 2. - width) / 2. + k * supInv,
                               (j * 2. - height) / 2. + l * supInv, 150.0);
    						RGB rgb = obj.getIntersection (ray);
    						pixel.add (rgb);
    						}
    					}
    				pixel.scale (supInv * supInv);
    				imageData[i + width * j] = pixel.toRGB ();
    			}
    		}
    		
    		MemoryImageSource source = new MemoryImageSource(width, height, imageData,0, width);
    		source.setAnimated(true);
    		source.setFullBufferUpdates(true);
    		Image tile = createImage(source);
    		source.newPixels();
    	
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(400, 300);
    		setLocationRelativeTo(null);
    		setTitle("Graafika");
    		setResizable(false);
    		setVisible(true);
    		}
    		
    	public static void main(String[] args){
    		new Graafika();
    		}
    	}
    Can someone tell me what i did wrong or what did i miss.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Drawig images.

    The preferred way to draw shapes is discussed in the tutorial:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: Drawig images.

    Ty i got it working :D

    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.JPanel;
    import javax.swing.BorderFactory;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    public class Graafika{
    	public Graafika(){
    		JFrame f = new JFrame("Graafika");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.add(new Mypanel());
    		f.pack();
    		f.setVisible(true);
    		}
    		
    	public static void main(String[] args){
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run(){
    				new Graafika();
    				}
    		});
    	}
    }
    
    class Mypanel extends JPanel{
    	public void MyPanel(){
    		setBorder(BorderFactory.createLineBorder(Color.black));
    		}
    	
    	public Dimension getPreferredSize(){
    		return new Dimension(600, 600);
    		}
    		
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		int width = 600;
    		int height = 600;
    		Sphere obj = new Sphere(100, 70, new SineTexture(5, 100, 5));
    		int[] imageData = new int[width * height];
    		
    		int sup = 2;
    		double supInv = 1.0 / sup;
    		for (int j = 0; j < height; ++ j) {
    			for (int i = 0; i < width; ++ i) {
    				RGB pixel = new RGB (0.0, 0.0, 0.0);
    				for (int k = 0; k < sup; ++ k) {
    					for (int l = 0; l < sup; ++ l) {
    						Vec ray = new Vec ((i * 2. - width) / 2. + k * supInv,
                               (j * 2. - height) / 2. + l * supInv, 150.0);
    						RGB rgb = obj.getIntersection (ray);
    						pixel.add (rgb);
    						}
    					}
    				pixel.scale (supInv * supInv);
    				imageData[i + width * j] = pixel.toRGB ();
    			}
    		}
    		
    		MemoryImageSource source = new MemoryImageSource(width, height, imageData,0, width);
    		source.setAnimated(true);
    		source.setFullBufferUpdates(true);
    		Image tile = createImage(source);
    		source.newPixels();
    		g.drawImage(tile, 0, 0, null);
    		}
    	}

Similar Threads

  1. Replies: 0
    Last Post: 11-14-2012, 12:15 PM
  2. ciruit drawig tool-interface
    By kapil1089theking in forum Java 2D
    Replies: 1
    Last Post: 11-05-2011, 10:13 PM
  3. Replies: 0
    Last Post: 12-16-2010, 06:14 PM
  4. Replies: 4
    Last Post: 12-16-2010, 05:32 PM
  5. images
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 11:54 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
  •