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

Author Topic: From JTextField to JSFML RenderWindow  (Read 4356 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
From JTextField to JSFML RenderWindow
« on: February 19, 2015, 09:45:04 pm »
Hello :)

I'm trying to write an application that is based on JFrame (JTextArea, JTextField, JButton) as a console components. JButton has event as private class implementing ActionListener`s method actionPerformed, an instance of RenderWindow is kept in JFrame`s extended class as a private field.

For instance client of an application writes "circle 30 150 200" clicks enter or presses JButton, and it should draw a circle in RenderWindow (it should not create new instance for any command). I tried various combinations without visible effects. Please help :)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.util.*;
import java.lang.Object;

import java.nio.file.*;

import org.jsfml.graphics.*;
import org.jsfml.graphics.Color;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.window.VideoMode;
import org.jsfml.window.event.Event;
import org.jsfml.graphics.Texture;
import org.jsfml.graphics.Shader.Type;
import org.jsfml.system.Clock;
import org.jsfml.system.Vector2f;
import org.jsfml.window.*;
import org.jsfml.window.event.MouseWheelEvent;

class JSFML_Frame extends JFrame //implements Runnable

        private JTextField     text;
        private JPanel         panel;
        private JTextArea      area;
        private JScrollPane    scroll;
        org.jsfml.graphics.Shape shape;

        RenderWindow               rw;
        /*internal helper class for JButton in JSFML c-tor*/
        class  ButtonOnClick   implements ActionListener
                //RenderWindow rw;
                public ButtonOnClick(/*RenderWindow  rw_arg*/)
                //rw = rw_arg;
                public void actionPerformed(ActionEvent  ae)
                        String s = text.getText();
                        area.append("JSFML>" s+"\n");
                        if (s.equals("circle"))
                                CircleShape circle = new CircleShape(30,30);
                                //shape = circle;
        public JSFML_Frame()   
                rw = new RenderWindow( new VideoMode(800,600), "JSFML" );
                //shapeToDraw = new CircleShape();
                setTitle("JSFML_Frame  command manager");
                Container ctr    = getContentPane();
                JButton   button = new JButton("Enter");
                panel = new JPanel();
                text  = new JTextField(20);
                button.addActionListener(       new ButtonOnClick()    );
                area    = new JTextArea(8, 40);
                scroll  = new JScrollPane(area);
                area.setFont(new java.awt.Font("SansSerif" ,java.awt.Font.BOLD, 16));
                ctr.add(panel, BorderLayout.SOUTH);
                ctr.add(area, BorderLayout.CENTER);
                Sprite   sprite = new Sprite();;
                        Texture texture = new Texture();
                        texture.loadFromFile( Paths.get("code.jpg") );
                        sprite = new Sprite( /*(ConstTexture)*/ texture );
                catch(Exception e){}
                Clock frameClock = new Clock();
                while (rw.isOpen())
                        //if (shape!=null)  rw.draw(shape);
                        for (Event event : rw.pollEvents())    
                                switch (event.type)
                                        case CLOSED:  { rw.close(); return; }
                                        case MOUSE_WHEEL_MOVED:
                                                Random r = new Random(9);
                                                float dt = frameClock.restart().asSeconds();   


And second question. From time to time I do not use any IDE for Java. How to compile project using command line?


  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: From JTextField to JSFML RenderWindow
« Reply #1 on: February 20, 2015, 01:16:41 pm »
First off, you might be running into threading issues, but even if you rule them out, your code is not going to work.

Upon the click of the button, you would draw a circle in the window ONCE only. Your main loop will immediately clear and redraw the whole window, however, so the circle is gone.

You commented out this line:
//if (shape!=null)  rw.draw(shape);

This would be the correct way to approach the issue, save the shape in a member variable and use that in your main loop. Make sure that you synchroninze any modification of that variable, because you are dealing with multiple threads (please read up on Swing / AWT event handling and the AWT thread, I can't offer support for that here).

How to compile project using command line?
Nothing special here, just make sure jsfml.jar is in the classpath.
JSFML - The Java binding to SFML.