Artikel kali ini saya mau sharing proyek kuliah. Pas pemrograman berorientasi obyek I disuruh buat java line screensaver yang membuat line secara random, kemudian setelah garis terbuat sebanyak tertentu kanvasnya akan kosong dan garisnya akan terbentuk lagi.
Hasilnya kayak gini :
Code nya seperti ini : pertama buat dulu Line.java kemudian buat ScreenSaver.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
public class Line {
private int x, y, x1, y1;
private Random acak;
private int width, height;
public Line(Dimension dimension) {
acak = new Random();
width = (int) dimension.getWidth();
height = (int) dimension.getHeight();
x = acak.nextInt(width + (int) (10 * Math.random()));
y = acak.nextInt(height + (int) (60 * Math.random()));
x1 = acak.nextInt(width + (int) (60 * Math.random()));
y1 = acak.nextInt(height + (int) (30 * Math.random()));
}
public void draw(Graphics g1, Graphics g2) {
g1.setColor(Color.GREEN);
g1.drawLine(x1, y, x, y1);
g2.setColor(Color.red);
g2.drawLine(x, x1, y1, x);
}
}
Dan kemudian ScreenSaver.java
import javax.swing.SwingUtilities;
import java.util.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.JPanel;
import javax.swing.JWindow;
public class ScreenSaver extends JWindow {
private ArrayList Lines = new ArrayList();
private Timer timer;
private BufferedImage image;
private Dimension windowDimension;
private int numberOfLines = 100;
public ScreenSaver() {
super();
windowDimension = Toolkit.getDefaultToolkit().getScreenSize();
timer = new Timer();
timer.schedule(new LineGenerator(), 150, 150);
setSize(windowDimension);
setBackground(Color.black);
setVisible(true);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent m) {
Lines.clear();
}
});
add(new JPanel() {
public void paintComponent(Graphics g) {
Graphics g1 = (Graphics) g;
Graphics g2 = (Graphics) g;
g1.drawImage(image, 0, 0, null);
g2.drawImage(image, 0, 0, null);
for (int i = 0; i < Lines.size(); i++) {
Line line = (Line) Lines.get(i);
line.draw(g1, g2);
}
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new
Runnable() {
public void run( ) {
new ScreenSaver();
}
});
}
public int getNumberOfLines() {
return numberOfLines;
}
public void setNumberOfLines(int numberOfLines) {
this.numberOfLines = numberOfLines;
}
class LineGenerator extends TimerTask{
private Thread LineThread;
public void start()
{
if (LineThread == null) {
LineThread = new Thread(this);
LineThread.start();
}
}
public void stop() {
if (LineThread != null) {
LineThread.stop();
LineThread = null;
}
}
public void run() {
Lines.add(new Line(windowDimension));
int i = 0;
try {
while (i < Lines.size()) {
Line line = (Line) Lines.get(i);
i++;
if ((i % numberOfLines) == 0) {
Lines.clear();
repaint();
} else if ((i % 25 == 0 || i % 45 == 0)) {
setBackground(Color.black);
} else if ((i % 10 == 0)) {
setBackground(Color.BLUE);
}
}
Thread.sleep(100);
repaint();
} catch (InterruptedException e) {
}
}
}
}
O'key...Gmana ? jangan lupa sertakan nama saya ya kalu presentasi proyek screensaver ini..hahahah Just Kiding.... :-)
Related Posts :
- Back to Home »
- java line screensaver , programming , project kuliah »
- Java Line ScreenSaver