Processing math: 100%

Friday, December 21, 2012

Basic Hack Plotting V1.0

Berikut ini adalah sedikit lanjutan tutorial sebelumnya tentang "plotting berjalan". Pekerjaan selanjutnya adalah bagaimana membuat skala vertikal dari plottting-nya bisa "serasi". Tapi untuk saat ini, hanya itu saja. Semoga bermanfaat.

Project di hosting pada:
https://bitbucket.org/moh_fajar/basicplothacking

Saturday, December 15, 2012

Belajar plot berjalan di java

Dalam tulisan kali ini saya akan memberikan contoh source code java yang bisa digunakan dalam membuat plotting berjalan seperti yang dimaksud pada situs http://www.chem.mtu.edu/~tbco/cm416/newpida.html
(pastikan di komputer anda terinstal java untuk melihat simulasi yang dimaksud)
Untuk mencoba secara sederhana bagaimana algoritma yang digunakan dalam program tersebut, anda dapat mencoba source code berikut:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tuningpid;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
/**
*
* @author mamat
*/
public class AppFrame extends JFrame {
public AppFrame(Component c, String title){
super(title);
this.setPreferredSize(new Dimension(700, 500));
this.setLayout(new BorderLayout());
this.add(c);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
}
}
view raw AppFrame.java hosted with ❤ by GitHub
/**
* Copyright 2012, Moh. Fajar
*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package tuningpid;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
public class PlotPanel extends JPanel implements Runnable {
int hor = 500;
int ver = 350;
private int mainX = 20, mainY = 10;
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list1 = new ArrayList<Integer>();
int horplot = hor + mainX;
public PlotPanel() {
this.setBackground(Color.gray);
for (int i = 50; i < horplot; i += 70) {
list.add(i);
list1.add(i);
}
Thread t = new Thread(this);
t.start();
}
@Override
public void paint(Graphics g) {
super.paintComponent(g);
g.drawRect(mainX, mainY, hor, ver);
g.setColor(Color.BLUE);
this.drawVerticalLine(g);
this.drawAxisValue(g);
}
// public void drawSineWave(Graphics g){
//
// }
public void drawAxisValue(Graphics g){
g.setColor(Color.BLACK);
for(int i=0; i<list.size() ;i++){
g.drawString(list1.get(i).toString(),list.get(i).intValue()-10 , ver+mainY+15);
}
}
public void drawVerticalLine(Graphics g) {
for (int i = 0; i < list.size(); i++) {
int a = list.get(i).intValue();
g.drawLine(a, ver + mainY, a, mainY);
}
}
public void movePanel() {
for (int i = 0; i < list.size(); i++) {
int a = list.get(i).intValue();
a--;
list.remove(i);
list.add(i, a);
}
int b = list.get(list.size() - 1).intValue();
int bb = list1.get(list1.size() - 1).intValue();
int cc = list.get(0).intValue();
if(cc <mainX){
list.remove(0);
list1.remove(0);
}
if ((horplot - b) > 70) {
list.add(b+70);
list1.add(bb+70);
}
}
@Override
public void run() {
while (true) {
this.movePanel();
this.repaint();
try {
Thread.sleep(15);
} catch (Exception e) {
System.out.println("Error");
}
}
}
public static void main(String[] args) {
PlotPanel plotpanel = new PlotPanel();
AppFrame frame = new AppFrame(plotpanel, "sinusiodal");
}
}
view raw PlotPanel.java hosted with ❤ by GitHub

Saturday, December 8, 2012

Hypocycloid di java

Berikut saya berikan contoh bagaimana membuat animasi hypocycloid di java:

source code nya:
  /**
   * Copyright 2012, Moh. Fajar
   * 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see 
    */


package fjr.geometri;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author mamat
 */
public class Hypocycloid extends JPanel {

    //posisi dan ukuran persegi utama
    int rectX = 10;
    int rectY = 10;
    int rectH = 420;
    int rectW = 600;
    
    //posisi dan diameter lingkaran  besar
    int radius1 = 270;
//    int x = rectW+rectX-radius1;
    int x = rectX;
    int y = rectH+rectY - radius1;
   
    //posisi dan diameter lingkaran kecil
    int radius2 = 100;
    int x1 = x + (radius1 - radius2) / 2, 
            y1 = y + (radius1 - radius2);
    
    //sudut putaran lingkaran kecil
    double omega = 0; 
    
    //posisi dan titik Hypocycloid
    int x2= x1+radius2;
    int y2 = x1 + radius2;
   
    public Hypocycloid() {
        this.start();
    }

    @Override
    public void paint(Graphics g) {
        super.paintComponent(g);
        drawMainRect(g);
        drawBigCircle(g);
        drawSmallCircle(g);
        drawPoint(g);
    }
   
    public void drawBigCircle(Graphics g) {
        g.setColor(Color.red);
        g.drawOval(x, y, radius1, radius1);
    }
    
    public void drawSmallCircle(Graphics g) {
        g.setColor(Color.BLUE);
        g.drawOval(x1, y1, radius2, radius2);

    }

    public void drawMainRect(Graphics g) {
        g.setColor(Color.BLUE);
        g.drawRect(rectX, rectY, rectW, rectH);
    }
    
    public void drawPoint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(Color.black);
        g2.fillOval(x2, y2, 10, 10);
        
    }
  
    public void start() {
        LingkaranDalam lingkarandalam = new LingkaranDalam(this);
        Thread t1 = new Thread(lingkarandalam);
        t1.start();
        LingkaranLuar lingkarangluar = new LingkaranLuar(this);
        Thread t2 = new Thread(lingkarangluar);
        t2.start();
              
    }
    
    private class LingkaranLuar implements Runnable {
        
        Hypocycloid hypo;
        
        public LingkaranLuar(Hypocycloid hypo){
            this.hypo = hypo;
        }
        
        @Override
        public void run() {

            while (true) {
                for (int i = 0; i < 110; i++) {//sorong ke kiri
                    x += (int) ((rectW + rectX - radius1) / 100);
                    synchronized (hypo) {
                        hypo.repaint();
                    }
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        System.out.println("thread error 2");
                    }
                }

                for (int i = 0; i < 50; i++) { //sorong ke atas
                    y -= (int) ((rectH + rectY - radius1) / 50);
                    synchronized (hypo) {
                        hypo.repaint();
                    }
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        System.out.println("thread error 2");
                    }
                }

                for (int i = 0; i <110; i++) { //sorong ke kanan
                    x -= (int) ((rectW + rectX - radius1) / 100);
                    synchronized (hypo) {
                        hypo.repaint();
                    }
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        System.out.println("thread error 2");
                    }
                }

                for (int i = 0; i < 50; i++) { //sorong ke bawah
                    y += (int) ((rectH + rectY - radius1) / 50);
                    synchronized (hypo) {
                        hypo.repaint();
                    }
                    try {
                        Thread.sleep(20);
                    } catch (Exception e) {
                        System.out.println("thread error 2");
                    }
                }
            }

        }
    }

    private class LingkaranDalam implements Runnable {
        
        Hypocycloid hypo;
        
        public LingkaranDalam(Hypocycloid hypo){
            this.hypo = hypo;
        }

        @Override
        public void run() {
            boolean run = true;
            int a = radius1/2;
            int b = radius2/2;
            int rtemp = a - b;
            while (run) {
                for (int i = 0; i < 200; i++) {
                    omega -= Math.PI / 200;
                    x1 = (int) (x + rtemp + rtemp * Math.cos(omega));
                    y1 = (int) (y + rtemp + rtemp * Math.sin(omega));
                       
                    x2 = (int)(x1+(b-5)+b*Math.cos((a-b)*omega/b));
                    y2 = (int)(y1+(b-5)-b*Math.sin((a-b)*omega/b) );
                    
                    synchronized(hypo){
                        hypo.repaint();
                    }
                    try {
                        Thread.sleep(5);
                    } catch (Exception e) {
                        System.out.println("thread error 1");
                    }
                }

            }
        }
    }

    public static void main(String[] args) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame mainframe = new JFrame("Geometri Fun");
                mainframe.setLayout(new BorderLayout());
                mainframe.setPreferredSize(new Dimension(700, 500));
                Hypocycloid hypo = new Hypocycloid();
                mainframe.add(hypo);
                mainframe.setResizable(false);
                mainframe.pack();
                mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                mainframe.setVisible(true);
            }
        });
    }
}