Source Code No.2 - UAS Komgraf [AJIT PRASETIYO]

                Never    
Java
       
package UAS2KOMGRAF;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.*;
import javax.swing.*;

/**
 *
 * @author aprstio
 */

public class UAS2 extends JPanel {
    
    private float xpos = 20f;
    private float ypos = 20f;
    private float diff = 1.0f;
    
    public UAS2() {
        new AnimasiRunnable();
        
        this.setPreferredSize(new Dimension(350, 250));
    }
    
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        Ellipse2D rect = new Ellipse2D.Float(xpos, ypos, 40f, 40f);
        g2d.setColor(Color.red);
        this.setBackground(Color.darkGray);
        g2d.fill(rect);
        
    }
      
    class AnimasiRunnable implements Runnable {
        private Thread runner;
        public AnimasiRunnable() {
            runner = new Thread(this);
            runner.start();
        }

        public void run() {
            while (true) {
                
                repaint();
                if (xpos > 400.0f)
                    diff = -1.0f;
                else if (xpos < 20f)
                    diff = 1.0f;

                xpos += diff;
                ypos += diff;

                try {
                    Thread.sleep(30);
                } catch (Exception e) {}
            }
        }
    }

    public static void main(String [] args){
        JFrame frame = new JFrame("UAS.2 - KOMGRAF 1");
        frame.addWindowListener(new WindowAdapter (){
            public void windowClosing(WindowEvent e){System.exit(0);}
        });
        UAS2 c = new UAS2();
        frame.getContentPane();
        frame.add(c);
        frame.pack();
        frame.setVisible(true);
    }
}

Raw Text