Remote Coder Blog

A brief respite from my coding closet

Java Timer Applet

November17

I am not certain whether or not it’s taboo to post your code on the internet. There are several sites out there that do this, and I know without some code examples every now and then to look at, I might never understand certain elements.

It was hard to find any thing that closely related to what I’m about to post here. Basically, I was looking for some example that would illustrate for me how to use the Javax.swing.timer method.

I could understand that I needed to create the method by using:
timer = new Timer(1000, new TimerListener); which initializes a variable called timer as a new Timer method, counting by seconds, and called using the TimerListener class (inside class I had to create).

The reason why I was trying to create this java class was so I could imbed it into an applet for use on an html page.

This is the html code I used to call the applet:
<.html>

<.head><.title>Timer Applet<./timer><./head>
<.body><.applet code="timer.class" width="300" height="100"><./applet><./body><./html>
<./code>

Basically, the applet tag calls the java class and puts it in a predetermined sized window. When you load the html page in your browser (with Java enabled) it will call the java class, and run it.

So my Java code is as follows:
//Name: David Nick
//Date: 11.16.2009
//Purpose: To create a Java Timer Applet

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

public class timer extends JApplet
{

private JTextField tLabel;
private JPanel tPanel;
private JPanel bPanel;
private JButton startBtn;
private JButton stopBtn;
private Timer timer;
private int x = 0;

public void init()
{
timer = new Timer(1000, new TimerListener());
getContentPane().setBackground(Color.black);

//building the panels
buildTpanel();
buildBpanel();

//add panels to content pane
add(tPanel);
add(bPanel);

//setting the panel layout
setLayout(new GridLayout(2, 1));
}

private void buildTpanel()
{
//create the panel
tPanel = new JPanel();

//create Textfield that goes into the tPanel
tLabel = new JTextField("000000");

//add textfield to panel
tPanel.add(tLabel);
}

private void buildBpanel()
{
//create the panel
bPanel = new JPanel();

//create the buttons
startBtn = new JButton("Start");
stopBtn = new JButton("Stop");

//create the button listeners
startBtn.addActionListener(new StartListener());
stopBtn.addActionListener(new StopListener());

//add buttons to the panel
bPanel.add(startBtn);
bPanel.add(stopBtn);
}

//creating a listener for the start button

private class StartListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
timer.start();
}
}

//creating a listener for the stop button

private class StopListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
timer.stop();
}
}

//creating a timer listener
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
x++;
tLabel.setText("000000"+(x));
}
}
}

One of the difficulties for me was nailing down how to increment the counter once I hit the start button. The Timer method has only timer.start(); and timer.stop();. There is no timer.reset(); method. It is in poor taste to try and force the timer to set itself to all zeros because in clicking start it continues where you hit stop.

And so there you have the Java Timer Method used in an HTML applet.

posted under HTML, XHTML, CSS, AJAX, Java, Programming, School, Source Code | Comments Off

Facebook, styles for FireFox, and killing that annoying status spam

May30

Recently one of my Twitter “reads” mentioned using a plug in for FireFox called Stylish to kill all of the spam on the right hand side of FaceBook.

If you are a frequent user of FaceBook, you know that it’s pretty annoying to constantly see a list of whatever “applications” your friends are using. (I.E.: List 5 things you hate, blah blah blah)

What Stylish is, is an add-on for FireFox that essentially modifies a websites formatting, and lays over the top of what ever site you’re viewing. So if something particular annoys you with a site, Stylish will allow you to remove the offending piece of code, thus making the site more enjoyable for your tastes.

Using Stylish for FaceBook turned off the notifications but still left other spam like the suggestions window, and other notifications.

So this is the code that stylish created for facebook:

@namespace url(http://www.w3.org/1999/xhtml);

@-moz-document domain("facebook.com") {
.UIHotStream {display:none;}

}

and I added the following line to completely block/hide everything on that right side:

.UITitledBox {display:none;}

and so altogether you have:

@namespace url(http://www.w3.org/1999/xhtml);

@-moz-document domain("facebook.com") {
.UIHotStream {display:none;}
.UITitledBox {display:none;}
}

I hope that my tip was helpful if you are as tired of looking at that garbage as I am. Now I am going to see what other sites I can use Stylish for because there is certainly plenty of spam/garbage out there I don’t want to look at.

Using counters in Java

March10

I’m reading about using counters in Java today. It’s not unlike what I’ve already explored through JavaScript.

The differences are that in Javascript, you can write the counter inside a function, while in Java it has to be used inside a constructor method (nearly the same thing). The distinct difference is that in java, you have to declare the counter variable as an int so that during compiling, you won’t get any errors. Like so:

public class counter
{

     public static void main(String[] args)
     {

          int counter = 0;
          while (counter <=5)
          {
             System.out.println(counter);
             counter++;
          }

      }
}

So that is where I am at this morning on my spring break (officially day two). What are your experiences using counters in Java? Do you have any you like better than this simple while loop?

posted under Java, School, Source Code | Comments Off

What Arnis de Mano can be

March7

I say “can be” because I am no where near that level of skill or intensity.

I found Arnis de Mano through a community education class. I kid you not! I was looking for something additional to supplement my studies in other Martial Arts when I found Arnis.

For those of you who don’t know, Arnis de Mano is Phillipino stick and knife fighting. This style has a heavy Spanish influence even though you’ll find it predominant in the Phillipines.

I have to say that I am very happy to have found it, as I have met some very influential people, and the style itself has been better for my health and conditioning.

Besides participating in Arnis, I also study Okinawan Karate, and train in the P90X fitness system. My only regret is that I hadn’t started any of this sooner. But that’s a “fate” question then isn’t it?

posted under Blogging, General Writing, Java, Lifestyle, Source Code | Comments Off

Java Switch statements….nested, and they don’t like strings :(

March2

For my first taste of switch statements in Java, we were given the assignment of making a program that prompts a user for a 2 letter state abbreviation code (like MN for Minnesota as an example).

The input wasn’t going to care if the case was all up, down, mixed, or whatever, the background work would ignore it, than rewrite it.

Any way, the problem with switch statements is that they only like int, or char values. NO STRINGS ALLOWED!

And, on top of that, the directions called for you to nest the switch statements in order to validate their input and output the full name of the state.

Read the rest of this entry »

posted under Blogging, Java, Programming, School, Source Code | Comments Off