How to get the current desktop width
Hi, this is my first post, im new to Java and am looking for a little help.
Im trying to get the current width of the desktop, heres what I came up with after reading the page at the following link.
http://cgi3.cc.gatech.edu/classes/AY2001/cs4451a_fall/polardoc/com/hermetica/magician/GLComponent.html
import java.awt.*;
publicclass Untitled
{
publicstaticvoid main(String args[])
{
int desktopWidth = getDesktopWidth();
System.out.println(desktopWidth);
}
}
Can anyone tell me what im doing wrong, why wont the above code work ?
[950 byte] By [
Novaa] at [2007-10-2 4:05:09]

> Hi, this is my first post, im new to Java and am
> looking for a little help.
> Im trying to get the current width of the desktop,
> heres what I came up with after reading the page at
> the following link.
>
> http://cgi3.cc.gatech.edu/classes/AY2001/cs4451a_fall/
> polardoc/com/hermetica/magician/GLComponent.html
>
> > import java.awt.*;
>
> public class Untitled
> {
>public static void main(String args[])
>{
>int desktopWidth = getDesktopWidth();
>System.out.println(desktopWidth);
>
>}
> }
>
>
> Can anyone tell me what im doing wrong, why wont the
> above code work ?
you're invoking a method (getDesktopWidth()) as if it belonged to the Untitled class. But there is no such method. Define it and implement it
this will help you :
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = defaultToolkit.getScreenSize();
use and abuse this : [url=http://java.sun.com/j2se/1.5.0/docs/api]API JavaDocs[/url]
Thanks for your reply but that seems to returns the string java.awt.Dimension[width=1280,height=1024]
import java.awt.*;
public class Untitled
{
public static void main(String args[])
{
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = defaultToolkit.getScreenSize();
System.out.println(screenSize);
}
}
Is there a way of just returning the width as an int and the height as an int something like
int screenSizeX = defaultToolkit.getScreenWidth();
int screenSizeY = defaultToolkit.getScreenHeight();
System.out.println(screenSize);
Novaa at 2007-7-15 23:27:53 >

use and abuse this : [url= http://java.sun.com/j2se/1.5.0/docs/api]API JavaDocs[/url]look up Dimension
mlka at 2007-7-15 23:27:53 >

Look at the API for Dimension http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Dimension.htmlYour screenSize object is of type Dimension, so you can ask IT for the width and height.
Of course, its painfull obviously now
double Height = screenSize.getHeight();
double Width = screenSize.getWidth();
int desktopHeight = (int)Height;
int desktopWidth = (int)Width;
Thanx a million for all your help.
Novaa at 2007-7-15 23:27:53 >

> Of course, its painfull obviously now
>
> > double Height = screenSize.getHeight();
> double Width = screenSize.getWidth();
> int desktopHeight = (int)Height;
> int desktopWidth = (int)Width;
>
>
> Thanx a million for all your help.
you can access width and height member attributes directly (they're int so no cast needed then)