In school I am working on a project using JavaFx. I would like to make it special and use a GIF as background.
It works and displays my GIF, but it makes my program and GIF run realy slow.
Is there a way to fix this using Threads?
(I am new to using threads and don't realy know how to use them here).
this is my code for the background:
public class WelkomScherm extends StackPane
{
private final screenControl sCtrl;
public WelkomScherm(screenControl sCtrl)
{
this.sCtrl = sCtrl;
//Image backgroundImage = new Image("/img/startBG.gif");
Image backgroundImage = new Image("/img/Welkom.png");
ImageView background = new ImageView(backgroundImage);
background.setFitHeight(height);
background.setFitWidth(width);
...some buttons and labels...
this.getChildren().addAll(background,...,...);
}
I am also new to working on bigger projects, all tips are welcome!
Related
I have a Problem with my Vaadin Grid.
Its a rather simple Grid that just shows some cars with name, model, ps, etc.
If you click on one of the rows, the grid detail opens, and shows a picture of the car.
If you click on the first row in the grid, the Image is cut of and you cant scroll.
But if you click around a bit, for example you click on the 6th row , then the Picture is fine.
Also then you can scroll and if you click on the first row, the image is now shown correctly.
Is this a mistake by me or is this a bug?
public FahrzeugView()
{
final Grid<Fahrzeug> grid = new Grid<>(Fahrzeug.class, false);
grid.setWidthFull();
grid.setHeight("800px");
grid.addColumn(Fahrzeug::getId).setHeader("Id").setSortable(true);
grid.addColumn(Fahrzeug::getMarke).setHeader("Marke").setSortable(true);
grid.addColumn(Fahrzeug::getModell).setHeader("Modell").setSortable(true);
grid.addColumn(Fahrzeug::getPs).setHeader("Ps").setSortable(true).setId("ps");
grid.setItemDetailsRenderer(this.createFahrzeugDetailsRenderer());
final List<Fahrzeug> fahrzeuge = Fahrzeug.setData();
grid.setItems(fahrzeuge);
this.add(grid);
}
private Renderer<Fahrzeug> createFahrzeugDetailsRenderer()
{
return new ComponentRenderer<>(FahrzeugDetailLayout::new,
FahrzeugDetailLayout::setFahrzeug);
}
}
public class FahrzeugDetailLayout extends FormLayout
{
private final Image image = new Image("", "");
public FahrzeugDetailLayout()
{
setResponsiveSteps(new ResponsiveStep("0", 1));
setColspan(this.image, 1);
this.add(this.image);
}
public void setFahrzeug(final Fahrzeug fahrzeug)
{
this.image.setAlt("Bild wird nicht angezeigt!");
this.image.setSrc(fahrzeug.getUrl());
}
}
I dont understand why it is only cut of when you first open the detail and not when you click around and click on it again.
I dont really know what to try to fix this, because i dont know what is causing it to happen.
I tryed google for it but this Problem is very specific.
Thankfull for all the help!
This is my first question at stackoverflow so if I miss information let me know.
I want to make a program which shows a worldmap in which you can zoom in and out and place marker which moves around (like a game map).
Because I would like the world map to be used offline I am using unfoldingmaps http://unfoldingmaps.org/.
This works great if I use the examples.
Now I would like to integrate this into the program I have already written.
I am trying to integrate the Papplet of the unfoldingmaps into a JPanel. This JPanel is part of a tabbedPanel.
This is the code I use to add it to the tabbedPanel:
//Create game map menu
gameMapMenu = new JPanel();
tabbedPanelMain.addTab("World map", gameMapMenu);
//tabbedPanelMain.setEnabledAt(2, false);
worldMap = map.WorldMap.getWorldMap();
gameMapMenu.add("World map", worldMap);
This is the code (in a separate Class) which is used to generate the unfolding map:
UnfoldingMap map;
public void setup() {
size(800, 600, OPENGL);
map = new UnfoldingMap(this, new MBTilesMapProvider(mbTilesString));
MapUtils.createDefaultEventDispatcher(this, map);
map.setZoomRange(1, 10);
}
public void draw() {
background(0);
map.draw();
}
public static WorldMap getWorldMap() {
WorldMap worldMap = new WorldMap();
return worldMap;
}
I have two questions I have been really struggling with and I cannot find any answers for:
The example above does not give any errors. It starts the application and tabbedPanel but does not show the map. If I run the Papplet of the world map in Eclipse as a Applet it works fine. How do I add the Papplet with the map to my existing application so it is viewed through the tabbedPanel?
To update the markers on the world map I was thinking about adding functions to the class with the worldmap Papplet. Is this the way to interact with the Papplet from the application?
After a lot of trying I found out I needed to add the .init() function.
Can someone explain to me what this does?
//Create game map menu
gameMapMenu = new JPanel();
tabbedPanelMain.addTab("World map", gameMapMenu);
tabbedPanelMain.setEnabledAt(2, false);
worldMap = map.WorldMap.getWorldMap();
gameMapMenu.add("World map", worldMap);
worldMap.init();
So for my 2D game I want to use an image to represent a player but when I try to use an image, it tells me that it couldn't be "instantiated". I don't know what that means:
public class PlayerOne extends Entity{
private Image img = new Image();
[...]
#Override
public void render(Graphics g){
g.drawImage( img , x, y, Color.BLUE, new ImageObserver());
}
}
I tried it in another class with BufferedImages but that somehow doesn't work.
So it can't create Objects of neither Image nor the ImageObserver. Does anyone know a fix for this error ?
You cannot instantiate an abstract class. Please see link.
Following syntax would be operational:
private Image image = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Also see link for more information on BufferedImage. Additionally, here is a tutorial which ilustrates an example implementation;
You should have an image file (*.png for example ) to start with. Then use
Image img = new ImageIcon("img.png").getImage();
I'm trying to make a GUI for my game. I've tried various libraries and I've ended up with Nifty. I haven't found any useful tutorial and therefore I'm learning from code examples.
I want to display a simple Button, but it seems that my code is not working. I've tried setting background color of Panel which has been working. I have no idea why the Button is not displaying.
Here's what I have:
protected void prepareNifty(Nifty nifty) {
ScreenBuilder sb = new ScreenBuilder("start");
LayerBuilder lb = new LayerBuilder();
PanelBuilder pb = new PanelBuilder();
pb.control(new ButtonBuilder("btn1", "First Button!"){{
alignCenter();
valignCenter();
height("5%");
width("15%");
backgroundColor(Color.WHITE);
}});
pb.childLayoutCenter();
lb.childLayoutVertical();
lb.panel(pb);
sb.layer(lb);
nifty.addScreen("start", sb.build(nifty));
}
I should add I'm using Slick2D and my class extends NiftyBasicGame.
How can I display the Button and set it an absolute position?
You'll need to load the controls and the styles too when you want to use the default controls, like the Button control.
Try something like:
nifty.loadStyleFile("nifty-default-styles.xml");
nifty.loadControlFile("nifty-default-controls.xml");
new ScreenBuilder("start") {{
layer(new LayerBuilder("background") {{
backgroundColor("#f008");
childLayoutAbsolute();
control(new ButtonBuilder("showPopupButton", "SHOW") {{
x(SizeValue.px(100));
y(SizeValue.px(100));
interactOnClick("showPopup()");
}});
}});
}}.build(nifty);
nifty.gotoScreen("start");
Besides that, it might help to read the nifty-gui-the-manual-1.3.2.pdf
I'm new to AndEngine and programming in general. I'm using ADT.
I have refactored some code into my Resource Manager, so it will load an animated sprite for me.
The image file is in the correct asset folder, and animated fine before I moved the code into the ResourceManager.java file.
Inside my onCreateResources (in GameActivity.java), I have this:
ResourceManager.getInstance().loadSpriteSheetTextures(mEngine, this);
pOnCreateResourcesCallback.onCreateResourcesFinished();
In the onCreateScene method of my GameActivity.java file, I have this:
animatedSprite = new AnimatedSprite(positionX,positionY,mTiledTextureRegion,mEngine.getVertexBufferObjectManager());
animatedSprite.animate(25);
mScene.attachChild(animatedSprite);
In my ResourceManager.java, I have this method:
public synchronized void loadSpriteSheetTextures(Engine engine, Context context){
//set our gfx asset folder
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
//create a animated sprite
BuildableBitmapTextureAtlas mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(engine.getTextureManager(),512,512,TextureOptions.BILINEAR);
mTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, context, "plus.png", 6, 4);
try{
//
mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource,BitmapTextureAtlas>(0,0,0));
mBitmapTextureAtlas.load();
}catch(TextureAtlasBuilderException e){Debug.e(e);}
}
I should get a spinning top, but I get the spritesheet printed to screen, with the top right borders cut off, no animation..
In LogCat, I get an error about Null pointer exception (I guess it's related).
It worked fine until I 'refactored' it into a mess ...
Please help.. What am I doing wrong?
(edited typos)
.