Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dual screen application

I have to run an application in dual screen mode. How can I run the application on both screen as independent window but sharing the same application model?

like image 405
jerome Avatar asked Nov 15 '12 11:11

jerome


2 Answers

If i am not wrong,this eg may help you. 1st position your frames on each screen devices.

frame1.setLocation(pointOnFirstScreen);
frame2.setLocation(pointOnSecondScreen);

to maximize:

frame.setExtendedState(Frame.MAXIMIZED_BOTH);

working example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class GuiApp1 {
protected void twoscreen() {
    Point p1 = null;
    Point p2 = null;
    for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment ().getScreenDevices()) {
        if (p1 == null) {
            p1 = gd.getDefaultConfiguration().getBounds().getLocation();
        } else if (p2 == null) {
            p2 = gd.getDefaultConfiguration().getBounds().getLocation();
        }
    }
    if (p2 == null) {
        p2 = p1;
    }
    createFrameAtLocation(p1);
    createFrameAtLocation(p2);
 }

 private void createFrameAtLocation(Point p) {
    final JFrame frame = new JFrame();
    frame.setTitle("Test frame on two screens");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    final JTextArea textareaA = new JTextArea(24, 80);
    textareaA.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
    panel.add(textareaA, BorderLayout.CENTER);
    frame.setLocation(p);
    frame.add(panel);
    frame.pack();
    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    frame.setVisible(true);
 }

 public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {


        public void run() {
            new GuiApp1().twoscreen();
        }
    });
 }

}
like image 144
joey rohan Avatar answered Nov 08 '22 08:11

joey rohan


You need to take a look at the GraphicsDevice API, you can find a great example there.

Sourced from Oracle:

In a multi-screen environment, the GraphicsConfiguration objects can be used to render components on multiple screens. The following code sample demonstrates how to create a JFrame object for each GraphicsConfiguration on each screen device in the GraphicsEnvironment:

GraphicsEnvironment ge = GraphicsEnvironment.
   getLocalGraphicsEnvironment();
   GraphicsDevice[] gs = ge.getScreenDevices();
   for (int j = 0; j < gs.length; j++) { 
      GraphicsDevice gd = gs[j];
      GraphicsConfiguration[] gc =
        gd.getConfigurations();
      for (int i=0; i < gc.length; i++) {
         JFrame f = new
         JFrame(gs[j].getDefaultConfiguration());
         Canvas c = new Canvas(gc[i]); 
         Rectangle gcBounds = gc[i].getBounds();
         int xoffs = gcBounds.x;
         int yoffs = gcBounds.y;
           f.getContentPane().add(c);
           f.setLocation((i*50)+xoffs, (i*60)+yoffs);
         f.show();
      }
}
like image 23
dsgriffin Avatar answered Nov 08 '22 08:11

dsgriffin