HOME | CATATAN |

Jumat, 16 November 2007

Tugas Pengolahan Citra

Berikut tugas pen citraan yang diberikan oleh Dosen Citra :

/*
* Swing version.
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import java.awt.geom.*;
import java.applet.*;
import java.util.Hashtable;
import java.awt.font.TextAttribute;

public class ArtLab extends JApplet implements ActionListener
{

public int frameNumber = -1;
boolean frozen = true;
Timer timer;
AnimationPane animationPane;

void buildUI(Container container) {
int fps = 0;

//How many milliseconds between frames?
int delay = (fps > 0) ? (1000 / fps) : 100;

//Set up a timer that calls this object's action handler.
timer = new Timer(delay, this);
timer.setInitialDelay(0);
timer.setCoalesce(true);

animationPane = new AnimationPane();
container.add(animationPane, BorderLayout.CENTER);

animationPane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (frozen) {
frozen = false;
startAnimation();
} else {
frozen = true;
stopAnimation();
}
}
});
}

//Can be invoked from any thread.
public synchronized void startAnimation() {
if (frozen) {
//Do nothing. The user has requested that we
//stop changing the image.
} else {
//Start animating!
if (!timer.isRunning()) {
timer.start();
}
}
}

//Can be invoked from any thread.
public synchronized void stopAnimation() {
//Stop the animating thread.
if (timer.isRunning()) {
timer.stop();
}
}

public void actionPerformed(ActionEvent e) {
//Advance animation frame.
frameNumber++;

//Display it.
animationPane.repaint();

}

//Here's where your shapes are drawn!
public class AnimationPane extends JPanel
{

//declare variables for use with shapes
AffineTransform original;
Stroke originalStroke;
BasicStroke wideStroke;
Graphics2D g2;

int screenWidth;
int screenHeight;
int width; //width, height, x and y used as parameters for drawXxx and fillXxx methods
int height;
int x;
int y;
int startAngle; //next two used only with the drawArc and fillArc methods
int arcAngle;
int x1; //next four used with the drawLine method
int y1;
int x2;
int y2;

public AnimationPane() {
}

//Draw the current frame of animation.
public void paintComponent(Graphics g)
{
super.paintComponent(g); //paint any space not covered
//by the background image
g2 = (Graphics2D) g;
wideStroke = new BasicStroke(8.0f);
originalStroke = g2.getStroke();

//get the screen size
screenWidth = getWidth();
screenHeight = getHeight();

//set the colors
Color black = Color.black;
Color blue = Color.blue;
Color gray = Color.gray;
Color red = Color.red;
Color yellow = Color.yellow;
Color orange = Color.orange;
Color cyan = Color.cyan;
Color green = Color.green;
Color magenta = Color.magenta;
Color white = Color.white;
Color pink = Color.pink;

//draw the background
g2.setPaint (white);
g2.fillRect (0, 0, screenWidth, screenHeight);

//draw the shapes

newShape();
g2.setPaint(Color.black);
g2.drawString("("+xFwdLine (50, 1)+",100)", xFwdLine (50, 1), 95);

newShape();
g2.setPaint(Color.black);
g2.drawString("("+xFwdLine (250, 1)+",100)", xFwdLine (200, 1), 95);

newShape();
g2.setPaint(Color.black);
g2.drawString("("+xBwdLine (550, 1)+",250)", xBwdLine (550, 1), 265);

newShape();
g2.setPaint(Color.black);
g2.drawString("("+xBwdLine (750, 1)+",250)", xBwdLine (700, 1), 265);

//yellow rectangle
newShape ( );
x = xFwdLine (50, 1);
y = 100;
width = 200;
height = 100;
g2.setPaint (yellow);
g2.fillRect (x, y, width, height);

//red rectangle
newShape ( );
x = xBwdLine (550, 1);
y = 150;
width = 200;
height = 100;
g2.setPaint (red);
g2.fillRect (x, y, width, height);

newShape();
g2.setPaint(Color.black);
g2.drawString("("+xBwdLine (550, 1)+",150)", xBwdLine (550, 1), 145);

newShape();
g2.setPaint(Color.black);
g2.drawString("("+xBwdLine (750, 1)+",150)", xBwdLine (700, 1), 145);

newShape();
g2.setPaint(Color.black);
g2.drawString("("+xFwdLine (250, 1)+",200)", xFwdLine (200, 1), 215);

newShape();
g2.setPaint(Color.black);
g2.drawString("("+xFwdLine (50, 1)+",200)", xFwdLine (50, 1), 215);

//red rectangle
newShape ( );
x = 50;
y = 340;
width = 200;
height = 120;
g2.setPaint (orange);
g2.fillRect (x, y, width, height);

newShape();
g2.setPaint(Color.black);
g2.drawString("Kelompok Tugas Pen-citraan : ", 55, 370);
g2.drawString("-- Fahrizal [ ] : ", 55, 385);
g2.drawString("-- Hardhika S [ 10105733 ] : ", 55, 400);
g2.drawString("-- Suradiyanto [ ] : ", 55, 415);
g2.drawString("-- Adi Junaedi [ ] : ", 55, 430);
g2.drawString("-- Hery Istyawan [ ] : ", 55, 445);

newShape ( );
x = (screenWidth/2) - 60;
y = 40;
width = 180;
height = 30;
g2.setPaint (orange);
g2.fillRect (x, y, width, height);

newShape();
g2.setPaint(Color.black);
g2.drawString("[ Click here to ON / OFF ]", (screenWidth/2)-40, 55);
} //Do not erase this bracket

//Methods for moving the objects
//Returns the x-coordinate that will move the object forward in a line in the x-direction
//start is where on the screen you want it to start, speed is how fast it should move
public int xFwdLine (int start, int speed)
{
double x;

x = start + frameNumber*speed;

//if component has travelled off-screen
if (x > screenWidth) {
x = x - (Math.floor(x/screenWidth))*screenWidth; //calculate the remainder
}
return (int) x;
}

//Returns the x-coordinate that will move the object backward in a line in the x-direction
//start is where on the screen you want it to start, speed is how fast it should move
public int xBwdLine (int start, int speed)
{
double x;

x = start - frameNumber*speed;

//if component has travelled off-screen
if ((x+width)< 0) {
x = Math.abs(x) - (Math.floor(Math.abs(x)/screenWidth))*screenWidth; //calculate the remainder
x = screenWidth - width - x;
}
return (int) x;
}

//resets the Stroke and rotation attributes of the new shape
public void newShape ()
{
if (original != null)
{
g2.setTransform(original);
}
if (g2.getStroke() == wideStroke)
{
g2.setStroke (originalStroke);
}
}
}

//Method to start program
public static void main(String[] args) {

JFrame f = new JFrame("Tugas pencitraan");
final ArtLab controller = new ArtLab();
controller.buildUI(f.getContentPane());

f.addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
controller.stopAnimation();
}
public void windowDeiconified(WindowEvent e) {
controller.startAnimation();
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

f.setSize(new Dimension(800, 550));
f.setVisible(true);

controller.startAnimation();
}
}

 
test