Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package learning;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. public class Draw4 extends JPanel {
  7.  
  8.     static int x = 0, y = 0;
  9.  
  10.     public void paint(Graphics g) {    
  11.         Graphics2D g2 = (Graphics2D) g;
  12.         g2.fillOval(x, y, 10, 10);             
  13.     }
  14.  
  15.     public static void main(String[] args) throws InterruptedException {       
  16.         JFrame frame = new JFrame("Draw");
  17.         Draw4 game = new Draw4();
  18.         frame.setBackground(Color.white);
  19.         frame.add(game);
  20.         frame.setSize(400, 300);
  21.         frame.setVisible(true);
  22.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  23.        
  24.         int length = 50;
  25.         double angle = 0.0;
  26.         double angle_stepsize = 0.05;
  27.        
  28.         // drawing first semicircle
  29.        
  30.         while(angle > -Math.PI) {
  31.             x = (int) Math.floor(length * Math.cos(angle))+ 50;
  32.             y = (int) Math.floor(length * Math.sin(angle))+ 50;
  33.             game.repaint();
  34.             angle -= angle_stepsize;
  35.             Thread.sleep(10);
  36.         }
  37.        
  38.         // drawing a line using the Bresenham Algorithm
  39.        
  40.         int x2 = 100, y2 = 100;
  41.         int w = x2 - x ;
  42.         int h = y2 - y ;
  43.         int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
  44.         if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
  45.         if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
  46.         if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
  47.         int longest = Math.abs(w) ;
  48.         int shortest = Math.abs(h) ;
  49.         if (!(longest>shortest)) {
  50.             longest = Math.abs(h) ;
  51.             shortest = Math.abs(w) ;
  52.             if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
  53.             dx2 = 0 ;            
  54.         }
  55.         int numerator = longest >> 1 ;
  56.         for (int i=0;i<=longest;i++) {
  57.             game.repaint();
  58.             Thread.sleep(10);
  59.             numerator += shortest ;
  60.             if (!(numerator<longest)) {
  61.                 numerator -= longest ;
  62.                 x += dx1 ;
  63.                 y += dy1 ;
  64.             } else {
  65.                 x += dx2 ;
  66.                 y += dy2 ;
  67.             }
  68.         }
  69.        
  70.         //drawing second circle
  71.        
  72.         angle = 0.0;
  73.         while(angle < Math.PI) {
  74.             x = (int) Math.floor(length * Math.cos(angle))+ 50;
  75.             y = (int) Math.floor(length * Math.sin(angle))+ 100;
  76.             game.repaint();
  77.             angle += angle_stepsize;
  78.             Thread.sleep(10);
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement