gpt4 book ai didi

java - 在另一个 JFrame 上显示 JPanel

转载 作者:搜寻专家 更新时间:2023-11-01 03:18:32 25 4
gpt4 key购买 nike

当前状态: 我有一个包含复杂组件(我自己编写的 3D Canvas )的 JPanel 对象。

问题: 现在有两个屏幕设备。我想用一个来控制,另一个只用来显示,就像 PowerPoint 一样。如何在另一个屏幕上有效地显示此 JPanel(静态 View 就足够了,但我希望它反射(reflect)控制屏幕上的变化?

我尝试过的: 我尝试每 200 毫秒将静态图片绘制到另一个 JPanel。

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

注意:contentPanel在显示屏的一个JFrame中,displayerPanel是我要复制的面板

但是我遇到一个问题,显示屏幕闪烁严重到让我无法接受...是我CPU的问题还是显卡的问题?或者这些是我可以使用的任何有效方法吗?请帮忙,非常感谢!

这是 MCVE(抱歉,这是我第一次在 stackoverflow 中提问,但你的帮助让我很感动!再次感谢!)

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);
}
}

最佳答案

给你举个例子:

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's paint method, using
// the Graphics object of the image.
component.paint( image.getGraphics() ); // alternately use .printAll(..)
return image;
}
}

getScreenShot 方法的来源是 here

关于java - 在另一个 JFrame 上显示 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39072384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com