Technology and Open Source Update

Latest information about new technology and open source.

Posts Tagged ‘java’

Transparent JFrame Background

Posted by megahacker136 on September 19, 2008

JFrame not come with the transparent window functionality, but you can try making it by using Java graphics class. The code below is the demo for the transparent frame background. Try the code below if you want to see it.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;

public class TransparentBackground extends JComponent
implements ComponentListener, WindowFocusListener, Runnable {

// constants —————————————————————

// instance —————————————————————-
private JFrame _frame;
private BufferedImage _background;
private long _lastUpdate = 0;
private boolean _refreshRequested = true;
private Robot _robot;
private Rectangle _screenRect;
private ConvolveOp _blurOp;

// constructor ————————————————————-
public TransparentBackground(JFrame frame) {

_frame = frame;

try {

_robot = new Robot();

} catch (AWTException e) {

e.printStackTrace();

return;

}

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

_screenRect = new Rectangle(dim.width, dim.height);

float[] my_kernel = {
0.10f, 0.10f, 0.10f,
0.10f, 0.20f, 0.10f,
0.10f, 0.10f, 0.10f
};

_blurOp = new ConvolveOp(new Kernel(3, 3, my_kernel));

updateBackground();

_frame.addComponentListener(this);

_frame.addWindowFocusListener(this);

new Thread(this).start();

}

// protected —————————————————————
protected void updateBackground() {

_background = _robot.createScreenCapture(_screenRect);

}

protected void refresh() {

if (_frame.isVisible() && this.isVisible()) {

repaint();

_refreshRequested = true;

_lastUpdate = System.currentTimeMillis();

}

}

// JComponent ————————————————————–
@Override
protected void paintComponent(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

Point pos = this.getLocationOnScreen();

BufferedImage buf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);

buf.getGraphics().drawImage(_background, -pos.x, -pos.y, null);

Image img = _blurOp.filter(buf, null);

g2.drawImage(img, 0, 0, null);

g2.setColor(new Color(255, 255, 255, 192));

g2.fillRect(0, 0, getWidth(), getHeight());

}

// ComponentListener ——————————————————-
@Override
public void componentHidden(ComponentEvent e) {
}

@Override
public void componentMoved(ComponentEvent e) {

repaint();

}

@Override
public void componentResized(ComponentEvent e) {

repaint();

}

@Override
public void componentShown(ComponentEvent e) {

repaint();

}

// WindowFocusListener —————————————————–
@Override
public void windowGainedFocus(WindowEvent e) {

refresh();

}

@Override
public void windowLostFocus(WindowEvent e) {

refresh();

}

// Runnable —————————————————————-
@Override
public void run() {

try {

while (true) {

Thread.sleep(100);

long now = System.currentTimeMillis();

if (_refreshRequested && ((now – _lastUpdate) > 1000)) {

if (_frame.isVisible()) {

Point location = _frame.getLocation();

_frame.setLocation(-_frame.getWidth(), -_frame.getHeight());

updateBackground();

_frame.setLocation(location);

refresh();

}

_lastUpdate = now;

_refreshRequested = false;

}

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

JFrame frame = new JFrame();

TransparentBackground bg = new TransparentBackground(frame);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(bg);

frame.pack();

frame.setSize(500, 500);

frame.setLocation(500, 500);

frame.setVisible(true);

}
}

Posted in Open Source, Programing | Tagged: , , | 3 Comments »

Conway’s Game of Life…

Posted by megahacker136 on September 19, 2008

The Game of Life is not your typical computer game. It is a ‘cellular automaton’, and was invented by Cambridge mathematician John Conway.

This game became widely known when it was mentioned in an article published by Scientific American in 1970. It consists of a collection of cells which, based on a few mathematical rules, can live, die or multiply. Depending on the initial conditions, the cells form various patterns throughout the course of the game.

The popular Conway’s Game of Life that we seen all over the internet today mostly written in java applet. But the program below is written in JFrame class. I hope you enjoy it…

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class ConwayGUI extends JFrame {

int gen;

int[][] conway = new int[50][50];

String[] speed = {“Slow”, “Fast”, “Hyper”};

javax.swing.Timer timerslow = new javax.swing.Timer(900, new startConway());

javax.swing.Timer timerfast = new javax.swing.Timer(100, new startConway());

javax.swing.Timer timerhyper = new javax.swing.Timer(1, new startConway());

javax.swing.Timer timerStart = new javax.swing.Timer(1, new mula());

public ConwayGUI () {

GUIComponent();

}

private void GUIComponent() {

this.setLayout(new BorderLayout());

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setTitle(“Game Of Life”);

this.setResizable(false);

JP1 = new JPanel();

JP1.setLayout(new GridLayout(50, 50));

arrButton = new JButton[50][50];

for (int i = 0; i < 50; i++) {

for (int j = 0; j < 50; j++) {

final int m = i;

final int n = j;

JP1.add(arrButton[i][j] = new JButton(“”));

arrButton[i][j].setPreferredSize(new java.awt.Dimension(12, 12));

arrButton[i][j].setBackground(new java.awt.Color(128, 128, 128));

arrButton[i][j].addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

arrBtnActionPerformed(evt, arrButton[m][n]);

}

});

}

}

add(BorderLayout.CENTER, JP1);

java.awt.GridBagConstraints gridBagConstraints;

JP2 = new JPanel();

JP2.setLayout(new GridBagLayout());

clearBtn = new javax.swing.JButton();

nextBtn = new javax.swing.JButton();

stopBtn = new javax.swing.JButton();

startBtn = new javax.swing.JButton();

speedLabel = new javax.swing.JLabel();

speedCombo = new javax.swing.JComboBox();

genLabel = new javax.swing.JLabel();

genField = new javax.swing.JTextField();

clearBtn.setText(“Clear”);

clearBtn.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

clearBtnActionPerformed(evt);

}

});

gridBagConstraints = new java.awt.GridBagConstraints();

gridBagConstraints.gridwidth = 5;

gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 16);

JP2.add(clearBtn, gridBagConstraints);

nextBtn.setText(“Next”);

nextBtn.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

nextBtnActionPerformed(evt);

}

});

gridBagConstraints = new java.awt.GridBagConstraints();

gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 16);

JP2.add(nextBtn, gridBagConstraints);

startBtn.setText(“Start”);

startBtn.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

startBtnActionPerformed(evt);

}

});

JP2.add(startBtn, new java.awt.GridBagConstraints());

speedLabel.setText(“Speed:”);

gridBagConstraints = new java.awt.GridBagConstraints();

gridBagConstraints.gridwidth = 3;

gridBagConstraints.insets = new java.awt.Insets(0, 21, 0, 0);

JP2.add(speedLabel, gridBagConstraints);

speedCombo.setModel(new javax.swing.DefaultComboBoxModel(speed));

speedCombo.setSelectedItem(speed[0]);

gridBagConstraints = new java.awt.GridBagConstraints();

gridBagConstraints.gridwidth = 4;

gridBagConstraints.insets = new java.awt.Insets(0, 11, 0, 0);

JP2.add(speedCombo, gridBagConstraints);

genLabel.setText(“Generation:”);

gridBagConstraints = new java.awt.GridBagConstraints();

gridBagConstraints.insets = new java.awt.Insets(0, 16, 0, 5);

JP2.add(genLabel, gridBagConstraints);

genField.setColumns(7);

genField.setEditable(false);

JP2.add(genField, new java.awt.GridBagConstraints());

add(BorderLayout.SOUTH, JP2);

pack();

}

private void clearBtnActionPerformed(java.awt.event.ActionEvent evt) {

genField.setText(“”);

gen = 0;

for (int i = 0; i < 50; i++) {

for (int j = 0; j < 50; j++) {

arrButton[i][j].setBackground(new java.awt.Color(128, 128, 128));

}

}

}

private void nextBtnActionPerformed(java.awt.event.ActionEvent evt) {

neighbor();

}

private void startBtnActionPerformed(java.awt.event.ActionEvent evt) {

String str = startBtn.getText();

if (str.equalsIgnoreCase(“Start”)) {

timerStart.start();

startBtn.setText(“Stop”);

clearBtn.setEnabled(false);

nextBtn.setEnabled(false);

} else {

timerStart.stop();

timerslow.stop();

timerfast.stop();

timerhyper.stop();

startBtn.setText(“Start”);

clearBtn.setEnabled(true);

nextBtn.setEnabled(true);

}

}

private void set_arr() {

String str = “java.awt.Color[r=255,g=255,b=0]“;

for (int i = 0; i < conway.length; i++) {

for (int j = 0; j < conway[i].length; j++) {

String s = “” + arrButton[i][j].getBackground();

if (s.equalsIgnoreCase(str)) {

conway[i][j] = 1;

} else {

conway[i][j] = 0;

}

}

}

}

private void neighbor() {

set_arr();

gen++;

genField.setText(“” + gen);

for (int i = 0; i < arrButton.length; i++) {

for (int j = 0; j < arrButton[i].length; j++) {

int bil = cal_Neighbor(conway, i, j);

String str = “java.awt.Color[r=255,g=255,b=0]“;

String kosong = “java.awt.Color[r=128,g=128,b=128]“;

String s = “” + arrButton[i][j].getBackground();

if (s.equalsIgnoreCase(str)) {

if (bil == 2 || bil == 3) {

arrButton[i][j].setBackground(new java.awt.Color(255, 255, 0));

} else if (bil < 2 || bil > 3) {

arrButton[i][j].setBackground(new java.awt.Color(128, 128, 128));

}

}

if (s.equalsIgnoreCase(kosong)) {

if (bil == 3) {

arrButton[i][j].setBackground(new java.awt.Color(255, 255, 0));

}

}

}

}

}

private int cal_Neighbor(int[][] arr, int num, int i) {

int total = 0;

if ((num – 1 != -1) && (i – 1 != -1)) {

if (arr[num - 1][i - 1] == 1) {

total += 1;

}

}

if (num – 1 != -1) {

if (arr[num - 1][i] == 1) {

total += 1;

}

}

if ((num – 1 != -1) && (i + 1 < arr[num].length)) {

if (arr[num - 1][i + 1] == 1) {

total += 1;

}

}

if (i – 1 != -1) {

if (arr[num][i - 1] == 1) {

total += 1;

}

}

if (i + 1 < arr[num].length) {

if (arr[num][i + 1] == 1) {

total += 1;

}

}

if ((num + 1 < arr.length) && (i – 1 != -1)) {

if (arr[num + 1][i - 1] == 1) {

total += 1;

}

}

if ((num + 1 < arr.length)) {

if (arr[num + 1][i] == 1) {

total += 1;

}

}

if ((num + 1 < arr.length) && (i + 1 < arr[num].length)) {

if (arr[num + 1][i + 1] == 1) {

total += 1;

}

}

return total;

}

private void arrBtnActionPerformed(java.awt.event.ActionEvent evt, JButton arrayBtn) {

String str = “java.awt.Color[r=255,g=255,b=0]“;

String s = “” + arrayBtn.getBackground();

if (s.equalsIgnoreCase(str)) {

arrayBtn.setBackground(new java.awt.Color(128, 128, 128));

} else {

arrayBtn.setBackground(new java.awt.Color(255, 255, 0));

}

}

class startConway implements ActionListener {

public void actionPerformed(ActionEvent e) {

neighbor();

}

}

class mula implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

if (speedCombo.getSelectedIndex() == 0) {

timerhyper.stop();

timerfast.stop();

timerslow.start();

} else if (speedCombo.getSelectedIndex() == 1) {

timerhyper.stop();

timerslow.stop();

timerfast.start();

} else if (speedCombo.getSelectedIndex() == 2) {

timerslow.stop();

timerfast.stop();

timerhyper.start();

}

} catch (Exception ew) {

}

}

}

public static void main(String[] args) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

ConwayGUI showWin = new ConwayGUI ();

showWin.setVisible(true);

}

});

}

private JButton[][] arrButton;

private JPanel JP1;

private JPanel JP2;

private JButton clearBtn;

private JButton nextBtn;

private JButton stopBtn;

private JButton startBtn;

private JComboBox speedCombo;

private JLabel speedLabel;

private JTextField genField;

private JLabel genLabel;

}

Posted in Open Source, Programing | Tagged: , , | Leave a Comment »

Java: Koch Snowflakes Fractal source

Posted by megahacker136 on September 2, 2008

Source code:

import java.awt.*;
import javax.swing.*;

/*
* koch.java
*
* Created on August 17, 2008, 9:29 AM
*/
/**
*
* @author  Megahacker
*/
public class koch extends javax.swing.JFrame {

drawKoch draw = new drawKoch();
int nxt = 0;
int[] s = {600, 100, 50, 10, 5, 3, 2, 1};

/** Creates new form koch */
public koch() {
initComponents();
panel2.add(draw);
}

/** 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.
*/
// <editor-fold defaultstate=”collapsed” desc=”Generated Code”>//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

panel1 = new javax.swing.JPanel();
resetBtn = new javax.swing.JButton();
backBtn = new javax.swing.JButton();
nextBtn = new javax.swing.JButton();
panel2 = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(“Koch Snowflakes Fractal”);
setResizable(false);

panel1.setLayout(new java.awt.GridBagLayout());

resetBtn.setText(“Reset”);
resetBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetBtnActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
panel1.add(resetBtn, gridBagConstraints);

backBtn.setText(“Back”);
backBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
backBtnActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
panel1.add(backBtn, gridBagConstraints);

nextBtn.setText(“Next”);
nextBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextBtnActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
panel1.add(nextBtn, gridBagConstraints);

getContentPane().add(panel1, java.awt.BorderLayout.CENTER);

panel2.setPreferredSize(new java.awt.Dimension(600, 600));
panel2.setLayout(new javax.swing.BoxLayout(panel2, javax.swing.BoxLayout.LINE_AXIS));
getContentPane().add(panel2, java.awt.BorderLayout.PAGE_START);

pack();
}// </editor-fold>//GEN-END:initComponents

private void resetBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetBtnActionPerformed
// TODO add your handling code here:
draw.size = 600;
draw.repaint();
}//GEN-LAST:event_resetBtnActionPerformed

private void nextBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextBtnActionPerformed
// TODO add your handling code here:
nxt++;
draw.size = s[nxt];
draw.repaint();
int num = draw.size;
backBtn.setEnabled(true);
if (num == 1) {
nextBtn.setEnabled(false);
}
}//GEN-LAST:event_nextBtnActionPerformed

private void backBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backBtnActionPerformed
// TODO add your handling code here:
nxt–;
draw.size = s[nxt];
draw.repaint();
int num = draw.size;
nextBtn.setEnabled(true);
if (num == 600) {
backBtn.setEnabled(false);
}
}//GEN-LAST:event_backBtnActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new koch().setVisible(true);
}
});
}

// Variables declaration – do not modify//GEN-BEGIN:variables
private javax.swing.JButton backBtn;
private javax.swing.JButton nextBtn;
private javax.swing.JPanel panel1;
private javax.swing.JPanel panel2;
private javax.swing.JButton resetBtn;
// End of variables declaration//GEN-END:variables
}

class drawKoch extends JPanel {

int size = 600;

public drawKoch() {
}

private koordinat mid(koordinat a, koordinat b) {
return new koordinat((a.x + b.x) / 2, (a.y + b.y) / 2);
}

private koordinat third(koordinat a, koordinat b) {
return new koordinat((b.x – a.x) / 3 + a.x, (b.y – a.y) / 3 + a.y);
}

private double dist(koordinat a, koordinat b) {
return Math.sqrt(Math.pow(b.x – a.x, 2) + Math.pow(b.y – a.y, 2));
}

private void drawKochSide(Graphics2D g2, koordinat a, koordinat b) {
if (dist(a, b) <= size) {
g2.drawLine((int) a.x, (int) a.y, (int) b.x, (int) b.y);
} else {
koordinat c = new koordinat(Math.cos(Math.PI / 3.0 + Math.atan2(b.y – a.y, b.x – a.x)) * (dist(a, b) / 3) + third(a, b).x,
Math.sin(Math.PI / 3.0 + Math.atan2(b.y – a.y, b.x – a.x)) * (dist(a, b) / 3) + third(a, b).y);
drawKochSide(g2, a, third(a, b));
drawKochSide(g2, third(a, b), c);
drawKochSide(g2, c, mid(third(a, b), b));
drawKochSide(g2, mid(third(a, b), b), b);
}
}

@Override
public void paintComponent(Graphics g) {
double w = getSize().getWidth();
double h = getSize().getHeight();

g.setColor(Color.BLACK);
g.fillRect(0, 0, (int) w, (int) h);
g.setColor(Color.YELLOW);
Graphics2D g2 = (Graphics2D) g;

double ox = 0.0, oy = 0.0;

if ((w * 2.0 * Math.sqrt(3.0)) < h * 3.0) {
h = (w * 2.0 * Math.sqrt(3.0)) / 3.0;
oy = (getSize().getHeight() – 1 – h) / 2.0;
}
if ((w * 2.0 * Math.sqrt(3.0)) > h * 3.0) {
w = (h * 3.0) / (2 * Math.sqrt(3.0));
ox = (getSize().getWidth() – 1 – w) / 2.0;
}
drawKochSide(g2, new koordinat(w / 2.0 + ox, oy), new koordinat(ox, h * 0.75 + oy));
drawKochSide(g2, new koordinat(w + ox, h * 0.75 + oy), new koordinat(w / 2.0 + ox, oy));
drawKochSide(g2, new koordinat(ox, h * 0.75 + oy), new koordinat(w + ox, h * 0.75 + oy));
}
}

class koordinat {

public double x,  y;

public koordinat(double x, double y) {
this.x = x;
this.y = y;
}
}

Output preview:

First stage

third stage

Posted in Open Source, Programing, Uncategorized | Tagged: , , | 1 Comment »