Kode program:
1. FrameUtama
package skill;
import java.awt.*;
public class FrameUtama extends Frame{
public FrameUtama(){
add(new Menggambar());
setSize(600,600);
setBackground(Color.yellow);
setVisible(true);
}
public static void main(String[] args) {
new FrameUtama();
}
}
2. Menggambar
package skill;
import java.awt.*;
public class Menggambar extends Canvas{
LingkaranDrawable id= new LingkaranDrawable(600);
SegiEmpatDrawable sed= new SegiEmpatDrawable(50,200);
public void paint(Graphics g){
id.setColor(Color.black);
id.setPosition(100,357);
id.draw(g);
id.setColor(Color.red);
id.setPosition(100,100);
id.draw(g);
sed.setColor(Color.blue);
sed.setPosition(300, 150);
sed.draw(g);
}
}
3. Drawable
package skill;
import java.awt.Color;
import java.awt.Graphics;
public interface Drawable {
public void draw(Graphics g);
public void setColor(Color c);
public void setPosition(int x,int y);
}
4. SegiEmpatDrawable
package skill;
import java.awt.Color;
import java.awt.Graphics;
public class SegiEmpatDrawable extends SegiEmpat implements Drawable {
public Color c;
public int x,y;
public SegiEmpatDrawable(double sisiDatar, double sisiTegak) {
super(sisiDatar, sisiTegak);
c=Color.BLUE;
}
public void draw(Graphics g) {
g.setColor(c);
g.drawRect(x, y, (int)getSisiDatar(), (int)getSisiTegak());
g.drawString("Luas = "+(int)luas(), x+(int)sisiDatar+10, y+20);
g.drawString("Keliling = "+(int)keliling(), x+(int)sisiDatar+10, y);
}
public void setColor(Color c) {
this.c=c;
}
public void setPosition(int x, int y) {
this.x=x;
this.y=y;
}
}
5. SegiEmpat
package skill;
public class SegiEmpat {
public double sisiDatar,sisiTegak;
public SegiEmpat(double sisiDatar,double sisiTegak){
this.sisiDatar=20;
this.sisiTegak=80;
}
public double getSisiDatar() {
return this.sisiDatar;
}
public double getSisiTegak() {
return this.sisiTegak;
}
public double keliling(){
return (2 * this.sisiDatar) + (2 * this.sisiTegak);
}
public double luas(){
return this.sisiDatar * this.sisiTegak;
}
}
6. LingkaranDrawable
package skill;
import java.awt.Color;
import java.awt.Graphics;
public class LingkaranDrawable extends Lingkaran implements Drawable {
public Color c;
public int x,y;
public LingkaranDrawable(double radius){
super(radius,radius);
c=Color.red;}
public void draw(Graphics g){
g.drawRoundRect(y, y, (int)getRadius(), x, x, x );
g.drawRoundRect(x, x, (int)getRadius(), x, x, x );
g.drawString(" Keliling ="+(int)Keliling(), y+10, y+70);
g.drawString(" Luas ="+(int)Luas(), y+10, y+50);
}
public void setColor(Color c){
this.c=c;
}
public void setPosition(int x,int y){
this.x=x;
this.y=y;
}
}
7. Lingkaran
package skill;
public class Lingkaran {
public double Pi,radius;
public Lingkaran(double Pi,double radius){
this.Pi=3.14;
this.radius=100;
}
public double getRadius() {
return this.radius;
}
public double Keliling() {
return 2*this.radius*this.Pi;
}
public double Luas() {
return this.radius*this.radius*this.Pi;
}
}
8. BangunGeometri2D
package skill;
public abstract class BangunGeometri2D {
public double keliling;
public double luas;
}
0 komentar:
Posting Komentar