Thursday, October 30, 2008

Displaying Java code fragments

I noticed that a few people have tried various things to display code fragments in a nice way within the blog. This does seem to be a bit of a problem. I have found the following solution which could perhaps be refined somewhat, but in the meantime would at least provide an acceptable solution.

Java2Html seems to be a nifty little Java program that converts Java code to HTML format with indentation and syntax highlighting. It provides a few options and it seems is also available as an Eclipse plug-in. I have not tried the plug-in. You can download it as a jar, which I believe should run on any operating system that has a JRE available.

This is what I managed to get using the program:



01 int k;
02 int xpos1 = 100
03 int xpos2 = 118
04 int count = 0
05 int timey = 0;
06 int num = 12;
07 
08 size(200200);
09 background(102);
10 noStroke();
11  
12 // Draw gray bars 
13 fill(255);
14 k=60;
15 for(int i=0; i < num/3; i++) {
16   rect(25, k, 1555);
17   k+=10;
18 }
19 
20 // Black bars
21 fill(51);
22 k = 40;
23 for(int i=0; i < num; i++) {
24   rect(105, k, 305);
25   k += 10;
26 }
27 k = 15;
28 for(int i = 0; i < num; i++) {
29   rect(125, k, 305);
30   k +=10;
31 }
32   
33 // Thin lines
34 k = 42;
35 fill(0);
36 for(int i=0; i < num-1; i++) {
37   rect(36, k, 201);
38   k+=10;
39 }





Step by step, here is what I have done:
  1. Download Java2Html from http://www.java2html.de/download.html
  2. Extract into a directory of your choice.
  3. Create a batch file in the same directory to run the program. All I put in my batch file, which I called run.bat, was: javaw -jar java2html.jar
  4. You can now double click the batch file in Explorer or create a short-cut on your desktop. For operating systems other than Windows you'll have to do what is required for that operating system to get the same functionality.
  5. Java2Html allows you to either convert a file or direct text. Select the tab you require and then either open the file or type (cut & paste) your text in the text area.
  6. I selected Line numbers and I set Tab-space to 8.
  7. Conversion is done immediately. If you opened a file, the conversion is available on the clip-board. If you did a direct conversion, the text is available in the "Converted Source" text area.
  8. I stripped off all the headers and code up to the <code> tag and after the </code> tag.
  9. I also removed all the <br /> tags at the end of each line. For this I used vi (my favourite editor). The command for stripping the tags is: :%s/<br />$//g
  10. The normal colour for code is black. My blogger theme did not display this well. To change the black text to white, I used vi again to do a global replacement. The command for this was: :%s/color="#000000"/color="#000000"/g
  11. Now cut and paste what is left over into Blogger using Edit Html
  12. Important: You have to paste this code while in Edit Html and not Compose, or else your HTML code will be displayed rather than the formatted HTML.
I hope this makes sense. Just leave comments if anything seems wrong or not clear and I'll update the instructions.

If I have time I might have a look at the source for Java2Html and either extend or modify it to create Blogger-ready code.

I hope this is of help to someone.

Tuesday, October 28, 2008

Why am I doing this?

Some of the reasons why I'm getting involved in this project:
  1. Feeling quite isolated as a correspondence student, I thought this might be a golden opportunity to get involved in an interesting project.
  2. I have also been looking for opportunities to get more experience in academic writing.
  3. I love programming.
  4. I am all for community involvement in projects. It provides opportunities for those who would like to contribute.
  5. An opportunity to learn more about something I didn't know before, ie the Processing IDE and Computational Art.

Chunk 83

So I think I'm down for Chunk 83. Now how did that happen again?

I think it was mainly because the chunks are described in reverse order in the Book Fragments Blog. As I started reading the blog my eye caught something about "buttons" and I wondered whether I would be able to do the button thingy in Processing. So when that turned out not to be rocket science I thought I should be able to cope with that section.

I don't know what the difference would be between a button, an advanced button 1 and an advanced button 2. I guess I'll have to wait for the book to discover that.

Monday, October 27, 2008

Introduction

I'm terrible at keeping my blogs active ... I've started a few but after a couple of posts I usually forget about the thing for months on end. I always blame it all on my kids. So I guess I have another eight years or so of excuses and then I'll have to find something or someone else to blame when I forget or don't get around to doing things!

Maybe this blog, being part of the mass writing project will be more structured and force me to update it more frequently.

I'm currently working on my research project and dissertation (M801) with the Open University and that is how I found myself on the mailing list requesting volunteers for the exciting but whacky project!

When not busy with the above, I work full-time and I'm a single mom. I do some other stuff but I can't remember what they are right now ...