Created
December 20, 2017 15:00
-
-
Save sugiartocokrowibowo/cd23498186b704a890afc466c1a67889 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package latihan4; | |
import java.awt.Color; | |
import java.awt.EventQueue; | |
import java.awt.Font; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.font.TextAttribute; | |
import java.text.AttributedString; | |
import javax.swing.JComponent; | |
import javax.swing.JFrame; | |
public class Main extends JFrame { | |
public Main(){ | |
initUI(); | |
} | |
private void initUI() { | |
add(new Canvas()); | |
setTitle("Computer Graphics"); | |
setSize(1000, 600); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setLocationRelativeTo(null); | |
} | |
public static void main(String[] args){ | |
EventQueue.invokeLater(new Runnable(){ | |
public void run() { | |
Main unsulbar = new Main(); | |
unsulbar.setVisible(true); | |
} | |
}); | |
} | |
public class Warna { | |
public Warna(String namaWarna, String codeWarna) { | |
this.nama = namaWarna; | |
this.code = codeWarna; | |
} | |
String nama; | |
String code; | |
} | |
public class Canvas extends JComponent { | |
public void paint(Graphics g) { | |
Graphics2D g2d = (Graphics2D) g; | |
int[]data = {42,26,16,50,18,2,8,15,23,26,46,30,35,41,15,25,20,25}; | |
Warna[]arrWarna ={ | |
new Warna("TURQUOISE","#1abc9c"), | |
new Warna("EMERALD","#2ecc71"), | |
new Warna("PETER RIVER","#3498db"), | |
new Warna("AMETHYST","#9b59b6"), | |
new Warna("WET ASPHALT","#34495e"), | |
new Warna("GREEN SEA","#16a085"), | |
new Warna("NEPHRITIS","#27ae60"), | |
new Warna("BELIZE HOLE","#2980b9"), | |
new Warna("WISTERIA","#8e44ad"), | |
new Warna("MIDNIGHT BLUE","#2c3e50"), | |
new Warna("SUN FLOWER","#f1c40f"), | |
new Warna("CARROT","#e67e22"), | |
new Warna("ALIZARIN","#e74c3c"), | |
//new Warna("CLOUDS","#ecf0f1"), | |
new Warna("CONCRETE","#95a5a6"), | |
new Warna("ORANGE","#f39c12"), | |
new Warna("PUMPKIN","#d35400"), | |
new Warna("POMEGRANATE","#c0392b"), | |
new Warna("SILVER","#bdc3c7"), | |
new Warna("ASBESTOS","#7f8c8d")}; | |
int xo,yo,y1 = this.getHeight()-50; | |
int tinggi,lebar=(int)((this.getWidth()-100)/(double)data.length); | |
int k=0; | |
for(int i=0;i<data.length;i++){ | |
tinggi = data[i]*10; | |
yo = y1 - tinggi; | |
xo = 50+(i*lebar)+2; | |
g2d.setColor(Color.decode(arrWarna[k].code)); | |
g2d.fillRect(xo, yo, lebar-4, tinggi); | |
g2d.setFont(new Font("Arial", Font.PLAIN, 16)); | |
g2d.drawString(""+data[i], xo+lebar/2-10, y1+18); | |
k++; | |
if(k>=arrWarna.length){ | |
k=0; | |
} | |
} | |
g2d.dispose(); | |
//--------------------------------------------------------------------------------------------------- | |
}//end of paint(Graphics g) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment