public Texture formatText (TextFormat format, String text, boolean border) { TextLayout layout = game.graphics.layoutText(text, format); float margin = border ? 10 : 0; float width = layout.size.width()+2*margin, height = layout.size.height()+2*margin; Canvas canvas = game.graphics.createCanvas(width, height); if (border) canvas.setFillColor(0xFFCCCCCC).fillRect(0, 0, canvas.width, canvas.height); canvas.setFillColor(0xFF000000).fillText(layout, margin, margin); if (border) canvas.setStrokeColor(0xFF000000).strokeRect(0, 0, width-1, height-1); return canvas.toTexture(); }
public void draw(Canvas canvas) { canvas.setFillColor(0xFF99CCFF); canvas.fillRect(0, 0, 100, 100); // draw a point and some lines canvas.setStrokeColor(0xFFFF0000); canvas.drawPoint(50, 50); canvas.drawLine(0, 25, 100, 25); canvas.drawLine(0, 75, 100, 75); canvas.drawLine(25, 0, 25, 100); canvas.drawLine(75, 0, 75, 100); // stroke and fill a circle canvas.strokeCircle(25, 75, 10); canvas.setFillColor(0xFF0000FF); canvas.fillCircle(75, 75, 10); } });
@Override public void render (Canvas canvas, TextLayout text, int textColor, boolean underlined, float x, float y) { canvas.save(); canvas.setStrokeColor(outlineColor); canvas.setStrokeWidth(outlineWidth*2); canvas.setLineCap(outlineCap); canvas.setLineJoin(outlineJoin); canvas.strokeText(text, x+outlineWidth, y+outlineWidth); canvas.setFillColor(textColor); canvas.fillText(text, x+outlineWidth, y+outlineWidth); if (underlined) { IRectangle bounds = text.bounds; float sx = x + bounds.x() + outlineWidth; float sy = y + bounds.y() + bounds.height() + outlineWidth + 1; canvas.fillRect(sx, sy, bounds.width(), 1); } canvas.restore(); }
void outer (Canvas canvas, float y) { canvas.setFillColor(0xFF000000); canvas.fillRect(2, y, 94, 30); } void inner (Canvas canvas, float y) {
public void draw(Canvas canvas, Image pea) { canvas.setFillColor(0xFF99CCFF); canvas.fillRect(0, 0, 100, 100); // draw an image normally, scaled, cropped, cropped and scaled, etc. float half = 37/2f; canvas.drawImage(pea, 10, 10); canvas.drawImage(pea, 55, 10, 37, 37, half, half, half, half); canvas.drawImage(pea, 10, 55, 37, 37, half, 0, half, half); canvas.drawImage(pea, 55, 55, 37, 37, half, half/2, half, half); } });
public void draw(Canvas canvas) { canvas.setFillColor(0xFF99CCFF); canvas.fillCircle(15, 15, 15); canvas.setStrokeColor(0xFF000000); canvas.strokeRect(0, 0, 30, 30); } });
Gradient.Config cfg = new Gradient.Radial( 0, 0, 50, new int[] { 0xFFFF0000, 0xFF00FF00 }, new float[] { 0, 1 }); canvas.setFillGradient(canvas.createGradient(cfg)); canvas.fillRect(0, 0, 100, 100); }); Gradient.Config cfg = new Gradient.Linear( 0, 0, 100, 100, new int[] { 0xFF0000FF, 0xFF00FF00 }, new float[] { 0, 1 }); canvas.setFillGradient(canvas.createGradient(cfg)); canvas.fillRect(0, 0, 100, 100); }); canvas.setFillPattern(tile.createPattern(true, true)); canvas.fillRect(0, 0, 100, 100); }); canvas.setFillColor(0xFF99CCFF); canvas.fillRect(0, 0, 100, 100); canvas.setStrokeColor(0xFFFF0000); canvas.drawPoint(50, 50); canvas.drawLine(0, 25, 100, 25); canvas.drawLine(0, 75, 100, 75); canvas.drawLine(25, 0, 25, 100); canvas.drawLine(75, 0, 75, 100); canvas.strokeCircle(25, 75, 10); canvas.setFillColor(0xFF0000FF); canvas.fillCircle(75, 75, 10);
protected void render (Canvas canvas, String strokeFill, TextBlock block, TextBlock.Align align, int color, float x, float y, boolean showBounds) { float sy = y + block.bounds.y(); for (TextLayout layout : block.lines) { float sx = x + block.bounds.x() + align.getX( layout.size.width(), block.bounds.width()-block.bounds.x()); if (showBounds) { IRectangle lbounds = layout.bounds; canvas.setStrokeColor(0xFFFFCCCC).setStrokeWidth(1); canvas.strokeRect(sx+lbounds.x(), sy+lbounds.y(), lbounds.width(), lbounds.height()); } if (strokeFill.equals("Fill")) { canvas.setFillColor(color).fillText(layout, sx, sy); } else { canvas.setStrokeColor(color).strokeText(layout, sx, sy); } sy += layout.ascent() + layout.descent() + layout.leading(); } }
public void draw(Canvas canvas) { float dotRadius = 40; canvas.save(); canvas.setFillGradient(graphics().createRadialGradient( 100 / 3, 100 / 2.5f, dotRadius, new int[] { 0xFFFFFFFF, 0xFFCC66FF }, new float[] { 0f, 1f })); canvas.fillCircle(50, 50, dotRadius); canvas.restore(); canvas.setStrokeColor(0xFF000000); canvas.setStrokeWidth(1.5f); canvas.strokeCircle(50, 50, dotRadius); } });
@Override public void init() { String text = "The quick brown fox jumped over the lazy dog."; TextFormat format = new TextFormat(new Font("Helvetica", 18)); TextBlock block = new TextBlock(game.graphics.layoutText(text, format, new TextWrap(100))); float x = 5; for (float scale : new float[] { 1f, 2f, 3f }) { float swidth = block.bounds.width() * scale, sheight = block.bounds.height() * scale; Canvas canvas = game.graphics.createCanvas(swidth, sheight); canvas.setStrokeColor(0xFFFFCCCC).strokeRect(0, 0, swidth-0.5f, sheight-0.5f); canvas.scale(scale, scale); canvas.setFillColor(0xFF000000); block.fill(canvas, TextBlock.Align.RIGHT, 0, 0); game.rootLayer.addAt(new ImageLayer(canvas.toTexture()), x, 5); addInfo(canvas, x + swidth/2, sheight + 10); x += swidth + 5; } }
@Override public void render (Canvas canvas, TextLayout layout, int textColor, boolean underlined, float x, float y) { canvas.save(); canvas.setFillColor(textColor); if (underlined) { IRectangle bounds = layout.bounds; float sx = x + bounds.x(), sy = y + bounds.y() + bounds.height() + 1; canvas.fillRect(sx, sy, bounds.width(), 1); } canvas.fillText(layout, x, y); canvas.restore(); } };
@Override public void update(int delta) { canvas.clear(); canvas.setStrokeWidth(3); canvas.setStrokeColor(0x88ff0000); noSegs += direction; if (noSegs > 50) direction = -1; if (noSegs < 20) direction = 1; final float r = 100; for (int ii = 0; ii < noSegs; ii++) { float angle = 2*FloatMath.PI * ii / noSegs; float x = (r * FloatMath.cos(angle)) + graphics().width() / 2; float y = (r * FloatMath.sin(angle)) + graphics().height() /2; canvas.strokeCircle(x, y, 100); } } }
protected Texture makeLabel(String label) { TextLayout layout = game.graphics.layoutText(label, new TextFormat()); Canvas canvas = game.graphics.createCanvas(layout.size); canvas.setFillColor(0xFF000000).fillText(layout, 0, 0); return canvas.toTexture(); }
@Override public Surface drawLine(float x0, float y0, float x1, float y1, float width) { canvas.save(); canvas.setStrokeWidth(width); canvas.drawLine(x0, y0, x1, y1); canvas.restore(); return this; }
protected ImageLayer createLabel(String text, GroupLayer parent, int fg, int bg, float x, float y, float padding) { TextLayout layout = game.graphics.layoutText(text, baseFormat); float twidth = layout.size.width() + padding * 2; float theight = layout.size.height() + padding * 2; Canvas canvas = game.graphics.createCanvas(twidth, theight); if (bg != 0) canvas.setFillColor(bg).fillRect(0, 0, twidth, theight); canvas.setFillColor(fg).fillText(layout, padding, padding); ImageLayer imageLayer = new ImageLayer(canvas.toTexture()); parent.addAt(imageLayer, x, y); return imageLayer; }
public void draw(Canvas canvas) { canvas.setFillColor(0xFFCCCCCC).fillRect(0, 0, 50, 50); canvas.setFillColor(0xFFCCCCCC).fillRect(50, 50, 50, 50); TextLayout capF = graphics().layoutText("F", new TextFormat().withFont(F_FONT)); float theta = -FloatMath.PI/4, tsin = FloatMath.sin(theta), tcos = FloatMath.cos(theta); canvas.setFillColor(0xFF000000).fillText(capF, 0, 0); canvas.transform(tcos, -tsin, tsin, tcos, 50, 50); canvas.setFillColor(0xFF000000).fillText(capF, 0, 0); } });
protected void addClippedLayers () { Canvas circle = game.graphics.createCanvas(100, 100); circle.setFillColor(0xFFCC99FF).fillCircle(50, 50, 50); Texture cirtex = circle.toTexture(); Gradient linear = sausage.createGradient(new Gradient.Linear( 0, 0, 100, 100, new int[] { 0xFF0000FF, 0xFF00FF00 }, new float[] { 0, 1 })); sausage.setFillGradient(linear).fillRoundRect(0, 0, 100, 50, 10); Texture saustex = sausage.toTexture();
protected Entity createShip (float x, float y) { Entity ship = create(true); ship.add(type, sprite, opos, pos, vel, spin, radius); Canvas canvas = graphics().createCanvas(30, 20); Path path = canvas.createPath(); path.moveTo(0, 0).lineTo(30, 10).lineTo(0, 20).close(); canvas.setFillColor(0xFFCC99FF).fillPath(path); ImageLayer layer = new ImageLayer(canvas.toTexture()); layer.setOrigin(15, 10); layer.setRotation(-MathUtil.HALF_PI); int id = ship.id; type.set(id, SHIP); sprite.set(id, layer); opos.set(id, x, y); pos.set(id, x, y); vel.set(id, 0, 0); radius.set(id, 10); return ship; }
Texture colorTex () { if (colorTex == null) { Canvas canvas = createCanvas(1, 1); canvas.setFillColor(0xFFFFFFFF).fillRect(0, 0, canvas.width, canvas.height); colorTex = canvas.toTexture(Texture.Config.UNMANAGED); } return colorTex; }
protected void addAction(String action) { _actions.add(0, action); if (_actions.size() > 10) _actions.subList(10, _actions.size()).clear(); _actionsImage.canvas().clear(); StringBuilder buf = new StringBuilder(); for (String a : _actions) { if (buf.length() > 0) buf.append("\n"); buf.append(a); } _actionsImage.canvas().setFillColor(0xFF000000); float y = 0; for (TextLayout layout : graphics().layoutText(buf.toString(), TEXT_FMT, new TextWrap(300))) { _actionsImage.canvas().fillText(layout, 0, y); y += layout.ascent() + layout.descent() + layout.leading(); } }