Skip to content

Commit

Permalink
Graphics: Improve clip region handling
Browse files Browse the repository at this point in the history
Previously FreeJ2ME was not consistent in regards to the clip
region that it has set, and what it would send to jars when they
requested data about the clip region and position... it also wasn't
following the MIDP spec too closely. These new changes improve it
by cleaning up the older clip code, and using a single, consistent
Rectangle object to keep track of the clip bounds.

Fixes #62 and probably many others that rely on clip region
information.
  • Loading branch information
AShiningRay committed Feb 26, 2025
1 parent e48d23b commit ad129a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 50 deletions.
30 changes: 9 additions & 21 deletions src/javax/microedition/lcdui/Graphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package javax.microedition.lcdui;

import org.recompile.mobile.PlatformGraphics;
import java.awt.Rectangle;

public class Graphics
{
Expand All @@ -34,10 +35,7 @@ public class Graphics
protected int translateX = 0;
protected int translateY = 0;

protected int clipX = 0;
protected int clipY = 0;
protected int clipWidth = 0;
protected int clipHeight = 0;
protected Rectangle rect = new Rectangle();

protected int color = 0xFFFFFF;
protected Font font = Font.getDefaultFont();
Expand Down Expand Up @@ -105,29 +103,19 @@ public void clipRect(int x, int y, int width, int height) { }

public void setClip(int x, int y, int width, int height) { }

public int getClipHeight() { return clipHeight; }
public int getClipHeight() { return rect.height; }

public int getClipWidth() { return clipWidth; }
public int getClipWidth() { return rect.width; }

public int getClipX() { return clipX; }
public int getClipX() { return rect.x; }

public int getClipY() { return clipY; }
public int getClipY() { return rect.y; }

public void translate(int x, int y)
{
translateX += x;
translateY += y;
}
public void translate(int x, int y) { }

public int getTranslateX()
{
return translateX;
}
public int getTranslateX() { return translateX; }

public int getTranslateY()
{
return translateY;
}
public int getTranslateY() { return translateY; }

public void setColor(int RGB) { color = RGB; }

Expand Down
39 changes: 10 additions & 29 deletions src/org/recompile/mobile/PlatformGraphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
Expand Down Expand Up @@ -79,25 +78,21 @@ public PlatformGraphics(PlatformImage image)

canvasData = ((DataBufferInt) canvas.getRaster().getDataBuffer()).getData();


platformGraphics = this;

clipX = 0;
clipY = 0;
clipWidth = canvas.getWidth();
clipHeight = canvas.getHeight();
clipRect(0, 0, canvas.getWidth(), canvas.getHeight());

setColor(0,0,0);
setStrokeStyle(SOLID);
gc.setBackground(new Color(0, 0, 0, 0));
gc.setFont(font.platformFont.awtFont);
gc.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

platformGraphics = this;
}

public void reset() //Internal use method, resets the Graphics object to its inital values
{
translate(-1 * translateX, -1 * translateY);
setClip(0, 0, canvas.getWidth(), canvas.getHeight());
clipRect(0, 0, canvas.getWidth(), canvas.getHeight());
setColor(0,0,0);
setFont(Font.getDefaultFont());
setStrokeStyle(SOLID);
Expand Down Expand Up @@ -250,9 +245,9 @@ public void flushGraphics(Image image, int x, int y, int width, int height)
int destIndex = j * canvas.getWidth() + i;
int srcIndex = j * image.getWidth() + i;

// The image data CAN go out of the destination bounds, we just can't draw it whenever it does.
if (i < 0 || i >= canvas.getWidth()) { continue; }
if (j < 0 || j >= canvas.getHeight()) { continue; }
// The image data CAN go out of the destination bounds (and so can the clip rectangle), we just can't draw it whenever it does.
if (i < getClipX() || i >= getClipX() + getClipWidth() || i >= canvas.getWidth()) { continue; }
if (j < getClipY() || j >= getClipY() + getClipHeight() || j >= canvas.getHeight()) { continue; }
if (destIndex < 0 || destIndex >= canvasData.length) { continue; }
if (srcIndex < 0 || srcIndex >= pixels.length) { continue; }

Expand Down Expand Up @@ -334,8 +329,8 @@ public void drawRGB(int[] rgbData, int offset, int scanlength, int x, int y, int
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();

if (y + height > clipY + clipHeight) { height = (clipY + clipHeight) - y; }
if (x + width > clipX + clipWidth) { width = (clipX + clipWidth) - x; }
if (y + height > getClipY() + getClipHeight()) { height = (getClipY() + getClipHeight()) - y; }
if (x + width > getClipX() + getClipWidth()) { width = (getClipX() + getClipWidth()) - x; }

// Ensure adjusted width and height are still positive
if (width <= 0 || height <= 0) { return; }
Expand All @@ -352,7 +347,7 @@ public void drawRGB(int[] rgbData, int offset, int scanlength, int x, int y, int

// Skip if the pixel isn't in the canvas bounds
if (x + j < 0 || x + j >= canvasWidth) { continue; }
if (y + i < clipY || y + i >= clipY + clipHeight || x + j < clipX || x + j >= clipX + clipWidth) { continue; }
if (y + i < getClipY() || y + i >= getClipY() + getClipHeight() || x + j < getClipX() || x + j >= getClipX() + getClipWidth()) { continue; }
if (destIndex < 0 || destIndex >= canvasData.length) { continue; }
if (pixelIndex < 0 || pixelIndex >= rgbData.length) { continue; }

Expand Down Expand Up @@ -507,23 +502,13 @@ public void setFont(Font font)
public void setClip(int x, int y, int width, int height)
{
gc.setClip(x, y, width, height);
Rectangle rect=new Rectangle();
gc.getClipBounds(rect);
clipX = (int) rect.getX();
clipY = (int) rect.getY();
clipWidth = (int)rect.getWidth();
clipHeight = (int)rect.getHeight();
}

public void clipRect(int x, int y, int width, int height)
{
gc.clipRect(x, y, width, height);
Rectangle rect=new Rectangle();
gc.getClipBounds(rect);
clipX = (int) rect.getX();
clipY = (int) rect.getY();
clipWidth = (int)rect.getWidth();
clipHeight = (int)rect.getHeight();
}

public int getTranslateX() { return translateX; }
Expand All @@ -535,10 +520,6 @@ public void translate(int x, int y)
translateX += x;
translateY += y;
gc.translate(x, y);
Rectangle rect=new Rectangle();
gc.getClipBounds(rect);
clipX = (int) rect.getX();
clipY = (int) rect.getY();
}

private int AnchorX(int x, int width, int anchor)
Expand Down

0 comments on commit ad129a8

Please sign in to comment.