作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个类:public class GraphEditorPane extends JGraph
我正在使用这个“GraphEditorPane”,如下所示:
public JScrollPane getGraphPaneScrollPane() {
if (graphPaneScrollPane == null) {
graphPaneScrollPane = new JScrollPane();
//graphPaneScrollPane.setViewportView(getGraphEditorPane());
graphPaneScrollPane.setViewportView(getGraphEditorPane());
}
return graphPaneScrollPane;
}
public GraphEditorPane getGraphEditorPane() {
graphEditorPane = new GraphEditorPane(); }
我正在使用这个“GraphEditorPane”在其上绘制图表。现在我的问题是 - 有什么方法可以将此 JGraph 转换为玻璃 Pane ,以便我的“GraphEditorPane”是透明的并且我仍然可以在它上面绘图?
最佳答案
我认为你把事情过于复杂化了。
玻璃板将是最上面的组件(当可见时),绘制在其他所有组件的顶部。如果您只是想将一个组件“覆盖”在另一个组件上,那么有更简单的解决方案......
我能想到的最简单的想法是使用JLayeredPane
,将其设置为使用GridBagLayout
,这样您就不必担心定位子项成分。这将为您提供快速、简单的方法来更改组件的顺序。
另一种解决方案是简单地将覆盖组件直接添加到底层组件之上。
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Overlay {
public static void main(String[] args) {
new Overlay();
}
public Overlay() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
ImagePane imagePane = new ImagePane();
imagePane.setLayout(new BorderLayout());
imagePane.add(new OverlayPane());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(imagePane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class OverlayPane extends JPanel {
public OverlayPane() {
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
g2d.setStroke(new BasicStroke(4));
int radius = 40;
g2d.drawOval(326 - radius / 2, 351 - radius / 2, radius, radius);
g2d.drawOval(416 - radius / 2, 351 - radius / 2, radius, radius);
int size = 20;
g2d.drawLine(374, 400, 374 - size, 400 + size);
g2d.drawLine(374, 400, 374 + size, 400 + size);
g2d.dispose();
}
}
public class ImagePane extends JPanel {
private BufferedImage buffer;
public ImagePane() {
try {
buffer = ImageIO.read(new File("/path/to/your/image"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return buffer == null ? new Dimension(200, 200) : new Dimension(buffer.getWidth(), buffer.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(buffer, 0, 0, this);
g2d.dispose();
}
}
}
关于java - 如何将 JGraph 转换为玻璃板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16703650/
我是一名优秀的程序员,十分优秀!