View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
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 org.microemu.device.swt;
21  
22  import org.eclipse.swt.graphics.GC;
23  import org.eclipse.swt.graphics.ImageData;
24  import org.microemu.app.ui.swt.SwtDeviceComponent;
25  import org.microemu.app.ui.swt.SwtGraphics;
26  import org.microemu.device.MutableImage;
27  
28  
29  public class SwtMutableImage extends MutableImage 
30  {
31  	public org.eclipse.swt.graphics.Image img;
32  	
33  	private org.eclipse.swt.graphics.GC gc;
34  	
35  	
36  	public SwtMutableImage(int width, int height) 
37  	{
38  		this.img = SwtDeviceComponent.createImage(width, height);
39  		this.gc = new GC(this.img);
40  		SwtDisplayGraphics displayGraphics = new SwtDisplayGraphics(new SwtGraphics(gc), this);
41  		displayGraphics.setColor(0x00ffffff);
42  		displayGraphics.fillRect(0, 0, width, height);
43  	}
44  
45  
46  	public javax.microedition.lcdui.Graphics getGraphics() 
47  	{
48  		SwtDisplayGraphics displayGraphics = new SwtDisplayGraphics(new SwtGraphics(gc), this);
49  		displayGraphics.setColor(0x00000000);
50  		displayGraphics.setClip(0, 0, getWidth(), getHeight());
51  		displayGraphics.translate(-displayGraphics.getTranslateX(), -displayGraphics.getTranslateY());
52  
53  		return displayGraphics;
54  	}
55  
56  
57  	public boolean isMutable() 
58  	{
59  		return true;
60  	}
61  
62  
63  	public int getHeight() 
64  	{
65  		return img.getBounds().height;
66  	}
67  
68  
69  	public org.eclipse.swt.graphics.Image getImage() 
70  	{
71  		return img;
72  	}
73  
74  
75  	public int getWidth() 
76  	{
77  		return img.getBounds().width;
78  	}
79  
80  
81  	public int[] getData() 
82  	{
83  		byte[] tmp = img.getImageData().data;
84  		int[] result = new int[tmp.length];
85  		
86  		for (int i = 0; i < tmp.length; i++) {
87  			result[i] = tmp[i];
88  		}
89  		
90  		return result;
91  	}
92  
93  
94  	public void getRGB(int[] argb, int offset, int scanlength, int x, int y, int width, int height) 
95  	{
96          if (width <= 0 || height <= 0) {
97              return;
98          }
99          if (x < 0 || y < 0 || x + width > getWidth() || y + height > getHeight()) {
100             throw new IllegalArgumentException("Specified area exceeds bounds of image");
101         }
102         if ((scanlength < 0 ? -scanlength : scanlength) < width) {
103             throw new IllegalArgumentException("abs value of scanlength is less than width");
104         }
105         if (argb == null) { 
106             throw new NullPointerException("null rgbData");
107         }
108         if (offset < 0 || offset + width > argb.length) {
109             throw new ArrayIndexOutOfBoundsException();
110         }
111         if (scanlength < 0) { 
112             if (offset + scanlength*(height-1) < 0) {
113                 throw new ArrayIndexOutOfBoundsException();
114             }
115         } else {
116             if (offset + scanlength*(height-1) + width > argb.length) {
117                 throw new ArrayIndexOutOfBoundsException();
118             }
119         }
120         
121         ImageData imageData = img.getImageData();
122         for (int i = 0; i < height; i++) {
123         		imageData.getPixels(x, y + i, width, argb, offset + i * scanlength);
124         }
125 	}
126 
127 }