Show JPanel on another JFrame

Current Status: I have a JPanel object that contains complex components (a 3D canvas written by me).

Problem: Now there are two screen devices. I want to use one for management, another for display, like PowerPoint. How can I effectively display this JPanel on another screen (a static view is enough, but I want it to reflect the change on the control screen?

What I tried: I tried to draw a static picture in another JPanel every 200 ms.

            Graphics2D g2 = (Graphics2D) contentPanel.getGraphics();
            g2.translate(x, y);
            g2.scale(scale, scale); 
            displayPanel.paintAll(g2);

Notes: contentPanel is in screen JFrame, displayerPanel is the panel I want to copy

But I have a problem with the screen flickering so seriously that I can’t take it ... Is this a problem with my processor or video card? Or is this an efficient method that I can use? Please help, thanks a lot!

And here is MCVE (sorry, this is my first question asking a question on stackoverflow, but I'm touched by your tips! Thanks again!)

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

public class DisplayWindow {

private static JFrame frame = new JFrame();
private static JPanel contentPanel = new JPanel();
private static JPanel displayPanel = null;
private static Timer timerDisplay = null;


static {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(50, 50, 1366, 768);
    frame.getContentPane().add(contentPanel);
    frame.setVisible(true);
    if (timerDisplay == null) {
        timerDisplay = new java.util.Timer();
        timerDisplay.schedule(new DisplayAtFixedRate(), 1000, 250);
    }
}

public static void display(JPanel panel) {
    displayPanel = panel;
}

private static class DisplayAtFixedRate extends TimerTask {
    @Override
    public void run() {
        if (displayPanel != null && displayPanel.getWidth() != 0) {
            double windowWidth = frame.getWidth();
            double windowHeight = frame.getHeight();
            double panelWidth = displayPanel.getWidth();
            double panelHeight = displayPanel.getHeight();
            double scale = Math.min(windowWidth / panelWidth, windowHeight / panelHeight);
            int x = (int) (windowWidth - panelWidth * scale) / 2;
            int y = (int) (windowHeight - panelHeight * scale) / 2;

            Graphics2D g2 = (Graphics2D) contentPanel.getGraphics();
            g2.translate(x, y);
            g2.scale(scale, scale);
            displayPanel.paintAll(g2);
        }
    }
}

public static void main(String[] args) {
    JFrame controlFrame = new JFrame();
    controlFrame.setBounds(50, 50, 1366, 768);
    JPanel controlPanel = new JPanel();
    controlFrame.getContentPane().add(controlPanel, BorderLayout.CENTER);
    controlPanel.add(new JLabel("Hello Stackoverflow!"));
    controlFrame.setVisible(true);
    DisplayWindow.display(controlPanel);
}
}
+4
source share
2 answers

Here is an example:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.WindowConstants;

public class PaintImage {

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

            @Override
            public void run() {
                startUI();
            }
        });
    }

    private static void startUI() {
        final JFrame mainFrm = new JFrame("Main");
        final JFrame paintFrame = new JFrame("Copy");
        mainFrm.add(new JScrollPane(new JTree()), BorderLayout.WEST);
        mainFrm.add(new JScrollPane(new JTextArea("Write your text here...")), BorderLayout.CENTER);
        mainFrm.pack();
        mainFrm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        mainFrm.setLocationRelativeTo(null);

        final JLabel label = new JLabel("");
        paintFrame.add(label);
        paintFrame.setSize(mainFrm.getSize());
        paintFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        mainFrm.setVisible(true);
        paintFrame.setVisible(true);

        final Timer t = new Timer(200, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                final Image img = getScreenShot(mainFrm.getContentPane());
                label.setIcon(new ImageIcon(img));
            }
        });
        t.start();
    }

    public static BufferedImage getScreenShot(Component component) {

        final BufferedImage image = new BufferedImage(
                component.getWidth(),
                component.getHeight(),
                BufferedImage.TYPE_INT_RGB
                );
        // call the Component paint method, using
        // the Graphics object of the image.
        component.paint( image.getGraphics() ); // alternately use .printAll(..)
        return image;
    }
}

Method origin getScreenShot here

+2
source

Painting can be done by passing a displayPanel for drawing.

class DuplicatePanel extends JPanel {
    private final JPanel displayPanel;

    DuplicatePanel(JPanel displayPanel) {
        this.displayPanel = displayPanel;
    }

    @Override
    public void paintComponent(Graphics g) {
        displayPanel.paintComponent(g);
    }
}

repaints, repaint(50L), SwingTimer .

0

Source: https://habr.com/ru/post/1652000/


All Articles