question java awt

Discussion in 'Software' started by skyler43@123, Jan 12, 2013.

  1. skyler43@123

    skyler43@123 Private E-2

    i have this code:
    Code:
    package subdivizare;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.*;
    
    public class Subdivizare extends Frame{
    
    	private static final long serialVersionUID = 1L;
            
         double bx[], by[];
         double ax[][];
         double ay[][];
         
         int n;
         int i, j;
         double t;
         
         Canvas2d c2d;
    	
    	Subdivizare(){
                /*
    		super("Subdivizare");
    		
    		c2d = new Canvas2d();
    		
    		add(c2d);
    		
    		WHandler wh = new WHandler();
    		addWindowListener(wh);
                    * */
    	}
       public void graphic(){
           c2d = new Canvas2d();
           add(c2d);
           
           WHandler wh = new WHandler();
           addWindowListener(wh);
        
       }
        public void read_input(){
               
                    System.out.println("Dati n:");
                    Scanner sc = new Scanner(System.in);
                    n = sc.nextInt();
                    
                    System.out.println("Dati t: ");
                    t = sc.nextDouble();
                    
                    bx = new double[n + 2];
                    by = new double[n + 2];
                    
                    for(i = 0; i <= n; i++){
                        System.out.println("Dati prima coordonata: ");
                        bx[i] = sc.nextDouble();
                        System.out.println("Dati a doua coordonata: ");
                        by[i] = sc.nextDouble();
                    }   
                    ax = new double[n + 1][n + 1];
                    ay = new double[n + 1][n + 1]; 
        }
        public void make_(){
            
                    for(i = 0; i <=n; i++){
                        ax[0][i] = bx[i];
                        ay[0][i] = by[i];
                    }
                    //Casteljau
                    for(i = 1; i <= n; i++)
                        for(j = 0; j <= n - i; j++){
                            ax[i][j] = (1 - t) * ax[i - 1][j] + t * ax[i - 1][j + 1];
                            ay[i][j] = (1 - t) * ay[i - 1][j] + t * ay[i - 1][j + 1];
                        }
        }
        public void print_on_screen_results(){
            
                    //afisare puncte de casteljau
                    for(i = 0 ;i <= n; i++)
                        for(j = 0; j <= n; j++){
                            System.out.print(ax[i][j] + " ");
                            System.out.print(ay[i][j] + " ");
                            System.out.println();
                        }
                    System.out.print("Prima restrictie: ");
                    for(i = 0; i <= n; i++){
                      
                       System.out.println(ax[i][0] + " " + ay[i][0]);
                      }
                     int k = 0;
                     System.out.print("A doua restrictie: ");
                     
                     for(j = n; j >= 0; j--)
                     {
                       System.out.println(ax[j][k] + " " + ay[j][k]);
                       k++;
                      }
        }
    	class WHandler extends WindowAdapter{
    		public void windowClosing(WindowEvent e){
    			System.exit(0);
    		}      
            }
    
    	public static void main(String[] args) {
                
    	  Subdivizare t = new Subdivizare();
              t.read_input();
              t.make_();
              t.print_on_screen_results();  
              t.graphic();
    	  t.setSize(600, 600);
    	  t.setLocation(200, 200);
    	  t.setVisible(true);
    	
    	}
    }
    
    class Canvas2d extends Canvas{
    
    	private static final long serialVersionUID = 1L;
    
    	public void paint(Graphics g){
    		
    		Graphics2D g2 = (Graphics2D) g;
    	        g2.setPaint(Color.red);
                    
                    Subdivizare ob = new Subdivizare();
                  
    		int x1, y1, x2, y2;
    
    		x1 = 0;
    		y1 = 300;
    		x2 = 600;
    		y2 = 300;
    		
    		Line2D l1 = new Line2D.Double(x1, y1, x2, y2); 
    		g2.draw(l1);
    		
    		x1 = 300;
    		y1 = 0;
    		x2 = 300;
    		y2 = 600;
    		
    		Line2D l2 = new Line2D.Double(x1, y1, x2, y2); 
    		g2.draw(l2);
                    
                    
                     int p = 1, i;
                     double aux1, aux2, aux3, aux4;
                     for(i = 1; i <= ob.n; i++){
                        aux1 = ob.ax[i][0];     
                        aux2 = ob.ay[i][0];
                        aux3 = ob.ax[p][0];
                        aux4 = ob.ay[p][0];  
                        Line2D l3 = new Line2D.Double(aux1 + 300, aux2 + 300, aux3 + 300, aux4 + 300); 
    		    g2.draw(l3);
                    }
                    
                    
                    
                  }
    }
    
    
    
    everything it works fine, but: starting with the line 149 i want to draw me a line with that elements, and it doesn't.. why? i don't know so much awt.. someone here who does?
     
  2. red death68

    red death68 Command Sergeant Major

    well I couldn't get it to draw as it isn't my area of expertise I did clean the code up a bit I hope this helps. I may take another crack at it once i get some sleep and do some research.


    Code:
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.geom.Line2D;
    import java.util.Scanner;
    import javax.swing.JFrame;
    
    public class Subdivizare extends JFrame
    {
    	private static final long serialVersionUID = 1L;
            
        double bx[], by[];
        double ax[][], ay[][];
        int i, j, n;
        double t;
        Canvas2d c2d;
        
    	
    	public Subdivizare()
    	{	
    		
    	}
       
    	public void graphic()
       {
           Canvas2d c2d = new Canvas2d();
           add(c2d);
           
           WHandler wh = new WHandler();
           addWindowListener(wh);
       }
        
    	public void read_input()
    	{
    		System.out.println("Dati n:");
            Scanner sc = new Scanner(System.in);
            n = sc.nextInt();
                    
            System.out.println("Dati t: ");
            t = sc.nextDouble();
                    
            bx = new double[n + 2];
            by = new double[n + 2];
            
            System.out.println("Da prima pozitie de coordonate x");
        	bx[0] = sc.nextDouble();
        	
        	System.out.println("Da prima pozitie de coordonate y");
        	bx[1] = sc.nextDouble();
        	
        	System.out.println("Da doua pozitie de coordonate x");
        	by[0] = sc.nextDouble();
        	
        	System.out.println("Da doua pozitie de coordonate y");
        	by[1] = sc.nextDouble();
    
            ax = new double[n + 1][n + 1];
            ay = new double[n + 1][n + 1]; 
        }
    	
        public void make()
        {     
        	ax[0][0] = bx[0];
        	ax[0][1] = bx[1];
        	ay[0][0] = by[0];
        	ay[0][1] = by[1];
            //Casteljau
                    
            for(i = 1; i <= n; i++)
            {
            	for(j = 0; j <= n - i; j++)
                {
            		ax[i][j] = (1 - t) * ax[i - 1][j] + t * ax[i - 1][j + 1];
                    ay[i][j] = (1 - t) * ay[i - 1][j] + t * ay[i - 1][j + 1];
                }
            }    
        }
        public void print_on_screen_results()
        {      
        	//afisare puncte de casteljau
        	for(i = 0 ;i <= n; i++)
        	{
        		for(j = 0; j <= n; j++)
                {
                System.out.print(ax[i][j] + " ");
                System.out.print(ay[i][j] + " ");
                System.out.println();
                }
            }    
                    
            System.out.print("Prima restrictie: ");
                    
            for(i = 0; i <= n; i++)
            { 
            	System.out.println(ax[i][0] + " " + ay[i][0]);
            }
                     
            int k = 0;
                     
            System.out.print("A doua restrictie: ");
                     
            for(j = n; j >= 0; j--)
            {
            	System.out.println(ax[j][k] + " " + ay[j][k]);
                k++;
            }
        }
        
    	class WHandler extends WindowAdapter
    	{
    		public void windowClosing(WindowEvent e)
    		{
    			System.exit(0);
    		}      
        }
    
    	public static void main(String[] args) 
        {       
    	  Subdivizare window = new Subdivizare();
          window.read_input();
          window.make();
          window.print_on_screen_results();  
          window.graphic();
    	  window.setSize(600, 600);
    	  window.setLocation(200, 200);
    	  window.setVisible(true);
    	}
    }
    
    class Canvas2d extends Canvas
    {
    
    	private static final long serialVersionUID = 1L;
    
    	
    	public void paint (Graphics g) 
    	{
    	    Graphics2D g2 = (Graphics2D) g;
    		g2.setPaint(Color.red);
                    
            Subdivizare ob = new Subdivizare();
                  
    		int x1, y1, x2, y2;
    
    		x1 = 0;
    		y1 = 300;
    		x2 = 600;
    		y2 = 300;
    		
    		Line2D l1 = new Line2D.Double(x1, y1, x2, y2); 
    		g2.draw(l1);
    		
    		x1 = 300;
    		y1 = 0;
    		x2 = 300;
    		y2 = 600;
    		
    		Line2D l2 = new Line2D.Double(x1, y1, x2, y2); 
    		g2.draw(l2);
                    
                    
            int p = 1, i;
            double aux1, aux2, aux3, aux4;
              
            for(i = 1; i <= ob.n; i++)
            {
            	aux1 = ob.ax[i][0];     
            	aux2 = ob.ay[i][0];
            	aux3 = ob.ax[p][0];
            	aux4 = ob.ay[p][0];  
            	Line2D l3 = new Line2D.Double(aux1 + 300, aux2 + 300, aux3 + 300, aux4 + 300); 
            	g2.draw(l3);
            }    
    	}
    }
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds