I put a string in a variabele and used it as text in a label. This works for shorter text, but my text is a few sentences long, so it ends with ... instead of finishing my string. I'm working with JavaFX 1.0. Does anyone know how i can fix this?
Edit:
This is the code I wrote. I put the variable HandleidingDobbelsteen in a stage in an other file.
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.control.Label;
package var DobbelsteenHandleiding = Label {
text: "Hier komt te staan wat de uitkomst van de dobbelsteen betekent."
translateX: 830
translateY: 245
width: 140
textWrap: true;
}
package var HandleidingDobbelsteen = Group {
content:[
DobbelsteenHandleiding,
Rectangle {
x: 825, y: 220
width: 150, height: 120
fill: null
stroke: Color.BLACK
strokeWidth: 1.3
},
Rectangle {
x: 825, y: 220
width: 150, height: 20
fill: Color.BLACK
},
Text {
font : Font {size: 18}
x: 845, y: 235
content: "De regels"
fill: Color.WHITE
},
]
}
I am going to assume that your instructor is mistaken on the version
of JavaFX they have you using. If they are correct, these instructions may not work for you (as I am not familiar with steam-powered Java)...
The likely cause of your issue is that the String being used to set the Label's textProperty is longer than the Label is able to display. Without seeing your actual code, I have a couple of suggestions:
First of all, make sure that your Label is wrapping its text:
label.setWrapText(true);
Secondly, you should make sure that the Label is wrapped in a layout container that can handle a node that grows (not always necessary, but recommended). My recommendation is a VBox:
VBox vBox = new VBox(label);
Again, there is some assumption here on the version of JavaFX you're actually using, but you could try this simple example to see if it works for you:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
// Simple interface
VBox vBox = new VBox(5);
vBox.setPadding(new Insets(10));
vBox.setAlignment(Pos.CENTER);
Label label = new Label();
// Let's add some very long text to our Label
label.setText("This is long text. It doesn't mean much because I am just writing things to make it long. Yes, I am that clever. Don't you just love this? Neither do I!");
// Make sure the Label is setup to wrap text when it reaches the end of its boundaries
label.setWrapText(true);
// Add the Label to our VBox
vBox.getChildren().add(label);
// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(vBox));
primaryStage.show();
}
}
Result:
To confirm that label.setWrapText(true); actually makes a difference, here's the result of the above code without that line, complete with the elipses ("...") you're experiencing:
Related
At the moment, I'm learning HaxeFlixel, and I can centre the text in the canvas, but i can't position the canvas in the centre of chrome the chrome window. Is there a way to do it and Main.hx?
PlayState.hx :
package;
import flixel.FlxG;
import flixel.FlxState;
import flixel.text.FlxText;
class PlayState extends FlxState
{
override public function create()
{
super.create();
var text = new FlxText(0, 0, 0, "Hello World\n", 64);
text.screenCenter();
add(text);
}
override public function update(elapsed:Float)
{
super.update(elapsed);
enterFullscreen();
}
public function enterFullscreen()
{
if (FlxG.keys.justPressed.ENTER)
{
FlxG.fullscreen = !FlxG.fullscreen;
}
}
}
Main.hx :
package;
import flixel.FlxGame;
import openfl.display.Sprite;
class Main extends Sprite
{
public function new()
{
super();
addChild(new FlxGame(0, 0, PlayState));
}
}
Please help,
Thanks.
When you compile an HaxeFlixel project for the web, there are two parts:
all the Haxe code is converted to Javascript, executing inside a <canvas> HTML object; all the rest of the HTML page is taken from a template, and you need to change that to center your canvas or otherwise add other elements on the side.
The default template that HaxeFlixel uses is inside the Lime project: https://github.com/openfl/lime/blob/develop/templates/html5/template/index.html
You need to copy that file inside your project, for example in assets/index.html; then customize it, for example adding the <center> tag around <div id="content"></div> (the div where your game canvas will be placed):
<center>
<div id="content"></div>
</center>
You can use CSS to center it, too, if you're so inclined. Just be careful touching the lines with :: as those are template directives.
With your new template placed in assets/index.html, add this line inside project.xml:
<template path="assets/index.html" />
I am trying to attach onclick listener on images in this code, using Glide framework but I'm not able to find a solution for that. Can someone help with this?
Images are showing in emulator already.
Thanks in advance
package com.example.andoridlifecycle.adapters
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.example.andoridlifecycle.R
import kotlinx.android.synthetic.main.item_custom_row.view.*
class ItemAdapter(private val context: Context, private val urls: ArrayList<String>) :
RecyclerView.Adapter<ItemAdapter.ViewHolder>() {
/**
* Inflates the custom view which is designed in xml layout file
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(context).inflate(
R.layout.item_custom_row,
parent,
false
)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Array with urls
val url = urls[position]
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.placeholder)
.into(holder.imageView)
}
// Gets the number of items in the list
override fun getItemCount(): Int {
return urls.size
}
// A ViewHolder describes an item view and metadata about its place within the RecyclerView.
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val imageView: ImageView = view.iv_image
}
}
I'm Java Developer so I cannot code with Kotlin but I'll show you how to set OnClickListener on your imageView. For using OnclickListener on imageView no need for Glide Library.
Just add the below lines in your onBindViewHolder
holder.imageView.setOnClickListener(view -> {
Toast.makeText(context, "this is number: "+position+" Image Selected", Toast.LENGTH_SHORT).show();
// or your code for imageView
});
Your new code looks like the below:-
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Array with urls
val url = urls[position]
//code for onClick on image
holder.imageView.setOnClickListener(view -> {
Toast.makeText(context, "this is number: "+position+" Image Selected", Toast.LENGTH_SHORT).show();
// or your code for imageView
});
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.placeholder)
.into(holder.imageView)
}
Make sure to change JAVA to Kotlin in my code. If you any problem is causing, Let me know
I wanted to connect a viewPager with an adapter (like this: viewPager.addOnPageChangeListener()), but the letters of the pageChangeListener just turn red like it wasn't a valid code...What am I doing wrong?? Heres a screenshot:
[1]: https://i.stack.imgur.com/IOYJY.png
Context: I'm currently working on a game with a few fragments where you can choose your game cards. I need the pageChangeListener to change the pictures of them cards. Maybe there could be another way to do this but i don't know how...
package com.suffv1
import android.os.Bundle
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import com.example.suff_02.Adapter2
import com.example.suff_02.R
import com.example.suff_02.kartenmodell
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(){
private lateinit var actionbar: ActionBar
private lateinit var liste: ArrayList<kartenmodell>
private lateinit var myAdapter: Adapter2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
super.setContentView(R.layout.activity_main)
actionbar = this.supportActionBar!!
loadCards()
viewpager2.addOnPageChangeListener(object: ViewPager.OnPageChangeListener{
override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) {
val title = liste[position].KartenImage
actionbar.title = title
}
})
}
private fun loadCards() {
liste = ArrayList()
liste.add(kartenmodell(R.drawable.bier_radler_klein_level_1))
liste.add(kartenmodell(R.drawable.bier_hopfentrunk_klein_level_1))
liste.add(kartenmodell(R.drawable.bier_butt_light_klein_level_1))
liste.add(kartenmodell(R.drawable.bier_becks_klein_level_1))
liste.add(kartenmodell(R.drawable.bier_tyskie_klein_level_1))
myAdapter = Adapter2(this, liste)
viewpager2.adapter = myAdapter
viewpager2.setPadding(100, 0, 100, 0)
}
}
Looks like you are using ViewPager2 not the original Viewpager and Viewpager2 does not have Page Change Listeners it instead has Page Change Callbacks
So you are using the wrong method to get notified when a pages is changed.
Instead do something like
var myPageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
Toast.makeText(this#MainActivity, "Selected position: ${position}",
Toast.LENGTH_SHORT).show()
}
}
viewpager.registerOnPageChangeCallback(myPageChangeCallback)
Though architecturally a lot of times it is bad form to use a OnPageChangeCallback as you are likely breaking the encapsulation idea of the Fragment and it can be better to use the lifecycle state change to Resumed of the Fragment to do things when a page(Fragment) is selected. e.g. put the code in the Fragments onResume method.
Though in this case of setting the actionbar title it is probably ok architecturally to use a OnPageChangeCallback
To create a AddOnPageChangeListener(), you do it as:
//See how, you have to use object to create an Object of the interface OnPageChangeListener.
//This is how you do for other listeners/interfaces/class as well when you've to implement its member functions instead of new in java.
viewPager.addOnPageChangeListener(object: ViewPager.OnPageChangeListener{
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
TODO("Not yet implemented")
}
override fun onPageSelected(position: Int) {
TODO("Not yet implemented")
}
override fun onPageScrollStateChanged(state: Int) {
TODO("Not yet implemented")
}
})
But, further in Kotlin, you can use Kotlin functions as
//This is for onPageSelected only.
viewPager.onPageChangeListener{ position: Int ->
//This position is the selected Page's position
}
Please help me, I'm pulling my hair out.
I have a class called "HudBarView" which extends the Group class(scene2d). It takes care of the drawing of the HUD Bar at the top of the screen(Pause button, score, etc).
Another class I have is "HUDManager" which takes care of all the HUD work in the game, including HUD Bar.
These two classes are very short and self-explantory. There is nothing sophisticated.
Now, currently I have only a pause button(ImageButton) in my HUD Bar but the problem is - it is visible only after I resize or pause and then resume the screen.
Here is a gif that describes my problem:
http://i.gyazo.com/6140c4ac1ea0778e4e1afce161ea3dc0.gif
I have tried to play a little bit with the code and I found out that the positioning of the actor is wrong. For instance if I define the position of the button with these values:
Vector2 position = new Vector2(Values.SCREEN_WIDTH-Values.Pause_Width*2,Values.SCREEN_HEIGHT-Values.Pause_Height*2);
Instead of these:
Vector2 position = new Vector2(Values.SCREEN_WIDTH-Values.Pause_Width,Values.SCREEN_HEIGHT-Values.Pause_Height);
It does show up on the screen but at the wrong position and after pause/resume it show up at the correct position.
Here is my HudBarView class:
package views.hud.views;
import views.renderers.HUDManager;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import engine.helpers.AssetsManager;
import engine.helpers.Values;
public class HUDBarView extends Group{
private ImageButton pause;
private HUDManager hud_manager;
private Skin skin;
public HUDBarView(HUDManager hud_manager) {
this.hud_manager = hud_manager;
initiate();
}
private void initiate() {
skin = new Skin(AssetsManager.getAsset(Values.BUTTONS_PACK, TextureAtlas.class));
pause = new ImageButton(skin.getDrawable("pause"));
pause.setSize(Values.Pause_Width, Values.Pause_Height);
pause.setPosition(Values.SCREEN_WIDTH - pause.getWidth(), Values.SCREEN_HEIGHT- pause.getHeight());
addActor(pause);
}
}
HUDManager class:
package views.renderers;
import views.hud.ParticleEffectsActor;
import views.hud.views.HUDBarView;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import engine.helpers.AssetsManager;
import engine.helpers.UIHelper;
import engine.helpers.Values;
public class HUDManager extends Group {
private Stage stage;
private Skin skin;
private Image text;
private HUDBarView hudView;
public HUDManager(Stage stage) {
this.stage = stage;
initiate();
addActor(hudView);
addActor(particles);
}
public void initiate() {
skin = AssetsManager.getAsset(Values.GAME_SKIN_PACK, Skin.class);
hudView = new HUDBarView(this);
particles = new ParticleEffectsActor();
}
public Stage getStage() {
return stage;
}
}
And Here is my game screen problem:
package views.screens;
import views.renderers.GameRenderer;
import views.renderers.HUDManager;
import engine.GameInputHandler;
import engine.GameWorld;
import engine.Values;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.StretchViewport;
/* Created by David Lasry : 10/25/14 */
public class GameScreen extends ScreenWrapper {
private GameWorld world;
private HUDManager hudManager;
private GameRenderer renderer;
private Stage stage;
public GameScreen() {
super();
ScreenHandler.getInstance().getGame().getAssetsManager()
.loadAsstes(2, this);
world = new GameWorld();
stage = new Stage(new StretchViewport(Values.SCREEN_WIDTH,
Values.SCREEN_HEIGHT));
camera = (OrthographicCamera) stage.getCamera();
InputMultiplexer inputs = new InputMultiplexer();
inputs.addProcessor(stage);
inputs.addProcessor(new GameInputHandler(world.getMonkeyManager(),
camera));
Gdx.input.setInputProcessor(inputs);
}
#Override
public void render(float delta) {
switch (state) {
case RUN:
stage.act(delta);
stage.draw();
world.update(delta);
break;
case PAUSE:
break;
}
}
// Callback method that is called when the assets are loaded
#Override
public void assetsLoaded() {
super.assetsLoaded();
renderer = new GameRenderer(world);
hudManager = new HUDManager();
stage.addActor(renderer);
stage.addActor(hudManager);
}
public HUDManager getHudManager() {
return hudManager;
}
#Override
public void dispose() {
stage.dispose();
ScreenHandler.getInstance().getGame().getAssetsManager().unload(2);
}
}
I'm struggling with this problem for almost 3 days and I have no idea what is causing this.
I even opened a new project, duplicated the relevant classes and just tried to position the button at the top right corner of the screen and it worked! So why it doesn't work in my actual game?
Please help !
I'm working on a JavaFX application which has a layout generated from an external data structure, composed of
displaying components that know their own aspect ratios (height as a dependent of width)
2 types of structural components that
display all children with an equal width across their page, each child up as much vertical space as needed
display all children down the page using the full width and taking as much vertical space as needed
But I'm finding things aren't displaying as I expect. I've made a simplified case that demonstrates the problem.
The code is below, and the problem is that v3 doesn't get displayed, and I can't for the life of me work out why. I guess there's some facet of VBoxes and HBoxes that I haven't understood.
I'd really appreciate any help or ideas. Thanks in advance!
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.Random;
public class Test extends Application {
static Random rand = new Random();
public static void main(String args[]) {
Application.launch("something");
}
#Override
public void start(Stage mainStage) throws Exception {
testVBoxes(mainStage);
}
private void testVBoxes(Stage mainStage) {
VBox root = new VBox();
Scene one = new Scene(root, 800, 600, Color.WHITE);
FixedAspectRatioH h1 = new FixedAspectRatioH();
FixedAspectRatioH h2 = new FixedAspectRatioH();
FixedAspectRatioH h3 = new FixedAspectRatioH();
FixedAspectRatioV v1 = new FixedAspectRatioV();
FixedAspectRatioV v2 = new FixedAspectRatioV();
FixedAspectRatioV v3 = new FixedAspectRatioV();
h1.prefWidthProperty().bind(root.widthProperty());
h2.add(v2);
v1.add(h3);
v1.add(h2);
h1.add(v1);
h1.add(v3);
root.getChildren().add(h1);
mainStage.setScene(one);
mainStage.show();
}
private class FixedAspectRatioV extends VBox {
public FixedAspectRatioV() {
Rectangle r = new Rectangle();
r.setFill(Color.rgb(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
r.widthProperty().bind(widthProperty());
r.heightProperty().bind(r.widthProperty().divide(3));
getChildren().add(r);
}
public void add(Region n) {
n.prefWidthProperty().bind(widthProperty());
getChildren().add(n);
}
}
private class FixedAspectRatioH extends HBox {
public FixedAspectRatioH() {
Rectangle r = new Rectangle();
r.setFill(Color.rgb(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
r.widthProperty().bind(widthProperty().divide(4));
r.heightProperty().bind(r.widthProperty());
getChildren().add(r);
}
public void add(Region n) {
HBox.setHgrow(n, Priority.ALWAYS);
getChildren().add(n);
}
}
}
its 2 years but the solution is you forgot
Node.setPrefSize(width,height);
and also add this to your constructor Hbox.setFillHeight(true);