Kali ini akan ada tutorial grafika komputer untuk color boundary atau memberikan warna pada objek yang dibuat.
Berikut adalah code-nya :
import java.awt.Color;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.Stack;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
public class DrawingPanel extends javax.swing.JFrame {
BufferedImage img;
public DrawingPanel() {
initComponents();
img = new BufferedImage(jLabel1.getWidth(), jLabel1.getHeight(), BufferedImage.TYPE_INT_RGB);
jLabel1.setIcon(new ImageIcon(img));
}
public void colorRecursive(int x, int y, Color o, Color n) {
if (img.getRGB(x, y) != n.getRGB() && img.getRGB(x, y) == o.getRGB()) {
img.setRGB(x, y, n.getRGB());
colorRecursive(x, y + 1, o, n);
colorRecursive(x, y - 1, o, n);
colorRecursive(x - 1, y, o, n);
colorRecursive(x + 1, y, o, n);
}
}
public void colorStack(int x, int y, Color o, Color n) {
Stack stack = new Stack();
stack.push(new Point(x, y));
while (!stack.empty()) {
Point point = stack.pop();
int currentX = point.x;
int currentY = point.y;
int currentColor = img.getRGB(currentX, currentY);
if (currentColor != n.getRGB() && currentColor == o.getRGB()) {
img.setRGB(currentX, currentY, n.getRGB());
stack.push(new Point(currentX + 1, currentY));
stack.push(new Point(currentX - 1, currentY));
stack.push(new Point(currentX, currentY + 1));
stack.push(new Point(currentX, currentY - 1));
}
}
}
public void CirclePolar(int xc, int yc, int r) {
double x, y;
for (double i = 1.0 / r; i <= 2 * Math.PI; i += 1.0 / r) {
y = yc + r * Math.sin(i);
x = xc + r * Math.cos(i);
img.setRGB((int) x, (int) y, Color.white.getRGB());
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel1MouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
);
pack();
}//
private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if(SwingUtilities.isLeftMouseButton(evt)){
CirclePolar(evt.getX(), evt.getY(), 50);
}
if(SwingUtilities.isRightMouseButton(evt)){
colorRecursive(evt.getX(), evt.getY(),Color.black, Color.yellow);
}
jLabel1.setIcon(new ImageIcon(img));
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DrawingPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DrawingPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DrawingPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DrawingPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DrawingPanel().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
Itulah cara Grafika Komputer : Mewarnai Objek - Java.


Comments
Post a Comment