java jpanel 分层显示,java – 在另一个JFrame上显示JPanel

当前状态:

我有一个JPanel对象,其中包含复杂的组件(我自己编写的3D画布).

问题:

现在有两个屏幕设备.我想用一个用于控制,另一个用于显示,就像PowerPoint一样.如何有效地在另一个屏幕上显示此JPanel(静态视图就足够了,但我希望它反映控制屏幕上的更改?

我尝试过的:

我试图每隔200ms将静态图片绘制到另一个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值