Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package learning;
- import java.awt.*;
- import javax.swing.*;
- public class Draw4 extends JPanel {
- static int x = 0, y = 0;
- public void paint(Graphics g) {
- Graphics2D g2 = (Graphics2D) g;
- g2.fillOval(x, y, 10, 10);
- }
- public static void main(String[] args) throws InterruptedException {
- JFrame frame = new JFrame("Draw");
- Draw4 game = new Draw4();
- frame.setBackground(Color.white);
- frame.add(game);
- frame.setSize(400, 300);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- int length = 50;
- double angle = 0.0;
- double angle_stepsize = 0.05;
- // drawing first semicircle
- while(angle > -Math.PI) {
- x = (int) Math.floor(length * Math.cos(angle))+ 50;
- y = (int) Math.floor(length * Math.sin(angle))+ 50;
- game.repaint();
- angle -= angle_stepsize;
- Thread.sleep(10);
- }
- // drawing a line using the Bresenham Algorithm
- int x2 = 100, y2 = 100;
- int w = x2 - x ;
- int h = y2 - y ;
- int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
- if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
- if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
- if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
- int longest = Math.abs(w) ;
- int shortest = Math.abs(h) ;
- if (!(longest>shortest)) {
- longest = Math.abs(h) ;
- shortest = Math.abs(w) ;
- if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
- dx2 = 0 ;
- }
- int numerator = longest >> 1 ;
- for (int i=0;i<=longest;i++) {
- game.repaint();
- Thread.sleep(10);
- numerator += shortest ;
- if (!(numerator<longest)) {
- numerator -= longest ;
- x += dx1 ;
- y += dy1 ;
- } else {
- x += dx2 ;
- y += dy2 ;
- }
- }
- //drawing second circle
- angle = 0.0;
- while(angle < Math.PI) {
- x = (int) Math.floor(length * Math.cos(angle))+ 50;
- y = (int) Math.floor(length * Math.sin(angle))+ 100;
- game.repaint();
- angle += angle_stepsize;
- Thread.sleep(10);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement