Processing- What could be possible errors of svg file not displayed? - svg

I'm trying to display an svg image with Processing, but it would only show a blank white space. I don't think there is any problem with the code as it works perfectly when I change the shape to ellipse. Do I have to modify any code below to display the svg file, or is there any other possible reasons why it wouldn't show? Thanks in advance!
Table table;
PFont f;
PShape leaf;
color [] c = {color(225, 50, 50), color(225, 100, 0), color(225, 225, 0), color(0, 150, 0), color(0), color(125)};
int i=0;
void setup() {
size(1100, 500);
background(255);
table=loadTable("P3_data.csv", "header");
leaf= loadShape("leaf.svg");
leaf.disableStyle();
}
void draw() {
stroke(255);
strokeWeight(0.1);
for (TableRow row : table.rows()) {
int friend= (row.getInt("Friend"));
int travel= (row.getInt("Travel"));
int selfimprovement= (row.getInt("Self-improvement"));
int club= (row.getInt("Club"));
int schoolwork= (row.getInt("Schoolwork"));
int money= (row.getInt("Money"));
int total= 0;
int [] Daily= {friend, travel, selfimprovement, club, schoolwork, money};
for (int k=0; k<6; k++) {
total +=Daily[k];
}
println (total);
for (int j=0; j<6; j++) {
for (int m=0; m< Daily[j]; m++) {
fill(c[j]);
ellipse((i%120)*10+10, (i/120)*40+10, 3*total, 4*total);
//shape(leaf, (i%120)*10+10, (i/120)*40+10, 3*total, 3*total);
total --;
}
}
if (i>1095) {
break;
}
i++;
}
save("sketch.png");
}

See the documentation of disableStyle()
Shapes are loaded with style information that tells them how to draw (the color, stroke weight, etc.) The disableStyle() method of PShape turns off this information. The enableStyle() method turns it back on.
If you want to display the SVG with its style, the you have to remove the disableStyle call:
leaf = loadShape("leaf.svg");
leaf.disableStyle();
If you want to change the stroke and fill color of the shape generated form the *svg" file, then indeed you have to disable the style:
leaf= loadShape("leaf.svg");
leaf.disableStyle();
In this case the shape is drawn with the current fill and stroke color. This means that all the shape is filled with the same fill color and drawn with the same stroke color:
for (int m=0; m< Daily[j]; m++) {
stroke(0, 0, 255); // blue
fill(255, 0, 0); // red
shape(leaf, (i%120)*10+10, (i/120)*40+10, 3*total, 3*total);
total --;
}

Related

LibGDX draws dots instead of lines

I would like to implement some kind of eraser,so in my render method I make the upper layer transparent.
#Override
public void render () {
cam.update();
Gdx.gl.glClearColor(1, 1, 1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (Gdx.input.isTouched()) {
pos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
pixmap.setColor(new Color(1, 1, 1, 0.5f)); //transparency
pixmap.fillCircle((int)pos.x, (int)pos.y, 10);
}
texture3.draw(pixmap, 0, 0);
batch.begin();
batch.draw(texture, 0, 0);
batch.draw(texture3, 0, 0);
batch.end();
}
But I got points when make swipes. It requires to do very slow speed to make lines instead of dots.
So I expect continuous line instead of dots.
Can you advice something please?
Dots instead of line
This is caused because of the frequency at which the input state is updated, the solution here would be to manually calculate the missing points needed to make a line, you could do this with a linear interpolation between each pair of dots, additionally you could calculate how many extra dots are necessary depending on how far is the newest dot from the previous one, in my example I use an arbitrary number of extra dots (20) like so:
public class TestDraw extends Game {
private Pixmap pixmap;
private Texture texture;
private SpriteBatch batch;
private Vector2 lastPos;
#Override
public void create() {
pixmap = new Pixmap(1000, 1000, Pixmap.Format.RGBA8888);
texture = new Texture(pixmap);
batch = new SpriteBatch();
lastPos = new Vector2();
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (Gdx.input.isTouched()) {
pixmap.setColor(new Color(1, 1, 1, 0.5f)); //transparency
int newX = Gdx.input.getX();
int newY = Gdx.input.getY();
pixmap.setColor(Color.RED);
pixmap.fillCircle(newX, newY, 10);
// If the distance is too far, fill with extra dots
if (lastPos.dst(newX, newY) > 10) { // Here far is 10, you can adjust as needed
int extraDots = 20; // How many extra dots to draw a line, I use 20, adjust as needed or calculate according to distance (for example lastPos.dst(newX,newY) * 5)
for (int i = 0; i < extraDots; i++) {
float progress = (1f / extraDots) * i;
int dotX = (int) MathUtils.lerp(lastPos.x, newX, progress);
int dotY = (int) MathUtils.lerp(lastPos.y, newY, progress);
pixmap.setColor(Color.BLUE);
pixmap.fillCircle(dotX, dotY, 10);
}
}
// Store last position for next render() call
lastPos.set(newX, newY);
}
texture.draw(pixmap, 0, 0);
batch.begin();
batch.draw(texture, 0, 0);
batch.end();
}
}
Adecuate to your code as needed, I didn't know what was texture3 so I didn't include in my example
Also another option which I don't like too much because of rendering and storage cost is using a Polygon to draw the lines.

How do I change the outline color (stroke) of my shape after drawing it?

I'm coding in Processing for the first time (previously familiar with Java) and I'm trying to make a grid of triangles where when I click one, it changes the fill and stroke to a different color. The fill is changing but the stroke remains the default color. Here is my code:
void setup() {
size(800, 600); // size of canvas
triangles = new ArrayList<TriangleClass>(); // Create an empty ArrayList
int L = 50; // length of triangle side
double halfStep = L * Math.sqrt(3);
// all the code about making the grid
}
void draw() {
background(0);
TriangleClass myCurrentTriangle;
for (int i = 0; i < triangles.size(); i++) {
// get object from ArrayList
myCurrentTriangle = triangles.get(i);
myCurrentTriangle.display();
}
}
void mouseClicked () {
TriangleClass myCurrentTriangle ;
for (int i=0; i < triangles.size(); i++) {
// get object from ArrayList
myCurrentTriangle = triangles.get(i);
myCurrentTriangle.mouseOver();
}
}
class TriangleClass {
double x1, y1, x2, y2, x3, y3; // points
color fill; // fill color
color stroke; // stroke color
float mouseSensorX, mouseSensorY;// check point for dist to mouse
// constructor
TriangleClass(
// ...
stroke = color(174, 208, 234);
fill = color(249, 249, 249);
mouseSensorX = (float) (x1+x2+x3 )/ 3;
mouseSensorY = (float) (y1+y2+y3 )/ 3;
}
void mouseOver() {
if (dist(mouseX, mouseY, mouseSensorX, mouseSensorY) < 17) {
if (fill == color(249, 249, 249)) {
stroke = color(251, 84, 84);
fill = color(251,84,84);
// ... repeated for other colors
}
}
void display() {
// show triangle
stroke(stroke);
fill(fill);
triangle((float) x1, (float) y1, (float) x2, (float) y2, (float) x3, (float) y3);
}
}
// =====================================================================
I believe the problem is the stroke weight. All you need to do it have this line of code at the end of your setup function:
strokeWeight(3);
The larger the number, the larger the outline.

how to apply gradient effect on Image GDI

How can I apply gradient effect on image like this image in c#. I have a transparent image with black drawing I want to apply 2 color gradient on the image is this possible in gdi?
Here is the effect i want to achieve
http://postimg.org/image/ikz1ie7ip/
You create a PathGradientBrush and then you draw your texts with that brush.
To create a bitmap filled with a gradient brush you could do something like:
public Bitmap GradientImage(int width, int height, Color color1, Color color2, float angle)
{
var r = new Rectangle(0, 0, width, height);
var bmp = new Bitmap(width, height);
using (var brush = new LinearGradientBrush(r, color1, color2, angle, true))
using (var g = Graphics.FromImage(bmp))
g.FillRectangle(brush, r);
return bmp;
}
So now that you have an image with the gradient in it, all you have to do is to bring over the alpha channel from your original image into the newly created image. We can take the transferOneARGBChannelFromOneBitmapToAnother function from a blog post I once wrote:
public enum ChannelARGB
{
Blue = 0,
Green = 1,
Red = 2,
Alpha = 3
}
public static void transferOneARGBChannelFromOneBitmapToAnother(
Bitmap source,
Bitmap dest,
ChannelARGB sourceChannel,
ChannelARGB destChannel )
{
if ( source.Size!=dest.Size )
throw new ArgumentException();
Rectangle r = new Rectangle( Point.Empty, source.Size );
BitmapData bdSrc = source.LockBits( r, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb );
BitmapData bdDst = dest.LockBits( r, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb );
unsafe
{
byte* bpSrc = (byte*)bdSrc.Scan0.ToPointer();
byte* bpDst = (byte*)bdDst.Scan0.ToPointer();
bpSrc += (int)sourceChannel;
bpDst += (int)destChannel;
for ( int i = r.Height * r.Width; i > 0; i-- )
{
*bpDst = *bpSrc;
bpSrc += 4;
bpDst += 4;
}
}
source.UnlockBits( bdSrc );
dest.UnlockBits( bdDst );
}
Now you could do something like:
var newImage = GradientImage( original.Width, original.Height, Color.Yellow, Color.Blue, 45 );
transferOneARGBChannelFromOneBitmapToAnother( original, newImage, ChannelARGB.Alpha, ChannelARGB.Alpha );
And there you are. :-)

ProcessingJS modify stroke-width in SVG

I would like to dynamically set the stroke-width of an SVG curve from within a ProcessingJS sketch.
So far, the only solution that I have found is to use disableStyle() on the SVG shape and then manually set all of the style attributes like Fill(), stroke(), strokeJoin() and strokeWeight(). However, the opacity of the line seems to render differently when executed this way.
Is there some way to access and modify only the stroke-width and leave the other styles unchanged?
Here is the code that I have so far. It seems to work okay, but it would be nice not to have to manually reset all the style attributes that were in the original svg file.
/* #pjs preload="frog_trajs.svg"; */
PShape trajs;
float zoom_factor = 1.0;
float imgW;
float ingH;
float lineWeight = 1.00;
//panning variables
float centerX;
float centerY;
boolean active = false;
PFont f;
void setup()
{
size(900, 600);
frameRate(20);
centerX = width/2;
centerY = height/2;
trajs = loadShape("frog_trajs.svg");
trajs.disableStyle();
imgW = trajs.width;
imgH = trajs.height;
}
void draw()
{
background(255);
//here, the styles are reset, with lineWeight dynamically updated by mouseScrolled()
shapeMode(CENTER);
strokeJoin(ROUND);
strokeWeight(lineWeight);
stroke(#EF25B2,170);
noFill();
shape(trajs,centerX,centerY,imgW,imgH);
}
void mouseScrolled()
{
if(active){
if(mouseScroll > 0)
{
//zoom out;
zoom_factor = 1 - mouseScroll*0.01;
}
else
{
//zoom in;
zoom_factor = 1 - mouseScroll*0.01;
}
lineWeight = lineWeight/zoom_factor;
}
}
void mouseOver(){
active = true;
}
void mouseOut(){
active = false;
}

Draw string vertically with transparent background

I would like to draw a string on the screen, rotated by 90 degrees, on a transparent background:
public static void drawStringWithTransformROT90(String text, int x, int y, int color, Graphics g) {
// create a mutable image with white background color
Image im = Image.createImage(g.getFont().stringWidth(text), g.getFont().getHeight());
Graphics imGraphics = im.getGraphics();
// set text color to black
imGraphics.setColor(0x00000000);
imGraphics.drawString(text, 0, 0, Graphics.TOP|Graphics.LEFT);
int[] rgbData = new int[im.getWidth() * im.getHeight()];
im.getRGB(rgbData, 0, im.getWidth(), 0, 0, im.getWidth(), im.getHeight());
for (int i = 0; i < rgbData.length; i++) {
// if it is the background color (white), set it to transparent
if (rgbData[i] == 0xffffffff) {
rgbData[i] = 0x00000000;
} else {
// otherwise (black), change the text color
rgbData[i] = color;
}
}
Image imageWithAlpha = Image.createRGBImage(rgbData, im.getWidth(), im.getHeight(), true);
Sprite s = new Sprite(imageWithAlpha);
// rotate the text
s.setTransform(Sprite.TRANS_ROT90);
s.setPosition(x, y);
s.paint(g);
}
Is there any better way to do this? Should I create a transparent image with the alphabet rotated, and draw it using a Sprite object?
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;
public class MyCanvas extends Canvas {
public void paint(Graphics g) {
//The text that will be displayed
String s="java";
//Create the blank image, specifying its size
Image img=Image.createImage(50,50);
//Create an instance of the image's Graphics class and draw the string to it
Graphics gr=img.getGraphics();
gr.drawString(s, 0, 0, Graphics.TOP|Graphics.LEFT);
//Display the image, specifying the rotation value. For example, 90 degrees
g.drawRegion(img, 0, 0, 50, 50, Sprite.TRANS_ROT90, 0, 0, Graphics.TOP|Graphics.LEFT);
}
}
As found at http://wiki.forum.nokia.com/index.php/How_to_display_rotated_text_in_Java_ME
If you use Java2D for drawing you can specify java.awt.Graphics2D#setTransform.

Resources