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

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

0 Members and 1 Guest are viewing this topic.

cx3

  • 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;
                }
               
                @Override
                public void actionPerformed(ActionEvent  ae)
                {
                        String s = text.getText();
                       
                        area.append("JSFML>" s+"\n");
                        //rw.clear(Color.BLACK);
                       
                        if (s.equals("circle"))
                        {
                                CircleShape circle = new CircleShape(30,30);
                                circle.setOrigin(50,50);
                                circle.setRadius(30);
                                circle.setFillColor(Color.GREEN);
                               
                                //shape = circle;
                               
                                rw.clear();
                                rw.draw(circle);
                                rw.display();
                        }
                        //rw.display();
                       
                }
        }
                       
       
        public JSFML_Frame()   
        {      
                rw = new RenderWindow( new VideoMode(800,600), "JSFML" );
                //shapeToDraw = new CircleShape();
       
                setTitle("JSFML_Frame  command manager");
                setSize(375,175);
               
                Container ctr    = getContentPane();
                JButton   button = new JButton("Enter");
                       
                panel = new JPanel();
                text  = new JTextField(20);
               
                panel.add(text);
                panel.add(button);
                       
                button.addActionListener(       new ButtonOnClick()    );
               
                area    = new JTextArea(8, 40);
                scroll  = new JScrollPane(area);
               
                area.setEditable(false);
                area.setFont(new java.awt.Font("SansSerif" ,java.awt.Font.BOLD, 16));
                       
                ctr.add(panel, BorderLayout.SOUTH);
                ctr.add(area, BorderLayout.CENTER);
                       
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);
                Sprite   sprite = new Sprite();;
                try
                {
                        Texture texture = new Texture();
                        texture.loadFromFile( Paths.get("code.jpg") );
                        sprite = new Sprite( /*(ConstTexture)*/ texture );
                }
                catch(Exception e){}
                rw.setFramerateLimit(30);
                //fWindow.setVerticalSyncEnabled(false);
                Clock frameClock = new Clock();
               
       
                while (rw.isOpen())
                {
                        rw.clear(Color.BLACK);
                        rw.draw(sprite);
                        //if (shape!=null)  rw.draw(shape);
                        rw.display();
                                       
                        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();   
                                               
                                                rw.clear(Color.BLACK);
                                                sprite.rotate(dt*45);
                                                rw.draw(sprite);
                                                rw.display();
                                        }
                                }
                                       
                }
        }
                               
}

//...
 

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

pdinklag

  • 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).

Quote
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.