Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trying to draw a line using VectorArray  (Read 4607 times)

0 Members and 1 Guest are viewing this topic.

Grimlen

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Trying to draw a line using VectorArray
« on: November 21, 2013, 07:55:48 am »
Just trying to draw a simple line. Nothing seems to be drawing.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jsoundeditor;

import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.JFileChooser;
import org.jsfml.audio.Sound;
import org.jsfml.audio.SoundBuffer;
import org.jsfml.graphics.Color;
import org.jsfml.graphics.RectangleShape;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.graphics.Vertex;
import org.jsfml.graphics.VertexArray;
import org.jsfml.system.Vector2f;
import org.jsfml.window.VideoMode;
import org.jsfml.window.event.Event;

/**
 *
 * @author Taylor
 */

public class JSoundEditor {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) throws IOException {
        RenderWindow window = new RenderWindow();
        window.create(new VideoMode(600, 225), "Hello!");
        window.setFramerateLimit(30);

        while(window.isOpen()){
            window.clear(Color.BLUE);
            window.display();
            VertexArray v = new VertexArray();
            Vector2f pos;
            pos = new Vector2f(1.0f, 1.0f);
            Vertex v1 = new Vertex(pos);
            pos = new Vector2f(5.0f, 1.0f);
            Vertex v2 = new Vertex(pos);
            v.add(v1);
            v.add(v2);
            window.draw(v);
            for(Event event : window.pollEvents()){
                if(event.type == Event.Type.CLOSED){
                    window.close();
                }
                else if(event.type == Event.Type.MOUSE_BUTTON_PRESSED){
                    System.out.println("test");
                }
            }
        }
    }
}
 

I figured it might have been because I was clearing the window as WHITE so I changed it to blue
so I could see the color of the line. Didn't work.
I can't even change the color of the Vertices.
IE:
v1.color = Color.RED;

I get an error:
"cannot assign a value to a final variable color"

So why isn't it drawing anything?

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Trying to draw a line using VectorArray
« Reply #1 on: November 21, 2013, 12:05:33 pm »
You need to tell the VertexArray what primitive type you want to use and add the correct constant for lines and its size (0 at first) to the constructor call.
That you cant change the color is probably a limitation of the Java-binding (I didn't use SFML with Java, but it was similar in Ruby).
I think you should be able to create the Vertex with the color directly.
Vertex v2 = new Vertex(pos,color);

Btw., it is bad to recreate the VertexArray anew every frame if you only use the same data (even if in such a tiny example program you wont notice a difference). You better move that code above of the gameloop. If later your game logic says it changed the data its based on, you can recreate it at a frame if it happens.

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: Trying to draw a line using VectorArray
« Reply #2 on: November 28, 2013, 07:03:13 am »
Vertices are immutable in JSFML, that's why there is not "setColor". That was a design decision for simple data structure classes, although it has not been too popular for vertices, so I might make an exception for these at some point.

wintertime is right, you can assign a color via the constructor. You can always consult the Javadoc for these kinds of problems.

And wintertime is correct about the primitive type for vertex arrays. You simply created a "new VertexArray()", that will cause every vertex to be drawn as a single point ("PrimitiveType.POINT"). Create it with "PrimitiveType.LINES" and it will connect each two vertices with a line. This is also documented in the Javadoc. :)
JSFML - The Java binding to SFML.