Source code: com/barteo/emulator/app/ui/swing/SwingDisplayComponent.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package com.barteo.emulator.app.ui.swing;
21
22 import java.awt.Component;
23 import java.awt.Graphics;
24
25 import com.barteo.emulator.DisplayComponent;
26 import com.barteo.emulator.app.ui.DisplayRepaintListener;
27 import com.barteo.emulator.device.Device;
28 import com.barteo.emulator.device.DeviceFactory;
29 import com.barteo.emulator.device.MutableImage;
30 import com.barteo.emulator.device.j2se.J2SEDeviceDisplay;
31 import com.barteo.emulator.device.j2se.J2SEMutableImage;
32
33 public class SwingDisplayComponent implements DisplayComponent
34 {
35 private Component deviceCanvas;
36 private J2SEMutableImage displayImage = null;
37 private DisplayRepaintListener displayRepaintListener;
38
39
40 SwingDisplayComponent(Component deviceCanvas)
41 {
42 this.deviceCanvas = deviceCanvas;
43 }
44
45
46 public void addDisplayRepaintListener(DisplayRepaintListener l)
47 {
48 displayRepaintListener = l;
49 }
50
51
52 public void removeDisplayRepaintListener(DisplayRepaintListener l)
53 {
54 if (displayRepaintListener == l) {
55 displayRepaintListener = null;
56 }
57 }
58
59
60 public MutableImage getDisplayImage()
61 {
62 return displayImage;
63 }
64
65
66 public void paint(Graphics g)
67 {
68 if (displayImage != null) {
69 g.drawImage(displayImage.getImage(), 0, 0, null);
70 }
71 }
72
73
74 public void repaint()
75 {
76 Device device = DeviceFactory.getDevice();
77
78 if (device != null) {
79 if (displayImage == null) {
80 displayImage = new J2SEMutableImage(
81 device.getDeviceDisplay().getFullWidth(), device.getDeviceDisplay().getFullHeight());
82 }
83
84 Graphics gc = displayImage.getImage().getGraphics();
85 ((J2SEDeviceDisplay) device.getDeviceDisplay()).paint(gc);
86
87 fireDisplayRepaint(displayImage);
88 }
89
90 deviceCanvas.repaint();
91 }
92
93
94 private void fireDisplayRepaint(MutableImage image)
95 {
96 if (displayRepaintListener != null) {
97 displayRepaintListener.repaintInvoked(image);
98 }
99 }
100
101 }