Blogger Widgets

Senin, 13 Oktober 2014

Pemrograman Grapik

selamat pagi,
salam bahagia
kali ini saya akan membuat Program tentang Bola(Ball).

  1. Perintah Ball:

    package Bola;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Polygon;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    import javax.swing.JPanel;

    /**
     *
     * @author Aris
     */
    public class Ball extends JPanel {
    private int delay = 50;
    // Create a timer with delay 1000 ms
       protected Timer timer = new Timer(delay, new TimerListener());

       public int x = 0; private int y = 0; // Current ball position
       private int radius = 5; // Ball radius
       private int dx = 2; // Increment on ball's x-coordinate
       private int dy = 2; // Increment on ball's y-coordinate

       public Ball() {
         timer.start();
    }
        private class TimerListener implements ActionListener {
         /** Handle the action event */
         public void actionPerformed(ActionEvent e) {
           repaint();

         }
       }
       protected void paintComponent(Graphics g) {
         super.paintComponent(g);

         g.setColor(Color.ORANGE);

         // Check boundaries
         if (x < radius) dx = Math.abs(dx);
         if (x > getWidth() - radius) dx = -Math.abs(dx);
         if (y < radius) dy = Math.abs(dy);
         if (y > getHeight() - radius) dy = -Math.abs(dy);

         // Adjust ball position
            x += dx;
            y += dy;
            g.fillOval(x - radius, y - radius, radius * 2, radius * 2);

           Polygon anu=new Polygon();
           anu.addPoint(0, 0);
            anu.addPoint(x - radius, y - radius);
           g.drawPolygon(anu);

          
    }

    public void suspend() {
    timer.stop(); // Suspend timer
    }

    public void resume() {
    timer.start(); // Resume timer
    }

    public void setDelay(int delay) {
    this.delay = delay;
    timer.setDelay(delay);
       }

    }
     
  2. Perintah BallControl:

    package Bola;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;

    /**
     *
     * @author Aris
     */
    public class BallControl extends JPanel {
        private Ball ball = new Ball();
        private JButton jbtStop = new JButton("Stop");
        private JButton jbtResume = new JButton("Lanjut");
        private JScrollBar jsbDelay = new JScrollBar();

       public BallControl() {
             JPanel panel = new JPanel();
         panel.add(jbtStop);
         panel.add(jbtResume);
             ball.setBorder(new javax.swing.border.LineBorder(Color.red));
            jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
         ball.setDelay(jsbDelay.getMaximum());
         setLayout(new BorderLayout());
         add(jsbDelay, BorderLayout.NORTH);
         add(ball, BorderLayout.CENTER);
         add(panel, BorderLayout.SOUTH);

          jbtStop.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
             ball.suspend();
           }
         });
           jbtResume.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
             ball.resume();
           }
         });
         jsbDelay.addAdjustmentListener(new AdjustmentListener() {
           public void adjustmentValueChanged(AdjustmentEvent e) {
             ball.setDelay(jsbDelay.getMaximum() - e.getValue());
           }
         });
       }
    }
  3. Perintah BounceBallApp:
    package Bola;
    import javax.swing.JApplet;
    /**
     *
     * @author Aris
     */
    public class BounceBallApp extends JApplet{
       public BounceBallApp(){
            add(new BallControl());
       }
    }
  4. Hasilnya:

0 komentar:

Posting Komentar