Guest User

Untitled

a guest
Apr 18th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. package java2d;
  3.  
  4. import java.awt.*;
  5. import java.awt.geom.*;
  6. import javax.swing.JFrame;
  7.  
  8. public class DDA extends JFrame {
  9.  
  10.     public static void main(String[] args) {
  11.        
  12.         JFrame frame = new JFrame();
  13.        
  14.         DDA z = new DDA();
  15.         z.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.         z.setVisible(true);
  17.         z.setTitle("Java 2D program");
  18.         z.setSize(1024,768);
  19.        
  20.        
  21.        
  22.        
  23.     }
  24.    
  25.     public void paint (Graphics g)
  26.     {
  27.        
  28.        
  29.         Graphics2D g2d =  (Graphics2D) g;
  30.        
  31.        
  32.         double dx,dy,steps,x1,x2,y1,y2;
  33.         double x3,y3,xinc,yinc;
  34.        
  35.        
  36.         x1=110; y1= 150 ; x2 = 500; y2 = 140;
  37.         dx = x2-x1;
  38.         dy = y2-y1;
  39.        
  40.         if(Math.abs(dx) > Math.abs(dy))
  41.         {
  42.             steps = Math.abs(dx);
  43.                
  44.            
  45.            
  46.            
  47.            
  48.         }
  49.        
  50.         else
  51.            
  52.             steps = Math.abs(dy);
  53.        
  54.  
  55.         xinc = (float)dx/steps;
  56.         yinc = (float)dy/steps;
  57.        
  58.         x3 = x1; y3 = y1;
  59.        
  60.        
  61.         drawPoint(x3,y3,g2d);
  62.        
  63.         for(int i = 1; i<steps;i++)
  64.         {
  65.            
  66.             x3 = x3+xinc;
  67.             y3 = y3 + yinc;
  68.             drawPoint((int)(x3+0.5),(int) (y3+0.5),g2d );
  69.            
  70.            
  71.            
  72.         }
  73.            
  74.        
  75.        
  76.        
  77.     }
  78.    
  79.     public static void drawPoint(double x , double y,Graphics2D g2d)
  80.     {
  81.        
  82.         Line2D.Double line = new Line2D.Double(x, y, x, y);
  83.          g2d.setColor(Color.red);
  84.                 g2d.draw(line);
  85.     }
  86.    
  87.    
  88. }
Add Comment
Please, Sign In to add comment