Pages

Java Asterisk Pattern using Loops - Pattern Two

This is another simple application in Java that uses for loops in creating asterisk patterns.


public class PatternTwo {

    public static void main(String[] args) {

        for (int j = 5; j > 0; j--) {

   System.out.println("");

            for (int i = 5; i >= j; i--) {
                System.out.print(" ");
  
            }
for (int q = 0; q < j; q++) {
                System.out.print("*");
            }
        }
        System.out.println("");
    }
}


This is the sample output of the code above.

How to write and read files in Java?

Question

How can I write to and read from files in Java?

Answer 

Java applications have access to local files. It means that it can read and write to files. Let's take a look at two simple applications that write lines of text and read it back.

Application one:

import java.io.*;

public class WriteFile
{
 
 public static void main (String args[]) //entry point
 {
  // FileOutputStream object
  FileOutputStream fout;  

  try
  {
      // Open an output stream
      fout = new FileOutputStream ("myfile.txt");

      // Print a line of text
      new PrintStream(fout).println ("This is simple application to write lines of text. Pretty Cool!!");

      // Close our output stream
      fout.close();  
  }
  // Catches any error conditions
  catch (IOException e)
  {
   System.err.println ("There were some errors writing lines.");
   System.exit(-1);
  }
 } 
}

The above code creates a FileOutputStream and writes a line of text to a file called myfile.txt. We must enclose the writing of file with try{...}catch block to trap or catch any exception that may occur.

Reading files in Java is easy as well. The code below creates FileInputStream to read file and display it to the user.

Application Two:

import java.io.*;

public class ReadFile
{
 // Main method
 public static void main (String args[])
 {
  // FileInputStream to read file
  FileInputStream fin;  

  try
  {
      // Open an input stream
      fin = new FileInputStream ("myfile.txt");

      // Read a line of text
      System.out.println( new DataInputStream(fin).readLine() );

      // Close our input stream
      fin.close();  
  }
  // Catches any error conditions
  catch (IOException e)
  {
   System.err.println ("Unable to read lines from file");
   System.exit(-1);
  }
 } 
}


Java Asterisk Pattern using Loops - Pattern One

        To understand better about loops, asterisk patterns will be a very big help for aspiring programmers. There are lots of patterns to form using loops in Java (while, do while, for loops). One can even draw shapes, text, figures and others. Today, I will show you some of the asterisk patterns in Java.

       
public class PatterOne
{

 public static void main(String[]args){  
   for (int j = 7; j > 0; j--){
                System.out.println("");
   for (int i = 5; i >= j; i--){
                System.out.print("*");
            }
            }
  System.out.println("");

 }

}

Here is the sample output of the code above:



Implementing the QuaQua LookAndFeel in Java

      I like to show you how to implement the QuaQua Look And Feel in Java. I heard someone says that Java isn't that good when it comes to look and feel and Java is only good for algorithms, back ends and all other good stuffs. I found that statement partially true. Why? Because Java is really good for algorithms. But I must say that Java is as good as other programming language when talking about LAF. There are lots of workaround of customizing the LAF of your Java Desktop Application.

You can create an application what uses Windows LAF, Metal and other available LAF for Java. You can also create a desktop application that uses other LAF like the QuaQua Look and Feel described here.

Examples of the implementation of the QuaQua Look and Feel in Java.

quaqua look and feel in java

quaqua look and feel in java

quaqua look and feel in java


Wow! That is very nice and very professional looking for a Java application.
So, how to do that?

Download the  QuaQua package from the QuaQua Look And Feel website.
Add add quaquar.jar and swing-layout.jar to the classpath of your project.

 SwingUtilities.invokeLater(
new Runnable() {

public void run() {
try {
UIManager.setLookAndFeel("ch.randelshofer.quaqua.QuaquaLookAndFeel");
} catch (Exception e) {
}
MainFrame MF = new MainFrame();
MF.setSize(647, 410);
MF.setLocationRelativeTo(null);
MF.setVisible(true);
MF.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
MF.getRootPane().setFont(UIManager.getFont("SystemFont"));
MF.getRootPane().putClientProperty("Quaqua.RootPane.isVertical", Boolean.FALSE);
MF.getRootPane().putClientProperty("Quaqua.RootPane.isPalette", Boolean.FALSE);
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);

Just use the code above to implement QuaQua Look And Feel in Java.

Credits:
Joel Benaldo
Jakemar Bantugan