Every Monday, there will be a event in the discord server (:
Vin vin 45 Junior Poster in Training
I made a JavaFX Discord Server for people who like to talk about JavaFX stuff, or need help with certain projects, I really like to work with java, especially with people who are working with JavaFX (:
Here is the discord link: https://discord.gg/yZ3Y3Fd or https://discord.gg/AzFaNU
rproffitt commented: Nice. Be sure to message here as well as we have cookies. (direct messaging.) +0
Vin vin 45 Junior Poster in Training
I don't have the code, but you can check out the download link of that website, there is some source code that can guide you.
Vin vin 45 Junior Poster in Training
solved it :D
This amazing website got it for me
http://lagers.org.uk/javafx/usemeshview.html
It took me 1 week to figure out how this works.
Vin vin 45 Junior Poster in Training
If you are wondering, what "FxTimer" is, it is from ReactFX library, with this I can make a Timeline, that loops 60 times per second.
this part of code is not really important. It was just to test if the cube can move. Like if the 6 sides of the cube can even move.
Vin vin 45 Junior Poster in Training
this is the code, what I have right now:
This is the first main class.
package com.vinayak.trianglemesh.experiment;
import java.time.Duration;
import org.reactfx.util.FxTimer;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.geometry.Rectangle2D;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class CoreCube extends Application {
public static final String PROGRAM_TITLE = "Mooie Kubus Experiment";
public static Rectangle2D primaryScreenBounds = Screen.getPrimary().getBounds();
public static double monitorWidth = (primaryScreenBounds.getWidth());
public static double monitorHeight = (primaryScreenBounds.getHeight());
public static double sceneWidth = (monitorWidth / 1.5);
public static double sceneHeight = (monitorHeight / 1.5);
public static boolean isFullScreen = false;
public static boolean isConsoleSpamOn = false;
public static Group group;
public static Scene scene;
public static Stage window;
public static int fps = 60;
public static int frame = 0;
public static Camera camera;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
group = new Group();
scene = new Scene(group, sceneWidth, sceneHeight, true);
window = primaryStage;
window.setScene(scene);
window.setTitle(PROGRAM_TITLE);
window.show();
Cube.makeCube();
cameraStuff();
}
public static void cameraStuff() {
camera = new PerspectiveCamera();
scene.setCamera(camera);
FxTimer.runPeriodically(Duration.ofMillis((long) (1000 / 60)), () -> {
for (int i = 0; i < Cube.rect.length; i++) {
Cube.rect[i].setTranslateX(frame);
Cube.rect[i].setTranslateY(frame);
Cube.rect[i].setTranslateZ(frame);
}
frame++;
});
}
}
And this is the second class:
package com.vinayak.trianglemesh.experiment;
import javafx.geometry.Point3D;
import javafx.scene.paint.Color;
import javafx.scene.shape.Box;
import javafx.scene.shape.Rectangle;
public class Cube extends CoreCube {
public static Rectangle rect[];
public static void makeCube() {
rect = new Rectangle[6];
rect[0] = new Rectangle(0, 0, 200, 200);
rect[0].setFill(Color.CORAL);
rect[0].setTranslateZ(-100);
rect[0].setRotationAxis(new Point3D(0.0, 0.0, 0.0));
rect[0].setRotate(0); …
Vin vin 45 Junior Poster in Training
I made a mistake in the discussion.
I want each texure on each side of the cube, not 6 textures on each side of the cube.
I am really sorry.
rproffitt commented: "Good news everyone!" It appears there are many priors to explore with 1 texture per side! (Zapp Brannigan!) +15
Vin vin 45 Junior Poster in Training
How do I make a cube in javafx that have 6 different textures on each side of the cube?
The only way I know how to solve this, is by making 6 imageviews and give them each a specific coordinates that make them look like a cube.
Is there a other way to do this?
Like, I like to give the cube later on lighting, and some rotations. It will be more complex if I use this method.
Btw, I heard about Triangle Mesh, but I don't know how TextureCoords works.
handy links:
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/TriangleMesh.html
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Box.html
Vin vin 45 Junior Poster in Training
this might help me:
https://stackoverflow.com/questions/18580712/opentk-opengl-texture-not-drawing
btw, I am going to use the newest version of opengl.
I am not going to use the "using OpenTK.Graphics.OpenGL;", but the "using OpenTK.Graphics.OpenGL4;".
Vin vin 45 Junior Poster in Training
an extra tutorial that I read about it is:
http://neokabuto.blogspot.com/p/tutorials.html
https://github.com/neokabuto/OpenTKTutorialContent
Vin vin 45 Junior Poster in Training
I worked about 2 weeks with OpenTK and I like to know how to texture triangles.
I like to give my program, the flexibility to make shapes like triangles, cubes, spheres and to give them different textures (or shaders).
I like to give my window a icon, but it says that I have this error:
"Exception Unhandled, System.ArgumentException: 'Argument 'picture' must be a picture that can be used as a icon.'".
This exception was originally thrown at this call stack:
System.Drawing.Icon.Initialize(int, int)
System.Drawing.Icon.Icon(string, int, int)
System.Drawing.Icon.Icon(string)
obamaExperimentingV1_14.ObamaBattlezCSharp.Main(string[]) in Program.cs
I am really thankfull if someone helps me or ask some questions at me or critisize my code, I know this is some multiple questions at one post.
But again, I am really thankfull for this.
Here are some tutorials (,but I can't really understand it fully):
https://github.com/opentk/LearnOpenTK
https://github.com/giawa/opengl4tutorials
https://www.youtube.com/playlist?list=PLWzp0Bbyy_3ggUK3XZjBmwnSjhbhJH3kp
https://github.com/eowind/dreamstatecoding
http://dreamstatecoding.blogspot.com/2017/01/opengl-4-with-opentk-in-c-part-1.html
https://opentk.net/learn/index.html
http://www.siltutorials.com/opentkbasicsindex
here are the things that I don't know what it is, here is a list what I don't know about:
- buffer;
- vector;
- matrix;
- buffertarget;
- bindbuffer;
- bufferdata;
- indices;
- clearbuffermask;
- colorbufferbit.
Here is a full code, there are just 2 classes in my project.
Program.cs
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System;
using System.Drawing;
namespace obamaExperimentingV1_14
{
public class ObamaBattlezCSharp : GameWindow
{
private static void Main(string[] args)
{
using (var window = new Window(1280, 720, GraphicsMode.Default, "Obama Battlez CSharp Version", GameWindowFlags.Default, DisplayDevice.Default)) {
window.Icon = new Icon("Resources/bruhstar (1).bmp");
window.Run(60.0f);
}
}
}
}
Window.cs
… Vin vin 45 Junior Poster in Training
btw, if someones knows how to change the icon of the window, then tell me how, I am really curious how to set the icon for the window.
Vin vin 45 Junior Poster in Training
Here is my full code, I have 2 classes in my namespace
Program.cs
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System;
using System.Drawing;
namespace obamaExperimentingV1_14
{
public class ObamaBattlezCSharp : GameWindow
{
private static void Main(string[] args)
{
using (var window = new Window(1280, 720, GraphicsMode.Default, "Obama Battlez CSharp Version", GameWindowFlags.Default, DisplayDevice.Default)) {
//window.Icon = new Icon("Resources/bruhstar (1).bmp");
window.Run(60.0f);
}
}
}
}
Window.cs
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System;
using System.Drawing;
namespace obamaExperimentingV1_14
{
class Window : GameWindow
{
public Window(int width, int height, GraphicsMode graphicsMode, string title, GameWindowFlags gameWindowFlags, DisplayDevice displayDevice)
: base(width, height, GraphicsMode.Default, title, GameWindowFlags.Default, displayDevice)
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(0.5f, 0.6f, 0.7f, 1.0f);
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
var input = Keyboard.GetState();
if (input.IsKeyDown(Key.Escape)) {
Exit();
}
base.OnUpdateFrame(e);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.Color4(0.4, 0.6, 0.1, 1.0);
GL.Begin(PrimitiveType.Triangles);
GL.Vertex2(0, 0);
GL.Vertex2(1, 0);
GL.Vertex2(0, 1);
GL.End();
GL.Color4(0.4, 0.2, 0.1, 1.0);
GL.Begin(PrimitiveType.Triangles);
GL.Vertex2(0, 0);
GL.Vertex2(-1, 0);
GL.Vertex2(0, -1);
GL.End();
SwapBuffers();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
GL.Viewport(0, 0, Width, Height);
}
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
}
}
}
Vin vin 45 Junior Poster in Training
I am now learning OpenTK. I have some trouble learning how buffers, vectors and how to make more than 2 3d shapes.
and if I try to set the texture or shaders, it will change all the shapes to the same textures and shaders.
I have read a lot of tutorials.
here are some list of tutorials that I have seen and read about it:
https://github.com/opentk/LearnOpenTK
https://github.com/giawa/opengl4tutorials
https://www.youtube.com/playlist?list=PLWzp0Bbyy_3ggUK3XZjBmwnSjhbhJH3kp
https://github.com/eowind/dreamstatecoding
http://dreamstatecoding.blogspot.com/2017/01/opengl-4-with-opentk-in-c-part-1.html
https://opentk.net/learn/index.html
http://www.siltutorials.com/opentkbasicsindex
how can I give these 2 triangles different shaders or/and textures.
```c#
GL.Color4(0.4, 0.6, 0.1, 1.0);
GL.Begin(PrimitiveType.Triangles);
GL.Vertex2(0, 0);
GL.Vertex2(1, 0);
GL.Vertex2(0, 1);
GL.End();
GL.Color4(0.4, 0.2, 0.1, 1.0);
GL.Begin(PrimitiveType.Triangles);
GL.Vertex2(0, 0);
GL.Vertex2(-1, 0);
GL.Vertex2(0, -1);
GL.End();
```
Vin vin 45 Junior Poster in Training
How can I minify my code in here, I am giving each 23 total imageview a eventhandler.
code works, but I like to make the code shorter. I tried to use for loops to shorten the code.
here is a example what I tried to do:
for (int i = 0 ; i < imgArrayList.size() ; i++) {
imgArrayList.get(i).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
selectedTile = i;
}
});
}
but it can't because, the "i" inside the handle method is final. I can't pass the "i" inside the handle method.
my goal is, that a specific imageview, that has for example ".get(3)", that needs to print out 3.
so if the imageview have a .get(i), it needs to print out the exact same number of i.
btw, the imageviews are stored in a arraylist, if you are curious where the ".get" comes from.
protected static void addImageViewMouseEvents() {
UI_SelectionMenu.imgArrayList.get(0).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
UI_SelectionMenu.selectedTile = 0;
System.out.println(UI_SelectionMenu.selectedTile);
}
});
UI_SelectionMenu.imgArrayList.get(1).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
UI_SelectionMenu.selectedTile = 1;
System.out.println(UI_SelectionMenu.selectedTile);
}
});
UI_SelectionMenu.imgArrayList.get(2).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
UI_SelectionMenu.selectedTile = 2;
System.out.println(UI_SelectionMenu.selectedTile);
}
});
UI_SelectionMenu.imgArrayList.get(3).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
UI_SelectionMenu.selectedTile = 3;
System.out.println(UI_SelectionMenu.selectedTile);
}
});
UI_SelectionMenu.imgArrayList.get(4).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
UI_SelectionMenu.selectedTile = 4;
System.out.println(UI_SelectionMenu.selectedTile);
}
});
UI_SelectionMenu.imgArrayList.get(5).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
UI_SelectionMenu.selectedTile = 5;
System.out.println(UI_SelectionMenu.selectedTile);
}
});
UI_SelectionMenu.imgArrayList.get(6).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void …
Vin vin 45 Junior Poster in Training
example of how to use Math.PI:
/
System.out.println(5 Math.PI);
*/
you don't even need to import the java.lang.Math.* thing.
Vin vin 45 Junior Poster in Training
nvm, my keys already are smooth enough XD
I am dumb, this is a dumb post, I facepalmed myself.
rproffitt commented: Smooth. Don't laugh. Optimizing too much is a thing. I was going to comment about something then I see you already did it so you're fine. Good going. +15
Vin vin 45 Junior Poster in Training
yes, that is the problem
Vin vin 45 Junior Poster in Training
//here is my code
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class SmoothKeyEvents extends Application {
public final static String TITLE = "simple key-input program";
public final static short WIDTH = 1280;
public final static short HEIGHT = 720;
private Stage window;
private Scene mainS;
private Group groupS;
private Rectangle rect;
private int x = 50;
private int y = 50;
private int velX = 0;
private int velY = 0;
@Override
public void start(Stage primaryStage) {
groupS = new Group();
mainS = new Scene(groupS, WIDTH, HEIGHT, Color.DARKBLUE);
window = primaryStage;
window.setScene(mainS);
window.setTitle(TITLE);
window.show();
window.requestFocus();
rect = new Rectangle();
rect.setFill(Color.MEDIUMSPRINGGREEN);
rect.setX(x);
rect.setY(y);
rect.setWidth(150);
rect.setHeight(150);
groupS.getChildren().add(rect);
mainS.setOnKeyPressed(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent ke) {
if(ke.getCode() == KeyCode.A) {
setVelX(-9);
}
if(ke.getCode() == KeyCode.S) {
setVelY(9);
}
if(ke.getCode() == KeyCode.D) {
setVelX(9);
}
if(ke.getCode() == KeyCode.W) {
setVelY(-9);
}
}
});
mainS.setOnKeyReleased(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent ke) {
if(ke.getCode() == KeyCode.A) {
setVelX(0);
}
if(ke.getCode() == KeyCode.S) {
setVelY(0);
}
if(ke.getCode() == KeyCode.D) {
setVelX(0);
}
if(ke.getCode() == KeyCode.W) {
setVelY(0);
}
}
});
final AnimationTimer at = new AnimationTimer() {
@Override
public void handle(long arg0) {
x += velX;
rect.setX(x);
y += velY;
rect.setY(y);
}
};
at.start();
}
public void setVelX(int velX) {
this.velX = velX;
}
public void setVelY(int velY) {
this.velY = velY;
}
public void stop() {
System.out.println("program exited");
}
public static void main(String[] args) {
launch(args);
}
}
Vin vin 45 Junior Poster in Training
I am making a ping pong game using JavaFX, but before I am going to make that, I like to test the KeyEvents.
I made a simple ping pong game about 2 years ago, but I like to improve my game.
And was pretty bad controlls back when I made the ping pong game, because of the not smooth controls.
so here is the problem:
whenever I holdpress A and switch quickly to holdpress D or D to A, same with (W to S and S to W).
it just takes a little break and then move, is there something I can change, improve?
I don't know why it does this.
Vin vin 45 Junior Poster in Training
how can I make it that another client can join in?
like there is 2 clients and 1 server.
do I just copy the clients code?
Vin vin 45 Junior Poster in Training
Nailed it :D
package com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v_aa;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class AWF_FX_v5 extends Application implements AudioSpectrumListener {
public final static String TITLE = "AWF special program";
public final static short WIDTH = 1280;
public final static short HEIGHT = 720;
public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
public static String AUDIO_FILE1 = "../mp3Music/AcerDroid -Skies.mp3";
public static String AUDIO_FILE2 = "../mp3Music/Capturez - Patterns.mp3";
public static String AUDIO_FILE3 = "../mp3Music/Kanooli - Instinct.mp3";
public static String AUDIO_FILE4 = "../mp3Music/RavenKreyn_InTheAir_NCSrelease.mp3";
public static String AUDIO_URI = AWF_FX_v1.class.getResource(AUDIO_FILE1).toExternalForm();
private Stage window;
private Scene mainS;
private Group groupS;
private MediaPlayer audioMP;
private float[] cMag;
private short power = 5;
private Rectangle[] rect = new Rectangle[40];
@Override
public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
for (int i = 0; i < magnitudes.length; i++) {
cMag = magnitudes;
cMag[i] = (int) cMag[i] * power;
System.out.println("cMag (" + i + "): " + cMag[i]);
}
for(int i = 0; i < rect.length; i++) {
rect[i].setY(500 + ((cMag[i] + (60 * power)) * -1));
rect[i].setHeight(50 + (cMag[i] + (60 * power)));
}
}
@Override
public void start(Stage primaryStage) {
groupS = new Group();
mainS = new Scene(groupS, WIDTH, HEIGHT, Color.DARKBLUE);
window = primaryStage;
window.setScene(mainS);
window.setTitle(TITLE);
window.show();
Media audioMedia = new Media(AUDIO_URI);
audioMP = new MediaPlayer(audioMedia);
audioMP.play();
audioMP.setCycleCount(MediaPlayer.INDEFINITE);
audioMP.setVolume(0.02);
audioMP.setAudioSpectrumInterval(0.01);
audioMP.setAudioSpectrumNumBands(40);
audioMP.setAudioSpectrumListener(this);
for(int i = 0; i < rect.length; i++) {
rect[i] = new Rectangle();
rect[i].setX(250 + (i * 20));
rect[i].setY(500);
rect[i].setWidth(10);
rect[i].setHeight(50); …
Vin vin 45 Junior Poster in Training
code without that comments lines:
package com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v_aa;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class AWF_FX_v1 extends Application implements AudioSpectrumListener {
public final static String TITLE = "AWF special program";
public final static short WIDTH = 1280;
public final static short HEIGHT = 720;
public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
public static String AUDIO_FILE1 = "../mp3Music/AcerDroid -Skies.mp3";
public static String AUDIO_FILE2 = "../mp3Music/Capturez - Patterns.mp3";
public static String AUDIO_FILE3 = "../mp3Music/Kanooli - Instinct.mp3";
public static String AUDIO_FILE4 = "../mp3Music/RavenKreyn_InTheAir_NCSrelease.mp3";
public static String AUDIO_URI = AWF_FX_v1.class.getResource(AUDIO_FILE2).toExternalForm();
private Stage window;
private Scene mainS;
private Group groupS;
private MediaPlayer audioMP;
private float[] cMag;
private Rectangle[] rect = new Rectangle[8];
@Override
public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
for (int i = 0; i < magnitudes.length; i++) {
cMag = magnitudes;
cMag[i] = (int) cMag[i];
}
for(int i = 0; i < rect.length; i++) {
rect[i].setY(310 + ((cMag[i] + 60) * -1));
rect[i].setHeight(50 + (cMag[i] + 60));
System.out.println(i + " setY: " + rect[i].getY());
System.out.println(i + " setHeight " + rect[i].getHeight());
}
}
@Override
public void start(Stage primaryStage) {
groupS = new Group();
mainS = new Scene(groupS, WIDTH, HEIGHT, Color.DARKBLUE);
window = primaryStage;
window.setScene(mainS);
window.setTitle(TITLE);
window.show();
Media audioMedia = new Media(AUDIO_URI);
audioMP = new MediaPlayer(audioMedia);
audioMP.play();
audioMP.setCycleCount(MediaPlayer.INDEFINITE);
audioMP.setVolume(0.02);
audioMP.setAudioSpectrumInterval(0.01);
audioMP.setAudioSpectrumNumBands(8);
audioMP.setAudioSpectrumListener(this);
rect[0] = new Rectangle(250, HEIGHT / 2 - 50, 50, 50);
rect[0].setFill(Color.AZURE);
rect[1] = new Rectangle(350, HEIGHT / 2 - 50, 50, 50);
rect[1].setFill(Color.CORNFLOWERBLUE);
rect[2] …
Vin vin 45 Junior Poster in Training
This is what I right now have:
Right now I am facing how to make these rectangles make more reactive like they change about 60 pixels maximal higher and I want to make it more than 60 pixels. Now I don't have a question this is just an update for my code.
package com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v_aa;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class AWF_FX_v1 extends Application implements AudioSpectrumListener {
public final static String TITLE = "AWF special program";
public final static short WIDTH = 1280;
public final static short HEIGHT = 720;
public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
public static String AUDIO_FILE1 = "../mp3Music/AcerDroid -Skies.mp3";
public static String AUDIO_FILE2 = "../mp3Music/Capturez - Patterns.mp3";
public static String AUDIO_FILE3 = "../mp3Music/Kanooli - Instinct.mp3";
public static String AUDIO_FILE4 = "../mp3Music/RavenKreyn_InTheAir_NCSrelease.mp3";
public static String AUDIO_URI = AWF_FX_v1.class.getResource(AUDIO_FILE2).toExternalForm();
private Stage window;
private Scene mainS;
private Group groupS;
private MediaPlayer audioMP;
private float[] cMag;
// private Rectangle rect0;
// private Rectangle rect1;
// private Rectangle rect2;
// private Rectangle rect3;
// private Rectangle rect4;
// private Rectangle rect5;
// private Rectangle rect6;
// private Rectangle rect7;
private Rectangle[] rect = new Rectangle[8];
@Override
public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
for (int i = 0; i < magnitudes.length; i++) {
cMag = magnitudes;
cMag[i] = (int) cMag[i];
//System.out.println((i + 1) + ": " + (cMag[i] * -1));
}
for(int i = 0; i < rect.length; i++) {
rect[i].setY(310 + …
Vin vin 45 Junior Poster in Training
that v7 -> v1 is not accidental. I just made a new package in v7.
if I placed the class just in v7, the error is gone and the music can play, but if the class is inside of v7.v1 then I get that error.
Vin vin 45 Junior Poster in Training
I got a error from this: "public static String AUDIO_URI = AWF_Core.class.getResource(AUDIO_FILE).toExternalForm();"
I made a new class, so the code looks more cleaner and readable.
but unfortunatly I don't know what happened?
I got a error:
Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$2(LauncherImpl.java:352)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v7.v1.AWF_Core.<clinit>(AWF_Core.java:18)
... 11 more
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.NullPointerException
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:383)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
... 5 more
this is how my project folders looks like in the navigator.
here is my new code:
package com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v7.v1;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class AWF_Core extends Application implements AudioSpectrumListener {
public final static String TITLE = "AWF special program";
public final static short WIDTH = 1280;
public final static short HEIGHT = 720;
public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
public static String AUDIO_URI = AWF_Core.class.getResource(AUDIO_FILE).toExternalForm();
private Stage window;
private Scene mainS;
private Group groupS;
private static MediaPlayer audioMP;
@Override
public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
System.out.println("data updated");
displayMag(magnitudes);
}
public void displayMag(float[] mag) {
for(int i = 0; i < mag.length; i++) {
System.out.println(i+": " + mag[i]);
}
System.out.println("length of mag = " + mag.length);
}
public void init() { …
Vin vin 45 Junior Poster in Training
this is what the console says:
data updated
0: -59.407444
1: -48.37642
2: -41.535774
3: -40.149982
4: -44.27457
5: -44.982887
6: -48.725376
7: -52.21941
8: -54.77891
9: -50.22617
10: -53.42569
11: -55.172703
12: -55.079517
13: -53.60956
14: -53.745964
15: -57.46172
16: -57.445312
17: -57.044395
18: -52.888622
19: -54.909782
20: -57.15198
21: -56.71712
22: -56.429665
23: -57.09065
24: -57.157146
25: -57.19809
26: -57.740284
27: -56.86342
28: -56.226715
29: -57.350414
30: -57.268696
31: -57.377327
32: -57.740887
33: -56.40759
34: -55.88547
35: -56.44446
36: -57.326187
37: -57.204216
38: -55.604618
39: -55.611687
40: -56.671505
41: -55.99083
42: -54.81242
43: -54.81779
44: -55.155308
45: -54.862213
46: -56.785095
47: -57.21616
48: -56.16005
49: -56.611744
50: -57.336994
51: -56.80366
52: -56.719086
53: -56.731297
54: -57.696148
55: -56.97476
56: -57.19966
57: -56.635822
58: -56.079693
59: -56.365444
60: -56.58281
61: -57.3104
62: -57.90639
63: -57.26793
64: -57.42998
65: -58.106003
66: -56.51674
67: -55.527695
68: -55.31978
69: -56.59834
70: -57.75076
71: -57.570744
72: -55.88267
73: -55.103874
74: -56.05077
75: -56.008892
76: -55.499
77: -56.47118
78: -56.83968
79: -57.00147
80: -56.647213
81: -56.488712
82: -55.836987
83: -56.97192
84: -57.72063
85: -58.830986
86: -58.433346
87: -57.937386
88: -58.045086
89: -57.081215
90: -56.94066
91: -57.460472
92: -57.537506
93: -57.83281
94: -58.671604
95: -58.726414
96: -58.39954
97: -57.964382
98: -59.422413
99: -58.608257
100: -56.711
101: -57.214962
102: -58.341293
103: -58.0045
104: -58.1659
105: -57.890232
106: -57.620907
107: -58.689537
108: -58.754578
109: -58.455357
110: -58.3346
111: -57.839603
112: -58.051525
113: -58.993805
114: -59.581917
115: -59.176373
116: -58.17461
117: -59.15124
118: -59.001194
119: -58.830784
120: -58.5519 …
Vin vin 45 Junior Poster in Training
yes! Now I can finally use the values :D
this is my code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class AWF_Main extends Application implements AudioSpectrumListener {
public final static String TITLE = "AWF special program";
public final static short WIDTH = 1280;
public final static short HEIGHT = 720;
public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
public static String AUDIO_URI = AWF_Main.class.getResource(AUDIO_FILE).toExternalForm();
private Stage window;
private Scene mainS;
private Group groupS;
private static MediaPlayer audioMP;
@Override
public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
System.out.println("data updated");
displayMag(magnitudes);
for(int i = 0; i < magnitudes.length; i++) {
//correctedMagnitude[i] = magnitudes[i] - audioMP.getAudioSpectrumThreshold();
// System.out.println("0"+magnitudes[0]);
// System.out.println("1"+magnitudes[1]);
// System.out.println("2"+magnitudes[2]);
// System.out.println("3"+magnitudes[3]);
// System.out.println("4"+magnitudes[4]);
// System.out.println("5"+magnitudes[5]);
// System.out.println("6"+magnitudes[6]);
// System.out.println("7"+magnitudes[7]);
// System.out.println("8"+magnitudes[8]);
// System.out.println("9"+magnitudes[9]);
//System.out.println(correctedMagnitude[i]);
}
}
public void displayMag(float[] mag) {
for(int i = 0; i < mag.length; i++) {
System.out.println(i+": " + mag[i]);
}
System.out.println("length of mag = " + mag.length);
}
public void init() {
Media audioMedia = new Media(AUDIO_URI);
audioMP = new MediaPlayer(audioMedia);
play();
}
@Override
public void start(Stage primaryStage) throws Exception {
groupS = new Group();
mainS = new Scene(groupS, WIDTH/2, HEIGHT/2, Color.DEEPPINK);
window = primaryStage;
window.setScene(mainS);
window.setTitle(TITLE);
window.show();
}
//audioMP.setVolume(0.02); is low, because I just like to listen in low volume while I listen to other music. (:
public void play() {
audioMP.play();
audioMP.setVolume(0.02);
audioMP.setAudioSpectrumInterval(0.5);
audioMP.setAudioSpectrumNumBands(256);
audioMP.setAudioSpectrumListener(this);
//AWF_newClass.printOutMagnitudes();
}
public void stop() {
System.out.println("program exited");
}
public static void main(String[] args) …
Vin vin 45 Junior Poster in Training
at com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v1.AWF_Main.start(AWF_Main.java:52) = System.out.println(correctedMagnitude[5]);
Vin vin 45 Junior Poster in Training
btw, this is a link too:
jnlps://download.oracle.com/otndocs/products/javafx/8/samples/Ensemble/Ensemble.jnlp
there is a audiobarchart.
Vin vin 45 Junior Poster in Training
I just want to System.out.println(magnitudes[i]);
in the start method, but how do I even do that?
I tried this:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class AWF_Main extends Application implements AudioSpectrumListener {
public final static String TITLE = "AWF special program";
public final static short WIDTH = 1280;
public final static short HEIGHT = 720;
public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
public static String AUDIO_URI = AWF_Main.class.getResource(AUDIO_FILE).toExternalForm();
private Stage window;
private Scene mainS;
private Group groupS;
private AudioSpectrumListener asl;
private static MediaPlayer audioMP;
private float[] correctedMagnitude;
@Override
public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
for(int i = 0; i < magnitudes.length; i++) {
correctedMagnitude[i] = magnitudes[i] - audioMP.getAudioSpectrumThreshold();
//System.out.println(correctedMagnitude[i]);
}
}
@Override
public void start(Stage primaryStage) throws Exception {
groupS = new Group();
mainS = new Scene(groupS, WIDTH, HEIGHT, Color.DEEPPINK);
window = primaryStage;
window.setScene(mainS);
window.setTitle(TITLE);
window.show();
// setup media
Media audioMedia = new Media(AUDIO_URI);
audioMP = new MediaPlayer(audioMedia);
audioMP.setAudioSpectrumListener(this);
play();
//correctedMagnitude[5], why 5, because I chosed a random number.
System.out.println(correctedMagnitude[5]);
}
//audioMP.setVolume(0.02); is low, because I just like to listen in low volume while I listen to other music. (:
public void play() {
audioMP.play();
audioMP.setVolume(0.02);
audioMP.setAudioSpectrumInterval(60.0);
audioMP.setAudioSpectrumNumBands(256);
}
public void stop() {
System.out.println("program exited");
}
public static void main(String[] args) {
launch(args);
}
}
I don't want to use charts.
here is my Error:
Exception in Application start method
program exited
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at …
Vin vin 45 Junior Poster in Training
I just need the magnitudes to be accesable from the whole class.
(btw, I am going to remove animationtimer
Vin vin 45 Junior Poster in Training
So I am making a responsive audio waveform, if you don't know what that is, it is something like this: (btw this is my goal to make those audio waveforms).
https://i.ytimg.com/vi/J7uN-SQtOYI/maxresdefault.jpg
https://www.youtube.com/watch?v=OVMuwa-HRCQ
https://www.youtube.com/watch?v=SCD2tB1qILc
I don't know how to describe it, so just look at the videos and image.
but my question is, how do I get the values from the "AudioSpectrumListener", it has a lambda expression.
And I need the value of the magnitude. So I can print out the values.
Later I will use the values to make rectangles and that rectangles will change height shape because of the changing value from the magnitude.
I looked at some examples and tutorials, here are some:
jnlps://download.oracle.com/otndocs/products/javafx/8/samples/Ensemble/Ensemble.jnlp
https://victor-fx.blogspot.com/2017/08/audio-visualization-in-javafx.html
as you can see, I used animation timer, because the "AudioSpectrumInterval" is on 60, that means it updates 60 times in a second.
I can use a for a loop too, but it will print out the same values about a couple of times, or even more than that.
Vin vin 45 Junior Poster in Training
I was kinda stuck, but I finally got it. I just forgot that the thread starts first then the initialize.
it is now fixed. Here is the complete code:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Server extends Application implements Runnable {
private Group serverGR;
private Scene serverSC;
private Stage window;
private TextField tf;
private TextArea ta;
private ServerSocket ss;
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
public void init() {
}
@Override
public void start(Stage primaryStage) {
try {
serverGR = new Group();
serverSC = new Scene(serverGR, 800, 500, Color.LIGHTCYAN);
window = primaryStage;
window.setTitle("Bleidorb Simplistic ChatProgram \\\\\\\\\"ServerMode\"\\\\\\\\");
window.setScene(serverSC);
window.show();
tf = new TextField();
tf.setTranslateX(10);
tf.setTranslateY(10);
tf.setPrefWidth(400);
tf.setPrefHeight(20);
tf.appendText("Type your message here.");
tf.setOnAction(event -> {
String server = "Server: ";
server += tf.getText();
try {
dos.writeUTF(server);
ta.appendText(server + "\n");
// ta.appendText(dis.readUTF() + "\n");
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
tf.clear();
});
ta = new TextArea();
ta.setTranslateX(10);
ta.setTranslateY(40);
ta.setPrefWidth(400);
ta.setPrefHeight(400);
serverGR.getChildren().addAll(tf, ta);
try {
new Thread(this).start();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.println(e.toString() + "\n something wrong in startmethod, lol?");
}
}
@SuppressWarnings("deprecation")
public void stop() {
try {
new Thread(this).stop();
dis.close();
ss.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
@Override
public void run() {
try {
ss = new ServerSocket(5279, 100);
s = ss.accept();
s.setTcpNoDelay(true);
dis = new DataInputStream(s.getInputStream());
dos = …
Vin vin 45 Junior Poster in Training
when I type something in the server and press enter in the textfield, it displayed in the server program, but not in the client program.
when I type something in the client and press enter in the textfield, it displayed in the server and client program, this one works fine.
server -> client (message not displayed on client, message displayed only on server)
client -> server (message displayed on both programs)
Vin vin 45 Junior Poster in Training
so I run the Server Program then the Client Program and when I type something in the server textfield and press enter, I get a error inside the Client Class.
client test input
java.lang.NullPointerException
at com.VINSTORM.bleidorb.ZcommunicationTestServerV4.main.Client.run(Client.java:104)
at java.lang.Thread.run(Unknown Source)
Client.java:104 =
ta.appendText(dis.readUTF() + "\n");
Vin vin 45 Junior Poster in Training
So I am really interested in making a database, I have knowledge about html and css, not javascript and I just want to begin learning and making a database.
I am going to use this website: https://www.000webhost.com/?utm_source=000webhostapp&utm_campaign=000_logo&utm_medium=website&utm_content=footer_img
so you can see my website in the internet, I don't really expect a lot of people are going to my website, like more than 20 people in a day, but here is the question or discussion?
how and where do I begin learn about databases, I don't really care what language I am going to use, like sql or php, is sql even a language? I don't know.
I just need a ton of information (:
btw, do I need to host a sql server or something, I am not going to pay anything with money.
I already downloaded xampp, I don't know if this going to help me.
Vin vin 45 Junior Poster in Training
much better code here
I just don't see the server message in the client program
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Server extends Application implements Runnable {
private Group serverGR;
private Scene serverSC;
private Stage window;
private TextField tf;
private TextArea ta;
private ServerSocket ss;
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
public void init() {
new Thread(this).start();
}
@Override
public void start(Stage primaryStage) {
try {
serverGR = new Group();
serverSC = new Scene(serverGR, 800, 500, Color.LIGHTCYAN);
window = primaryStage;
window.setTitle("Bleidorb Simplistic ChatProgram \\\\\\\\\"ServerMode\"\\\\\\\\");
window.setScene(serverSC);
window.show();
tf = new TextField();
tf.setTranslateX(10);
tf.setTranslateY(10);
tf.setPrefWidth(400);
tf.setPrefHeight(20);
tf.appendText("Type your message here.");
tf.setOnAction(event -> {
String server = "Server: ";
server += tf.getText();
try {
dos.writeUTF(server);
ta.appendText(server + "\n");
// ta.appendText(dis.readUTF() + "\n");
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
tf.clear();
});
ta = new TextArea();
ta.setTranslateX(10);
ta.setTranslateY(40);
ta.setPrefWidth(400);
ta.setPrefHeight(400);
serverGR.getChildren().addAll(tf, ta);
} catch (Exception e) {
System.out.println(e.toString() + "\n something wrong in startmethod, lol?");
}
}
@SuppressWarnings("deprecation")
public void stop() {
try {
new Thread(this).stop();
dis.close();
ss.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
@Override
public void run() {
try {
ss = new ServerSocket(5279, 100);
s = ss.accept();
s.setTcpNoDelay(true);
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
for (;;) {
System.out.println("server test input");
ta.appendText(dis.readUTF() + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.DataInputStream; …
Vin vin 45 Junior Poster in Training
ignore the code above please, I forgot that I can use one class instead of two, just for the "implements runnable".
let me simplify this code.
Vin vin 45 Junior Poster in Training
the client does not get the server message, what went wrong?
btw I used the "implements Runnable" (:
I don't know how to fully use lambdas, I read this: https://www.freecodecamp.org/news/learn-these-4-things-and-working-with-lambda-expressions-b0ab36e0fffc/
rn, I am not really focusing on lambdas, it's nice to know, I will make a seperate project to test it out, but there is something wrong with the client input, (that gets the server messages from the server) ,or the server output, (that sends the server messages to the client). I don't know if for loop is making some problems, I just have a feeling of it, but I don't know why?
btw here is my code:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Server extends Application {
private Group serverGR;
private Scene serverSC;
private Stage window;
private TextField tf;
private TextArea ta;
private ServerSocket ss;
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
ConnectionThread cT = new ConnectionThread();
public void init() {
new Thread(new ConnectionThread()).start();
}
@Override
public void start(Stage primaryStage) {
try {
serverGR = new Group();
serverSC = new Scene(serverGR, 800, 500, Color.LIGHTCYAN);
window = primaryStage;
window.setTitle("Bleidorb Simplistic ChatProgram \\\\\\\\\"ServerMode\"\\\\\\\\");
window.setScene(serverSC);
window.show();
tf = new TextField();
tf.setTranslateX(10);
tf.setTranslateY(10);
tf.setPrefWidth(400);
tf.setPrefHeight(20);
tf.appendText("Type your message here.");
tf.setOnAction(event -> {
String server = "Server: ";
server += tf.getText();
try {
dos.writeUTF(server);
ta.appendText(server + "\n");
// ta.appendText(dis.readUTF() + "\n");
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
tf.clear();
}); …
Vin vin 45 Junior Poster in Training
So, I made a simple server-client chat program, but I have a problem:
if the server tries to message the client, the server lags or freezes a bit, same with the client.
is there something that I do wrong?
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Server extends Application {
private Group serverGR;
private Scene serverSC;
private Stage window;
private TextField tf;
private TextArea ta;
private ServerSocket ss;
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
ConnectionThread cT = new ConnectionThread();
public void init() {
cT.start();
}
@Override
public void start(Stage primaryStage) {
try {
serverGR = new Group();
serverSC = new Scene(serverGR, 800, 500, Color.LIGHTCYAN);
window = primaryStage;
window.setTitle("Bleidorb Simplistic ChatProgram \\\\\\\\\"ServerMode\"\\\\\\\\");
window.setScene(serverSC);
window.show();
tf = new TextField();
tf.setTranslateX(10);
tf.setTranslateY(10);
tf.setPrefWidth(400);
tf.setPrefHeight(20);
tf.appendText("Type your message here.");
tf.setOnAction(event -> {
String server = "Server: ";
server += tf.getText();
try {
dos.writeUTF(server);
dos.flush();
ta.appendText(dis.readUTF() + "\n");
} catch (IOException e) {
e.printStackTrace();
}
tf.clear();
});
ta = new TextArea();
ta.setTranslateX(10);
ta.setTranslateY(40);
ta.setPrefWidth(400);
ta.setPrefHeight(400);
serverGR.getChildren().addAll(tf, ta);
} catch (Exception e) {
System.out.println(e.toString() + "\n something wrong in startmethod, lol?");
}
}
@SuppressWarnings("deprecation")
public void stop() {
try {
cT.stop();
dis.close();
ss.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
private class ConnectionThread extends Thread {
@Override
public void run(){
try {
ss = new ServerSocket(5279, 100);
s = ss.accept();
s.setTcpNoDelay(true);
dis = new …
Vin vin 45 Junior Poster in Training
I somehow got this, I made a new class and I extended to Thread,
I did then this, I don't know what this is called:
but like so, "ServerInnerClass sic = new ServerInnerClass();"
then I started the thread, by doing this. sic.start();
in the ServerInnerClass, I made the run method.
And I placed the socket.accept in there.
Does this stop from lagging? freezing? IDK what it is called, but this fixed it.
of course, I know a little bit of threading (:
and I need to learn more about it.
I will finish my server-client program thing. And I might post a minimalistic example
of this answer.
source link that helped me: http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html
Vin vin 45 Junior Poster in Training
rproffitt commented: After all this, I worry you are not getting it. If you have a blocking call, it appears to freeze, but is NOT frozen. +15
Vin vin 45 Junior Poster in Training
//I forgot to put this in my code:
//"server = new ServerSocket(6789, 100);"
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ServerChatProgram extends Application{
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private static Socket connection;
private Stage window;
private Scene scene;
private Group root;
private TextField inputMessages;
private TextArea viewsMessages;
private void startRunning() {
try {
server = new ServerSocket(6789, 100);
new AnimationTimer() {
@Override
public void handle(long now) {
try{
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Server ended the connection! ");
}catch (IOException IOException){
IOException.printStackTrace();
}finally{
closeCrap();
}
}
}.start();
}catch(IOException ioException) {
ioException.printStackTrace();
}
}
private void waitForConnection() throws IOException{
showMessage(" Waiting for someone to connect... \n");
System.out.println("hello");
try {
connection = server.accept();
}catch (IOException e) {
e.printStackTrace();
}
showMessage(" Now connected to " + connection.getInetAddress().getHostName());
System.out.println(connection.getInetAddress().getHostName().toString());
}
private void setupStreams() throws IOException{}
private void whileChatting() throws IOException{}
private void closeCrap(){}
private void sendMessage(String message){}
private void showMessage(final String text){
Platform.runLater(() -> {
viewsMessages.appendText(text);
});
}
private void ableToType(final boolean tof){}
private void createContent() {
inputMessages = new TextField();
inputMessages.setTranslateX(10);
inputMessages.setTranslateY(10);
inputMessages.setPrefWidth(340);
inputMessages.setPrefHeight(20);
viewsMessages = new TextArea();
viewsMessages.setTranslateX(10);
viewsMessages.setTranslateY(50);
viewsMessages.setPrefWidth(340);
viewsMessages.setPrefHeight(300);
inputMessages.setOnAction(event -> {
String Message = "Server: ";
Message += inputMessages.getText();
inputMessages.clear();
viewsMessages.appendText(Message + "\n");
});
root = new Group(inputMessages, viewsMessages);
}
@Override
public void start(Stage primaryStage) throws Exception {
createContent();
scene = new Scene(root, 640, 360, Color.LIGHTCYAN);
window = primaryStage; …
Vin vin 45 Junior Poster in Training
https://stackoverflow.com/questions/9872356/java-socket-accept-freezes-ui
I got the:
new Runnable(){@Override public void run() {}}; idea from that site.
Vin vin 45 Junior Poster in Training
//I did this, but I saw nothing in the console, the waitForConnection() method does nothing ):
private void startRunning() {
try{
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Server ended the connection! ");
}catch (IOException IOException){
IOException.printStackTrace();
}finally{
closeCrap();
}
}
private void waitForConnection() throws IOException{
new Runnable(){
@Override
public void run() {
showMessage(" Waiting for someone to connect... \n");
System.out.println("hello");
try {
connection = server.accept();
} catch (IOException e) {
e.printStackTrace();
}
showMessage(" Now connected to " + connection.getInetAddress().getHostName());
System.out.println(connection.getInetAddress().getHostName().toString());
}};
}
Vin vin 45 Junior Poster in Training
//I don't know what I have done, but I did this.
//The freezing thing is not here anymore, because of the "new Runnable{ run method}" thing.
package com.VINSTORM.VinvinBleidorb_aka_BleidorbPerfect.chatprogramTest;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ServerChatProgram extends Application{
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private static Socket connection;
private Stage window;
private Scene scene;
private Group root;
private TextField inputMessages;
private TextArea viewsMessages;
private void startRunning() {
try{
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Server ended the connection! ");
}catch (IOException IOException){
IOException.printStackTrace();
}finally{
closeCrap();
}
}
private void waitForConnection() throws IOException{
new Runnable(){
@Override
public void run() {
showMessage(" Waiting for someone to connect... \n");
try {
connection = server.accept();
} catch (IOException e) {
e.printStackTrace();
}
showMessage(" Now connected to " + connection.getInetAddress().getHostName());
}};
}
private void setupStreams() throws IOException{}
private void whileChatting() throws IOException{}
private void closeCrap(){}
private void sendMessage(String message){}
private void showMessage(final String text){}
private void ableToType(final boolean tof){}
private void createContent() {
inputMessages = new TextField();
inputMessages.setTranslateX(10);
inputMessages.setTranslateY(10);
inputMessages.setPrefWidth(340);
inputMessages.setPrefHeight(20);
viewsMessages = new TextArea();
viewsMessages.setTranslateX(10);
viewsMessages.setTranslateY(50);
viewsMessages.setPrefWidth(340);
viewsMessages.setPrefHeight(300);
inputMessages.setOnAction(event -> {
String Message = "Server: ";
Message += inputMessages.getText();
inputMessages.clear();
viewsMessages.appendText(Message + "\n");
});
root = new Group(inputMessages, viewsMessages);
}
@Override
public void start(Stage primaryStage) throws Exception {
createContent();
scene = new Scene(root, 640, 360, Color.LIGHTCYAN);
window = primaryStage;
window.setScene(scene);
window.setTitle("Server \\\\ ChatProgram \\\\ Test");
window.show();
startRunning();
} …
Vin vin 45 Junior Poster in Training
This is a part of an example from a JavaFX (Server/Client) program
this is from Almas Baimagambetov
https://www.youtube.com/watch?v=VVUuo9VO2II
I have the code, but is there something I can get from there?
btw, I am new on this forum, it says "Curly braces { } may only be used when posting code.".
is there a way to share the example?
Vin vin 45 Junior Poster in Training
I put "connection = server.accept();" in a loop and the freeze stopped, but I get errors from "output.close();"
private void waitForConnection() throws IOException {
new AnimationTimer() {
@Override
public void handle(long now) {
showMessage(" Waiting for someone to connect... \n");
try {
connection = server.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
showMessage(" Now connected to " + connection.getInetAddress().getHostName());
}
}.start();
}