Mar 21

We often get students struggling to read and validate console input from the user. The following example gives a simple example of an approach that can be used.

It reads the input from the user and checks if it is a valid double. If its not it prompts the user again. Could easily be modified for different validation that you may require.

  Scanner input = new Scanner( System.in );
  Double value = null;
  while (value==null) {
	  System.out.print("Please enter number: ");
	  String s = input.nextLine();
	  if (s.length()==0) {
		  System.out.println("You did not enter a value; Try again");
	  } else {
		  try {
			  value = new Double(s);
		  } catch (Exception ex) {
			  System.out.println("You did not enter a valid value; Try again");
		  }
	  }
  }
  double d = value.doubleValue();

written by objects \\ tags: , , , ,

Mar 12

When using non standard fonts in a PDF we need to embed the font so it is available to whoever is reading that PDF (as they probably will not have the font installed on their system).

iText provides a great library for generating PDF’s using Java. It also includes the ability to embed fonts.

If the font is not installed on your system and you instead have a font file that you want to embed then you need to first register the font with iText FontFactory.

Then get the font from the FontFactory making sure to tell it that you want the font embedded.


import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class EmbedFont {
  public static void main(String[] args) throws Exception {

      // Pass name of font file on command line

      String filename = args[1];

      // Register the font

      FontFactory.register(filename, filename);

      // Get the font NB. last parameter indicates font needs to be embedded

      Font font = FontFactory.getFont(filename, BaseFont.CP1252, BaseFont.EMBEDDED);

      // Lets now use the font to create a PDF

      Document document = new Document();
      PdfWriter.getInstance(document,  new FileOutputStream(filename+".pdf"));
      document.open();
      Paragraph p = new Paragraph("This uses an embedded font", font);
      document.add(p);
      document.close();
  }
}

written by objects \\ tags: , , , , ,

Mar 11

Here is a quick reference on setting the opacity of html elements using CSS, Javascript and JQuery.

.someClass {
   opacity: 0.6;
   
   /* For IE8 */
   -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=60)"; 
   /* For IE6 - 8 NB. Element needs to have layout */
   filter: alpha(opacity=60);
}

To change the CSS Opacity with Javascript

 // for most browsers
document.getElementById("myElement").style.opacity = ".4";
// for IE
document.getElementById("myElement").style.filter = "alpha(opacity=40)"; 

To change the CSS Opacity with JQuery

$(".someClass").css({ opacity: .4 });

// or we can animate the change

$("#myElement").animate({
	opacity: .4
	}, 1000, function() {
});

written by objects \\ tags: ,