|
关键部分代码
- public PreviewPanel preview = null;
- ....
- preview = new PreviewPanel();
- preview.setOpaque(false);
- preview.addComponentListener(preview);
- preview.graph = graph; //右边的图
- ......
- //内部类实现
- public static class PreviewPanel extends JPanel implements
- ComponentListener {
- public GraphPanel graph = null;
- public void componentHidden(ComponentEvent e) {
- }
- public void componentMoved(ComponentEvent e) {
- }
- public void componentResized(ComponentEvent e) {
- repaint();
- }
- public void componentShown(ComponentEvent e) {
- }
- @Override
- protected void paintComponent(Graphics g) {
- if (graph != null) {
- int w = this.getWidth();
- int h = w * graph.getHeight() / graph.getWidth();
- // this.setSize(w, h);
- BufferedImage bi = new BufferedImage(graph.getWidth(), graph
- .getHeight(), BufferedImage.TYPE_INT_ARGB);
- Graphics gi = bi.createGraphics();
- graph.paint(gi);
- Image pi = bi.getScaledInstance(w, h, BufferedImage.SCALE_SMOOTH);
- g.clearRect(0, 0, getWidth(), getHeight());
- //g.setColor(Color.WHITE);
- //g.fillRect(0,0,getWidth(),getHeight());
- g.drawImage(pi, 0, 0, w, h, this);
- }
- repaint();
- }
- }
复制代码 |
|