Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using two screens in Win XP 2

Status
Not open for further replies.

allanhn

Instructor
Feb 4, 2002
1
DK
I'm running Windows XP with two screens attached.

I need to show two different images - one on each screen.

How do I tell my Java application to show an image on a specific screen??

- Allan Helboe Nielsen
 
The following code is taken from the book "Core JFC" by Kim Topley. It prints out information about your monitors and puts a frame with an image on all of the monitors. The monitors don't need to display the same nr of colors or to be set to the same resolution :

package com.ecc.swing2;

import java.awt.*;
import javax.swing.*;

public class MultimonitorExample {

public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();
System.out.println("There are " + gd.length + " screen device(s)");
ImageIcon image = new ImageIcon("D:/images/Belgium.gif");
for (int i = 0; i < gd.length; i++) {
GraphicsConfiguration[] gcs = gd.getConfigurations();
System.out.println(&quot;Device &quot; + i + &quot; has &quot; + gcs.length + &quot; configuration(s)&quot;);
for (int j=0; j<gcs.length; j++) {
System.out.println(&quot;\tConfig &quot; + j + &quot; bounds: &quot; + gcs[j].getBounds());
}
JFrame f = new JFrame(&quot;Screen &quot; + i);
JLabel l = new JLabel(image);
f.getContentPane().add(l);
f.pack();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle pt = gc.getBounds();
f.setLocation(pt.x + 100, pt.y +100);
f.setVisible(true);
}
}

}
 
Correction : The code put between &quot;code&quot; and &quot;/code&quot;

Code:
package com.ecc.swing2;

import java.awt.*;
import javax.swing.*;

public class MultimonitorExample {

  public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gd = ge.getScreenDevices();
    System.out.println(&quot;There are &quot; + gd.length + &quot; screen device(s)&quot;);
    ImageIcon image = new ImageIcon(&quot;D:/images/Belgium.gif&quot;);
    for (int i = 0; i < gd.length; i++) {
      GraphicsConfiguration[] gcs = gd[i].getConfigurations();
      System.out.println(&quot;Device &quot; + i + &quot; has &quot; + gcs.length + &quot; configuration(s)&quot;);
      for (int j=0; j<gcs.length; j++) {
        System.out.println(&quot;\tConfig &quot; + j + &quot; bounds: &quot; + gcs[j].getBounds());
      }
    JFrame f = new JFrame(&quot;Screen &quot; + i);
    JLabel l = new JLabel(image);
    f.getContentPane().add(l);
    f.pack();
    GraphicsConfiguration gc = gd[i].getDefaultConfiguration();
    Rectangle pt = gc.getBounds();
    f.setLocation(pt.x + 100, pt.y +100);
    f.setVisible(true);
    }
  }

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top