Site Sponsors:
Voice, Text, and HTML Logging 
Just a quick note to let everyone know that we just updated EzLog for Java.

Here is the project. (http://sourceforge.net/projects/ezlogforjava/)

EzLog4J now has the ability to capture voice notes, as well as our traditional Text and HTML meanderings.

Tested just fine across Linux (Ubuntu), OS X, and Windows.

Enjoy,

-Rn

[ view entry ] ( 2135 views )   |  permalink  |  related link
Order Of Operations 
When we set our layout *after* we have added our components, we do not need to use any of the size-oriented (setSize(), setPreferredSize(), setMaximumSize(), setMinimumSize()) operations:


import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
* Use the content of a panel for the main form. Application ends when a main form
* is closed.
*
* @author profnagy
*/
public class MainFrame extends JFrame implements Runnable {

public Thread pwThread = null;

public MainFrame(JPanel jPanel) {
super();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
if (jPanel == null) {
JLabel lbl = new JLabel();
lbl.setText(this.getClass().getName());
this.add(lbl);
} else {
this.add(jPanel); // do this first -
}
this.setLayout(new CardLayout()); // the layout (queries all children), afterwards
this.pack();
com.soft9000.gui.Misc.ScreenCenter(this);
}

public void start() {
pwThread = new Thread(this);
pwThread.start();
this.setVisible(true);
}

public void run() {
}
}


So if you ever see that your window size does not work, check to see when we have applied our layout manager. In general, simply remember to assign the layout manager just before calling pack().

Enjoy,

-Rn

[ view entry ] ( 1814 views )   |  permalink
Analog Clock - Combining two examples 
Went looking for an analog clock today. Found a nice one, designed for a Java Applet.

Converted it to an application - it was blinky, so we mixed-in another example.

Tried to share the result with the author, but the comment / post was too big.

Because I think Manoj's concept of having an outer "gear" revolving around the clock is cool, I decided to post the blend here so as to share-it with the author... as well as anyone else:


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import javax.swing.JFrame;

public class Clock extends JFrame {
class MoveIt implements MouseMotionListener {

@Override
public void mouseMoved(MouseEvent me) {
}

@Override
public void mouseDragged(MouseEvent me) {
xmou = me.getX(); //changing the clock position on mouse drag
ymou = me.getY(); //changing the clock position on mouse drag
}
}

// The object we will use to write with instead of the standard screen graphics
Graphics bufferGraphics = null;
// The image that will contain everything that has been drawn on
// bufferGraphics.
Image offscreen = null;
// To get the width and height of the applet.
Dimension dim;
int xmou = 200; //set the center of circle
int ymou = 200; //set the center of circle
double theta = -0.1047; //theta for second's hand
int x = xmou; //x position of Second's hand
int y = ymou; //y position of second's hand
int p, b; //perpendicular and base of Second's hand
int h = 60; //hypotenous(heigth) of clock's hand
double the = -0.1047; //theta for creating outer circle
double thetamin = -0.1047; //theta for minutes hand
int xm = xmou; //x position of minute's hand
int ym = ymou; //y position of minute's hand
int pmin, bmin; //perpendicular and base of Minute's hand
double thetah = -0.1047; //theta for hour hand
int xh = xmou; //y position of hour's hand
int yh = ymou; //y position of hour's hand
int ph, bh; //perpendicular and base of hour's hand
double thetan = -0.0; //theta for numbers of clock
int xn = xmou; //x position of Clock numbers
int yn = ymou; //y position of Clock numbers
int pn, bn; //perpendicular and base of clock numbers
int num = 0; //for writing the numbers

public Clock(int x, int y) {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Click and drag to move the clock");
dim = getSize();
dim.setSize(x, y);
setSize((int) dim.getWidth(), (int) dim.getWidth());
setBackground(Color.WHITE);
}

@Override
public void paint(Graphics g) {
if (offscreen == null) {
return;
}

bufferGraphics.clearRect(0, 0, dim.width, dim.width);
bufferGraphics.setColor(Color.red);
bufferGraphics.drawString("Bad Double-buffered", 10, 10);

doPaint(bufferGraphics);

if (g != null) {
g.drawImage(offscreen, 0, 0, this);
}
}

@Override
public void update(Graphics g) {
paint(g);
}

private void doPaint(Graphics g) {
//for writing numbers in clock and outer circle
for (int ss = 0; ss < 60; ss++) {

int xocir = xmou; //x position of outer circle
int yocir = ymou; //y position of outer circle
int pocir, bocir; //perpendicular and base of outer circle

pocir = (int) (Math.sin(the) * (h + 23));
bocir = (int) (Math.cos(the) * (h + 23));
xocir = xocir - pocir;
yocir = yocir - bocir;
the = the - 0.1047;

g.setColor(Color.BLUE);
g.drawLine(xocir + 5, yocir + 5, xocir, yocir);
g.setColor(Color.BLACK);
if (ss % 5 == 0) {
num++;
if (num > 12) {
num = 1;
}
xn = xmou;
yn = ymou;
if (thetan <= -6.28318531) {
thetan = 0.0;
}
thetan = thetan - 0.523598776;
pn = (int) (Math.sin(thetan) * (h + 10));
bn = (int) (Math.cos(thetan) * (h + 10));
xn = xn - pn;
yn = yn - bn;
g.drawString("" + num, xn - 3, yn + 5);
}

}
// Drawing Clock hands
g.setColor(Color.BLACK);
g.drawLine(xmou, ymou, xm, ym); //drawing minute's hand
g.drawLine(xmou, ymou, xh, yh); //drawing hour's hand

g.setColor(Color.RED);
g.drawLine(xmou, ymou, x, y); //drawing second's hand
}

public void newpoint() {
Calendar now = Calendar.getInstance(); //creating a Calendar variable for getting current time

// For the second hand:
x = xmou;
y = ymou;
theta = -0.1047;
theta = theta * now.get(Calendar.SECOND);
p = (int) (Math.sin(theta) * h);
b = (int) (Math.cos(theta) * h);
x = x - p;
y = y - b;
//theta=theta - 0.1047;

// For the minute hand:
xm = xmou;
ym = ymou;

thetamin = -0.1047;
thetamin = thetamin * now.get(Calendar.MINUTE);
pmin = (int) (Math.sin(thetamin) * (h - 6));
bmin = (int) (Math.cos(thetamin) * (h - 6));
xm = xm - pmin;
ym = ym - bmin;

// For the hour hand:
xh = xmou;
yh = ymou;
thetah = -0.1047;
thetah = thetah * now.get(Calendar.HOUR) * 5;

if (now.get(Calendar.MINUTE) >= 12 && now.get(Calendar.MINUTE) < 24) {
thetah = thetah - 0.1047;
} else if (now.get(Calendar.MINUTE) >= 24 && now.get(Calendar.MINUTE) < 36) {
thetah = thetah - (2 * 0.1047);
} else if (now.get(Calendar.MINUTE) >= 36 && now.get(Calendar.MINUTE) < 48) {
thetah = thetah - (3 * 0.1047);
} else if (now.get(Calendar.MINUTE) >= 48 && now.get(Calendar.MINUTE) < 60) {
thetah = thetah - (4 * 0.1047);
}

ph = (int) (Math.sin(thetah) * (h - 15));
bh = (int) (Math.cos(thetah) * (h - 15));
xh = xh - ph;
yh = yh - bh;
}

@Override
public void setVisible(boolean bVis) {
super.setVisible(bVis);
if (offscreen == null) {
offscreen = createImage(dim.width, dim.height);
if (offscreen != null) {
bufferGraphics = offscreen.getGraphics();
addMouseMotionListener(new MoveIt());
}
}
}

public static void main(String[] args) {
Clock analog = new Clock(500, 500);
analog.h = 60;
analog.setVisible(true);

while (true) {
analog.newpoint();
analog.repaint();
try {
Thread.sleep(6);
} catch (Exception e) {
}
}
}
}


Such is the joy of sharing examples: What would have taken most of us a day to think about, took about 10 minutes to glue together.

Enjoy,

-Rn


[ view entry ] ( 9823 views )   |  permalink  |  related link

<<First <Back | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | Next> Last>>