I have been wondering how I would be able to move from one frame to another using a button. At the moment, the way I have been doing it is by hiding this frame, the frame that is currently showing by this.setVisible(false), and creating a new frame by using new BuyRatesFrame().setVisible(true);
Although this technically works, it is not the best method that should be used when traversing between panels.
While googling on more efficient methods, I stumbled upon Card Layout.
I will update more after finishing a game that I'm playing. :D
Friday, February 10, 2012
Thursday, February 9, 2012
Android
It has been ages since I last posted. I will try to post everyday and learn regard in by Java but it is quite hard. For now I will just post something I learned regarding android. The baseman version of an android is what will be used to connect the phone with the provider. It is used for communication and even if the version number of the baseband is different than the kernel, it will still work
Saturday, February 4, 2012
Printing v2
Printing in Java as we learned earlier requires the Printable implementation. One line of code caught my eye that I believe I need to fully understand.
Graphics g;
Graphics2D g2d = (Graphics2D) g;
As my java is a bit rusty I fully intend to learn this. From what I have garnered so far, it is just a simple casting from the Graphics object to a Graphics2D object. Hmm. Graphics apparently is also abstract. I just looked it up on an API.
This kinda throws everything off for me a bit. I had assumed that since Graphics g was a command it would not be abstract.
However I now realize, it is created but not initialized so it still would work, since we only declared it but not initialzied it.
Graphics g = new Graphics() would obviously fail, now I understand that.
Then how could the line
g2d.translate(tx,ty); be called??
... Okay I feel silly. Apparently I mistook interfaces with Abstract Classes. Interfaces are abstract classes in which all the methods are abstract. With an abstract class, you can make it such that only some of the methods are abstract and some aren't.
Printing in Java
Printing in Java is something I have not done previously. In order to print using Java, I would have to implement the Printable and ActionListener class.
The code and examples for printing in java can be found in the Oracle website.
Printing has to be done using the Graphics class
Steps to printing in Java
There are basically two tasks that must be done for printing
1) The first task is for creating the jobs that must be printed. What items needs to be printed and the number of copies and associations with a printer
2) Drawing the content of the page must be done as the second step in order to print
1) Importation of a Print class must be done. PrintJob is what is going to be used to print the items In Java
import java.awt.print.*;
That must be done in order for the print functions to work
PrinterJob pj = PrinterJob.getPrinterJob();
Now the statement above will return a PrinterJob that is associated with the default printer. As it is an abstract class, PrinterJob will not be able to initialized. Hence, the statement above is a way to get PrinterJob object.
The printable class is an interface that is implemented by the print methods.
It will be used by using Printable.print(..)
TIP: Ensure that Graphics g and PageFormat pf has the correct syntax and capitalization. As Pageformat is obviously different than PageFormat but its easy to miss sometimes.
When using the Printable, the PageFormat object must be used as well. This class is used for the page orientation, size, imaginable units.
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
/* We want to only print the first page so anything above zero
* is not required
*/
if (pageIndex > 0 ) {
return NO_SUCH_PAGE;
}
/* We have to translate the X and Y values in the PageFormat
* to avoid clipping
*/
Graphics2D g2d = (Graphics2D)graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
graphics.drawString("Hello World!", 100, 100);
return PAGE_EXISTS;
}
Notice that
Graphics2D g2d = (Graphics2D) graphics;
where Graphcis2D g2d is an abstract class.
The code and examples for printing in java can be found in the Oracle website.
Printing has to be done using the Graphics class
Steps to printing in Java
There are basically two tasks that must be done for printing
1) The first task is for creating the jobs that must be printed. What items needs to be printed and the number of copies and associations with a printer
2) Drawing the content of the page must be done as the second step in order to print
1) Importation of a Print class must be done. PrintJob is what is going to be used to print the items In Java
import java.awt.print.*;
That must be done in order for the print functions to work
PrinterJob pj = PrinterJob.getPrinterJob();
Now the statement above will return a PrinterJob that is associated with the default printer. As it is an abstract class, PrinterJob will not be able to initialized. Hence, the statement above is a way to get PrinterJob object.
The printable class is an interface that is implemented by the print methods.
It will be used by using Printable.print(..)
TIP: Ensure that Graphics g and PageFormat pf has the correct syntax and capitalization. As Pageformat is obviously different than PageFormat but its easy to miss sometimes.
When using the Printable, the PageFormat object must be used as well. This class is used for the page orientation, size, imaginable units.
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
/* We want to only print the first page so anything above zero
* is not required
*/
if (pageIndex > 0 ) {
return NO_SUCH_PAGE;
}
/* We have to translate the X and Y values in the PageFormat
* to avoid clipping
*/
Graphics2D g2d = (Graphics2D)graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
graphics.drawString("Hello World!", 100, 100);
return PAGE_EXISTS;
}
Notice that
Graphics2D g2d = (Graphics2D) graphics;
where Graphcis2D g2d is an abstract class.
Thursday, February 2, 2012
CIV v2 Databases
In order for the program to be able to save past history, I need to make a database.
The database will be quite simple. It needs to just store the amount of money that is still currently available.
So the columns of the database will be as follows
Date
User
twodollars
oneDollar
fiftyCents
twentyCents
tenCents
fiveCents
-- a Date is needed because we want to know what time the money was taken out of the system
-- the user is required because we want to know who is taking the money
Also we need to make sure an ID is available.
The SQL Command that I used is
create table civtable (
id integer primary key generated always as identity,
username varchar(30),
twodollars int,
onedollar int,
fiftycents int,
twentycents int,
tencents int,
fivecents int
);
The database will be quite simple. It needs to just store the amount of money that is still currently available.
So the columns of the database will be as follows
Date
User
twodollars
oneDollar
fiftyCents
twentyCents
tenCents
fiveCents
-- a Date is needed because we want to know what time the money was taken out of the system
-- the user is required because we want to know who is taking the money
Also we need to make sure an ID is available.
The SQL Command that I used is
create table civtable (
id integer primary key generated always as identity,
username varchar(30),
twodollars int,
onedollar int,
fiftycents int,
twentycents int,
tencents int,
fivecents int
);
CIV
I am trying to start a new project. This project is named the CIV project. Basically, all this project does is maintain a database and a list of coins. It is a small version of a book keeping diary. Perhaps a picture is worth a thousand words so the following is the CIV program that I have started to make.
As can be seen form the image above, the software will add the number of coins that is taken. So basically, for example, if I have with me 50 dollar worth of $2 coins, I can click the $2 button and take it out. I haven't implemented the buttons to take out the coins yet. At the moment all I have done is create the GUI.
-- To update all the fields, I have decided that instead of updating one by one when I click the button, I create another method where it will update every field. I have stored values at the moment using integers. For example:
int stackTwoDollars =0;
int stackOneDollar =0;
For now, I will be happy with starting with zero just to make sure it works. Later on I plan to connect it to a database so that the values from the previous time that money was taken out can be saved.
private void updateFields() {
twoDollarsField.setText(""+stackTwoDollars);
oneDollarField.setText(""+stackOneDollar);
fiftyCentsField.setText(""+stackFiftyCents);
twentyCentsField.setText(""+stackTwentyCents);
tenCentsField.setText(""+stackTenCents);
fiveCentsField.setText(""+stackFiveCents);
}
The code above is used to update and refresh all the fields;
Annotations
Annotations are important nowadays. When I started learning Java, annotations have not been implemented yet. Nowadays, annotations are everywhere and it will be silly if I did not at least learn about it.
Annotations ( from the Java Tutorials)
- It is used as information for the compiler - can be used to detect errors or suppress warnings
- Can be sued to generate code, xml files
- runtime processing annotations are available to be examined at runtime.
Java Annotations can be used with Java Persistence. However, I am still unsure how to do this.
Annotations ( from the Java Tutorials)
- It is used as information for the compiler - can be used to detect errors or suppress warnings
- Can be sued to generate code, xml files
- runtime processing annotations are available to be examined at runtime.
Java Annotations can be used with Java Persistence. However, I am still unsure how to do this.
Subscribe to:
Posts (Atom)
