|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
|
|
#1
|
|||
|
|||
|
Simple JAVA question from a beginner
Hi there, i wonder if anyone could help me understand this simple applet.
I am just a beginner programmer in Java (although experienced in other non OOP languages) and still having difficulty with objects etc. In the applet below i dont really understand whats happening in line 6. Font header = new Font("TimesRoman",3,24); I know that Font is a class and that the three arguments of Fonts are its three properties but its the word 'header' that confuses me. Is this line creating an object, called header, of the Font class and therefore takes on its three properties? Is header a referenced variable? If you could explain exactly what it is i would be very grateful. and lastly could you explain what the g is on line 10 public void paint(Graphics g) is it an object? and what does it have to do in the following lines? g.setFont(header); g.drawString("Times Roman, Bold & Italic, 24 Point",0,30); thanks very much in advance, this would be a great help. -------------------------------------------------- import java.awt.*; import java.applet.*; public class Fonts extends Applet { Font header = new Font("TimesRoman",3,24); Font subhead = new Font("Helvetica",Font.BOLD,218); Font body = new Font("Courier",0,14); public void paint(Graphics g) { g.setFont(header); g.drawString("Times Roman, Bold & Italic, 24 Point",0,30); g.setFont(subhead); g.drawString("Helvetica, Bold, 18 Point",0,60); g.setFont(body); g.drawString("Courier, 14 Point",0,90); } } |
|
#2
|
|||
|
|||
|
let me try to explain (i am not a java pro but i am quite good at oop)
Code:
Font header = new Font("TimesRoman",3,24);
create a new variable called "header" of type "Font" (the first Font in the line) and then make it an instance of class Font with calling the constructor of class Font that takes three parameters (one string, two numbers). after passing this this line, "header" is an object, an instance of class "Font". thus you can eg. call methods: Code:
header.setFontName("Arial");
int myFontSize=header.getFontSize();
(those names are inventions by me as i am not in the mood to look up the Font class in the reference right now...) for your second question: Code:
public void paint(Graphics g) this is the code to define a function (aka method if itīs inside a class). itīs name is "paint", it has no return parameter and it takes one parameter called "g" which should be an instance of class "Graphics". this function is called by the virtual machine to make the program paint itself. the virtual machine passes one parameter of type "Graphics" that is the main graphical context of your applet. there you can paint on and the virtual machine will make sure that what you paint on it will appear on the screen. if you still have questions, go ahead ![]()
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. Last edited by M.Hirsch : November 11th, 2002 at 12:41 PM. |
|
#3
|
|||
|
|||
|
Thanks
Thanks M.Hirsch ,
that has really cleared a few things up, i have been working through a beginners Java course and that part wasnt explained properly. Cheers, Hargos |
![]() |
| Viewing: Dev Shed Forums > Other > Beginner Programming > Simple JAVA question from a beginner |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|