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();
Related
I'm trying to integrate Processing 3 into a swing application, but because PApplet doesn't extend Applet anymore I can't just add it as a component right away.
Is there anyway of embeding a Processing 3 sketch into Swing, it would be enough if I could just open the sketch in a seperate window without the PDE.
You can run a sketch from Java by extending PApplet and then using the runSketch() function to run that PApplet. It'll look something like this:
String[] args = {"MyPapplet "};
MyPapplet mp = new MyPapplet ();
PApplet.runSketch(args, mp);
public class MyPapplet extends PApplet {
public void settings() {
size(200, 100);
}
public void draw() {
background(255);
fill(0);
ellipse(100, 50, 10, 10);
}
}
Then if you want to get at the underlying component, you have to write code that depends on which renderer you're using. Here's how you'd do it with the standard renderer:
PSurfaceAWT awtSurface = (PSurfaceAWT)mp.surface;
PSurfaceAWT.SmoothCanvas smoothCanvas = (PSurfaceAWT.SmoothCanvas)awtSurface.getNative();
Once you have the SmoothCanvas, you can remove it from its frame and add it to yours.
I am creating a REST API to generate video dynamically based on user input and provided animation types. So, I am using processing 2.2.1 for this.
I want to generate 3D animation using OPENGL. But OPENGL requires Window object. Since I am using processing in background to just generate frame, how can I use processing with OPENGL to generate animated frames without interactive means without displaying the window.
My sample code
import com.hamoid.VideoExport;
import processing.core.*;
public class CircleSketch extends PApplet {
private VideoExport videoExport;
public void setup() {
size(400, 400,OPENGL);
videoExport = new VideoExport(this, "F:/work/tmp.mp4");
background(0);
}
public void draw() {
background(0);
fill(200);
rotateX(radians(50));
rectMode(CENTER);
rect(width/2,height/2, 100, 100);
videoExport.saveFrame();
}
}
DisplayFrame Class
public class DisplayFrame extends javax.swing.JFrame {
public DisplayFrame(){
this.setSize(600, 600); //The window Dimensions
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setBounds(20, 20, 600, 600);
processing.core.PApplet sketch = new CircleSketch();
panel.add(sketch);
this.add(panel);
this.setVisible(false);
//this is the function used to start the execution of the sketch
sketch.init();
}
public static void main(String[] args) {
new DisplayFrame().setVisible(false);
}
}
Any suggestion please???
how can I use processing with OPENGL to generate animated frames without interactive means without displaying the window.
In short terms: Given the current OpenGL driver models you can't (as long as you want to use a GPU for rendering). Period, that's how it is.
If you can live with software rendering (slow), then there's OSMesa. Also in the forseeable future there should be changes in the driver models, which allow to use OpenGL in a headless environment. Until then, you need a window of some sort on a graphics environment that actually actively feeds a display output (so it doesn't suffice to start a X11 server and background it).
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 making a simple point&click game using libGdx and their Scene2d. Now, when I enter a location my Stage is cleared and new Actors are beeing attached. It doesnt feel right and its not efficient.
Can I make all Actors at the begining (except backgrounds, I will load them when entering a location), add them to Stage and associate them with locations, so the Stage would know witch to draw?
My only idea was to check that in draw and act methods of every actor, but that would mean houndreds of checks in a loop. Maybe Scene2d got something to help me out? Or maybe there is another way to do it?
My only idea was to check that in draw and act methods of every actor, but that would mean houndreds of checks in a loop.
Yes, that will be inneficient, and above all, a hell to maintain.
Now, when I enter a location my Stage is cleared and new Actors are beeing attached.
This is where your problem is, you're not using scene2D as it should be IMHO. I hope you're up for an intense architecture & code refactoring session.
When entering a new location, you should be entering a new stage.
So first, you should have several stages :
class MainMenu extends Stage {
public MainMenu(){
// Add buttons to play or quit the game
}
}
class PointNClickStage extends Stage {
// Add stuff common to all point'n click stages such as an inventory display
}
class Island extends PointNClickStage {
public Island (){
// Add some palm trees and an hidden chest
}
}
class PirateShip extends PointNClickStage {
public PirateShip(){
// Add some pirates and their ship
}
}
... etc
Then in your application listener, you should implement a way to change the current stage being rendered. Conceptually, this is often called a "scene/stage director". Some scene-based frameworks, such as Cocos2D provides their own scene director, but libgdx doesn't currently. So, you have to implement this mechanism by yourself and here is a very basic example to help you get the gist of it :
public MyApp extends ApplicationAdapter {
private Stage currentStage;
private static MyApp instance;
// ...
#Override
public void create () {
instance = this;
MyApp.setStage(new MainMenu()); // The game begins in the main menu
}
#Override
public void render () {
Gdx.gl.glClearColor(0.15f, 0.1f, 0.15f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
currentStage.act();
currentStage.draw();
}
public static void setStage(Stage stage){
instance.currentStage = stage;
Gdx.input.setInputProcessor(stage); // Important ;)
}
// ...
}
So that to change the location (current stage) you will only have to do :
MyApp.setStage(new PirateShip())
Then, if you don't want to recreate a new stage every time you change your location, you may initialize and keep a reference on them somewhere so that you will be able to change the location like that for example.
MyApp.setStage(some_list_containing_initialized_stage.get(id))
Alternatively, you may also look into this libgdx extension that provides scene2d utils classes such as a scene director, and transitions that may be useful for you if you don't want to reinvent the wheel later.
I'm using Jung 2.0 for a while now, but I'm a bit confused with all the configuration code that is done in the various demos that ship with the package.
Could anyone tell me how to configure a VisualizationViewer so that I can move the graph nodes around by clicking and draging them with the mouse?
The code I have is below. It's a mix of the various demos I studied. But I can only pan and rotate the graph with it.
public class GrafoParticipacaoSocietaria extends JFrame {
Graph<VerticeParticipacaoSocietaria, Integer> graph;
VisualizationViewer<String, String> vv;
Layout<VerticeParticipacaoSocietaria, Integer> layout;
public GrafoParticipacaoSocietaria(Graph<VerticeParticipacaoSocietaria, Integer> grafoPart) {
super("Participação Societária");
graph = grafoPart;
layout = new ISOMLayout(graph);
final VisualizationModel visualizationModel = new DefaultVisualizationModel(layout);
vv = new VisualizationViewer(visualizationModel);
vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());
vv.setForeground(Color.blue);
getContentPane().add(vv);
// this class will provide both label drawing and vertex shapes
VertexLabelAsShapeRenderer<String, String> vlasr = new VertexLabelAsShapeRenderer<String, String>(
vv.getRenderContext());
vv.getRenderContext().setVertexShapeTransformer(vlasr);
vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.lightGray));
vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.lightGray));
vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(1.0f)));
// customize the renderer
// vv.getRenderer().setVertexRenderer(new MyGradientVertexRenderer<String, String>(Color.cyan, Color.white, true));
vv.getRenderer().setVertexLabelRenderer(vlasr);
vv.setGraphMouse(new DefaultModalGraphMouse());
}
}
Thanks to GrahamA's answer, I found the code I was looking for:
DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
vv.setGraphMouse(graphMouse);
That code changes the mode in ModalGraphMouse to the PICKING, which allows the user to select and move nodes around.
You can find an example on the Jung website here use Editing to add nodes and then switch to Picking to move the nodes you have added. The sourcode is avalaible from SourceFource check out jung-samples-X.X.X.jar the class called GraphEditorDemo
Screen shot from the Jung Website