Marquee selection does not works on Jgraphx - jgraphx

I'm writting a program using Jgraphx and the multiple selection works well (I can select one node, press ctrl and click on other node and is selected then), but I cannot make the marquee selection works (I click and drag the mouse but no rectangle appears to select nodes). What I'm doing wrong?

To add this functionallity create a new mxRubberband. This will do all the work for you.
Example:
public class HelloWorld extends JFrame
{
private static final long serialVersionUID = -2707712944901661771L;
public HelloWorld()
{
super("Hello, World!");
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
String s1 = "Hello";
String s2 = "World!";
Object v1 = graph.insertVertex(parent, "ID1", s1, 20, 20, 80,
30);
//graph.insertVertex(parent, null, v1, 20, 280, 80, 30);
Object v2 = graph.insertVertex(parent, "ID2", s2, 240, 150,
80, 30);
mxCell edge = (mxCell) graph.insertEdge(parent, "ID3", "TEST", v1, v2);
}
finally
{
graph.getModel().endUpdate();
}
Object o = graph.getDefaultParent();
mxGraphComponent graphComponent = new mxGraphComponent(graph);
new mxRubberband(graphComponent); // Adds support for marquee selection
getContentPane().add(graphComponent);
}
public static void main(String[] args)
{
HelloWorld frame = new HelloWorld();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
}

Related

Sliding in and out using xamarin

I am trying to implement this kind of design as shown in fig on my page, how can I achieve this in Xamarin.forms mainly in shared project. As I am trying to reduce the complexity by doing in platform specific.
I tried using this even https://github.com/XAM-Consulting/SlideOverKit .
But the issue is I can't slide in the menu when its open (i.e after touching on page I want the menu to hide) but it doesn't happen. We need to manually drag that out to close it.
So please let me know how to achieve this.
Thanks
Based on your description, I modifed my code,there is a GIF
When you click the ImageButton, you should call this method this.HideMenu();
code of QuickInnerMenuPage
public QuickInnerMenuPage()
{
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
Children = {
new Label(){Text="1222"}
}
};
this.SlideMenu = new QuickInnerMenuView(MenuOrientation.RightToLeft);
QuickInnerMenuView.ib.Clicked += (o, e) =>
{
this.HideMenu();
};
}
}
There is code of QuickInnerMenuView
public class QuickInnerMenuView : SlideMenuView
{
public static ImageButton ib;
public QuickInnerMenuView (MenuOrientation orientation)
{
ib = new ImageButton
{
Source = "Happy.png",
WidthRequest = 25,
HeightRequest = 25,
};
var mainLayout = new StackLayout {
Spacing = 15,
Children = {
ib,
new ImageButton {
Source = "Home.png",
WidthRequest = 25,
HeightRequest = 25,
},
new ImageButton {
Source = "MessageFilled.png",
WidthRequest = 25,
HeightRequest = 25,
},
new ImageButton {
Source = "Settings.png",
WidthRequest = 25,
HeightRequest = 25,
},
}
};
// In this case the IsFullScreen must set false
this.IsFullScreen = false;
this.BackgroundViewColor = Color.Transparent;
// You must set BackgroundColor,
// and you cannot put another layout with background color cover the whole View
// otherwise, it cannot be dragged on Android
this.BackgroundColor = Color.FromHex ("#C82630");
this.MenuOrientations = orientation;
if (orientation == MenuOrientation.BottomToTop) {
mainLayout.Orientation = StackOrientation.Vertical;
mainLayout.Children.Insert (0, new Image {
Source = "DoubleUp.png",
WidthRequest = 25,
HeightRequest = 25,
});
mainLayout.Padding = new Thickness (0, 5);
// In this case, you must set both WidthRequest and HeightRequest.
this.WidthRequest = 50;
this.HeightRequest = 200;
// A little bigger then DoubleUp.png image size, used for user drag it.
this.DraggerButtonHeight = 30;
// In this menu direction you must set LeftMargin.
this.LeftMargin = 100;
}
Do you add want to achieve it like following GIF.
If so,when you used SlideMenu, you should Create a new view that inherit's from MenuContainerPage like following code.Note:MenuContainerPage comes from SlideOverKit
public class QuickInnerMenuPage: MenuContainerPage
{
public QuickInnerMenuPage()
{
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
Children = {
new Label(){Text="1222"}
}
};
this.SlideMenu = new QuickInnerMenuView(MenuOrientation.RightToLeft);
}
}
Here is my demo.you could refer to it.
https://github.com/851265601/SlideOverKitDemo

Java OOP using Translate Transition and JavaFX

The Translate Transition does not output. It uses a method in the main class. I believe it is not working because it is used as an object. There has to be a different code to implement. It uses a method in a main and then puts it into a tester. However, I do not know how to use it because it uses a constructor/object as well. Then, the object turns and changes into a node which I changed it. I do not know how the Translate Transition method is attached to the object and displays it into the javafx console. Please help solve the problem for positive feedback as it shows.
import javafx.animation.TranslateTransition;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Shape;
import javafx.util.Duration;
public class AnxiousShapes {
private Shape shape;
private double delay ;
private int howFarToMoveX;
private int howFarToMoveY;
public AnxiousShapes(int type, int x, int y, int size, double delay, Color color, int hftmX, int hftmY) {
if (type == 0) {shape = new Circle(x, y, size, color);}
//these are the only lines you can change
//in this main class
//else if (type == 1){shape = new Rectangle(x,y,color);}
//else if (type == 2){shape = new Polygon();}
//else if (type == 3) {shape = new Circle(x, y, size, color);}
//else { System.out.println("Error in type");shape = new
//Circle(???????);}
this.delay = delay;
this.howFarToMoveX = hftmX;
this.howFarToMoveY = hftmY;
}
// getter and setters
public TranslateTransition calculateTt() {
TranslateTransition tt = new TranslateTransition(Duration.seconds(this.delay), this.shape);
tt.setToX(this.shape.getLayoutX() + howFarToMoveX);
tt.setToY(shape.getLayoutY() + howFarToMoveY);
// Let the animation run forever -- if the shape
// tries to move "off-screen" it will return to the beginning
tt.setCycleCount(TranslateTransition.INDEFINITE);
return tt;
}
#Override
public String toString() {
return "AnxiousShape [shape=" + shape + ", delay=" + delay + ", howFarToMoveX=" + howFarToMoveX
+ ", howFarToMoveY=" + howFarToMoveY + "]";
}
}
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.Group;
import javafx.scene.Node;
import java.util.Random;
public class AnxiousShapesTester extends Application {
#Override
public void start(Stage stage) {
// adding the new things
Group root = new Group();
stage.setTitle("Welcome to JavaFX!");
// create the shape circle
AnxiousShapes circle1 = new AnxiousShapes(0, 200, 200, 50, 15,
Color.GREEN, 10 ,35);
root.getChildren().add(circle1.getShape());
// this does not work
// TranslateTransition trans = circle1.calculateTt();
// trans.setNode(root);
// trans.play();
// and I tried this and I already have the movement in constructor for
// delay and x and y but TranslateTransition
// asks for duration.millis(500)
TranslateTransition tt = new
TranslateTransition(Duration.millis(500), root);
tt.play();
Scene scene = new Scene(root, 600, 600, Color.WHITE);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
In a comment, you say, "this does not work." The problem is trans.setNode(root), which attempts to make root "The target node of this TranslateTransition." Your implementation of calculateTt() already specifies this.shape as the target node. Instead, add a suitable accessor to AnxiousShapes and use the transition as constructed; the following changes are illustrated below:
public Shape getShape() { return this.shape; }
…
AnxiousShapes circle1 = new AnxiousShapes(0, 100, 100, 100, 3, Color.GREEN, 400, 400);
root.getChildren().add(circle1.getShape());
TranslateTransition trans = circle1.calculateTt();
trans.play();

How to add objects to an ArrayList?

I am very new on programming, and I am trying to build a little game so I can teach myself OOP, however I have an problem that I cant solve it at all.
I have a class ComponentsPanel from which I am calling another (JavaFish) as an ArrayList. The addFish() method adds to the ArrayList a JavaFish. When I call addFish() method from the constructor works perfectly fine.
The problem is, when I call it from the start(), from another class and package, it dose not add any element to the ArrayList.
Main class
package framework;
public class Core {
JFrame window;
ComponentsPanel panel;
int width = 600;
int height = 400;
public void start() {
window = new JFrame();
window.setTitle("Java Game");
window.setPreferredSize(new Dimension(width, height));
window.setMinimumSize(new Dimension(width, height));
window.setMaximumSize(new Dimension(width, height));
window.setResizable(false);
panel = new ComponentsPanel();
window.add(panel, BorderLayout.CENTER);
window.setVisible(true);
window.pack();
}
public void stop() {
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
ComponentsPanel
package framework;
public class ComponentsPanel extends JPanel {
ArrayList<JavaFish> javaFishes = new ArrayList<JavaFish>();
public ComponentsPanel() {
javaFishes = new ArrayList<JavaFish>();
//addFish(new JavaFish(100, 100, 25, 25));
}
public void addFish(JavaFish javaFish) {
javaFishes.add(javaFish);
repaint();
}
public void start() {
addFish(new JavaFish(100, 100, 25, 25));
}
#Override
public void paint(Graphics g) {
for (JavaFish jFish : javaFishes) {
jFish.draw(g);
}
}
}
Fish class
package framework;
public class JavaFish {
BufferedImage img;
private int x_pos;
private int y_pos;
private int sizeX;
private int sizeY;
public JavaFish(int x_pos, int y_pos, int sizeX, int sizeY) {
this.x_pos = x_pos;
this.y_pos = y_pos;
this.sizeX = sizeX;
this.sizeY = sizeY;
}
public void draw(Graphics g) {
BufferedImage JavaFish = LoadImage("img/JavaFish.png");
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(JavaFish, x_pos, y_pos, sizeX, sizeY, null);
}
BufferedImage LoadImage(String FileName) {
img = null;
try {
img = ImageIO.read(new File (FileName));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}
Simulation class
package user;
import framework.ComponentsPanel;
import framework.Core;
public class Simulation {
Core c;
ComponentsPanel panel;
boolean endSim = false;
public Simulation() {
c = new Core();
panel = new ComponentsPanel();
}
private void populate() {
panel.start()
}
private void updateWorld() {
while (!endSim) {
c.start();
break;
}
c.stop();
}
public static void main(String[] args) {
Simulation simulation = new Simulation();
simulation.populate();
simulation.updateWorld();
}
}
start() method present in ComponentsPanel is not a lifecycle method. So unless you invoke start() method manually it won't get invoked. You can add a print statement for tracking the calls.
#Cristea Thank you for the updated code.
This will work if you call the addFish() method on the ComponentsPanel instance created in Core class's start() method as below.
public void start() {
window = new JFrame();
window.setTitle("Java Game");
window.setPreferredSize(new Dimension(width, height));
window.setMinimumSize(new Dimension(width, height));
window.setMaximumSize(new Dimension(width, height));
window.setResizable(false);
panel = new ComponentsPanel(); // This panel is passed to the JFrame
panel.addFish(new JavaFish(100, 100, 25, 25)); // so do addFish() here.
window.add(panel, BorderLayout.CENTER);
window.setVisible(true);
window.pack();
}
The problem with your code was, you are creating two instances of ComponentsPanel class. On the first instance(let's call it panel-1 ) you created, you are calling addFish() method, through the method below. This start() method is called on panel-1.
// panel you used to call this start() method was not used in JFrame
public void start() {
addFish(new JavaFish(100, 100, 25, 25));
}
Then you are creating 2nd instance of ComponentsPanel class(let's call it panel-2) from the Core class's start() method and panel-2 does not do addFish(). But this instance (panel-2)is what is added to the JFrame, which will be used for the purpose of paint(). Since panel-2 does not do addFish(), paint() does not find any JavaFishes.
Remember, instance variable javaFishes is specific to each instance of ComponentsPanel. javaFishes added by panel-1 would not be available for panel-2.

Cannot show user input with a textfield

I'm trying to use a TextField to get some user input:
public void render() {
Gdx.gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(0, 0, 0, 0);
batch.begin();
batch.end();
stage = new Stage();
Gdx.input.setInputProcessor(stage);
Skin skin = new Skin(Gdx.files.internal("assets/uiskin.json"));
TextButton btnLogin = new TextButton("Click", skin);
btnLogin.setPosition(300, 300);
btnLogin.setSize(300, 60);
btnLogin.addListener(new ClickListener() {
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
System.out.println(txfUsername.getText());
return false;
}
});
txfUsername = new TextField("", skin);
txfUsername.setPosition(300, 250);
txfUsername.setSize(300, 40);
stage.addActor(txfUsername);
stage.addActor(btnLogin);
stage.act();
stage.draw();
}
All I get is a blank field. The user can't interact with it in any way.
I followed the instructions in this video
How do I make the textfield editable?
You use txfUsername = new TextField ("", skin); in your render method. That creates a new TextField from scratch on each render.
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
// do other rendering ...
batch.end();
Gdx.app.log("MyTextField", txfUsername.getText());
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
In your class variables:
private TextButton btnLogin;
TextField txfUsername;
In your method show or create (not render):
#Override
public void show() {
btnLogin = new TextButton("Click", skin);
btnLogin.setPosition(300, 300);
btnLogin.setSize(300, 60);
btnLogin.addListener(new ClickListener() {
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
System.out.println(txfUsername.getText());
return false;
}
});
txfUsername = new TextField("", skin);
txfUsername.setPosition(300, 250);
txfUsername.setSize(300, 40);
stage.addActor(txfUsername);
stage.addActor(btnLogin);
}
Use txfUsername.getText(); written for the field
Edit: I do not know how you worked with GL11, if GL10, I could understand a you malfunction, if you get error in GL10, you should update libgdx, GL10 is an interface and I think last versions for in libgdx not bring already

Why are my screenshots only black?

could someone tell me why my screenshots are only black? I am still learning and couldnt find a clue why they are only black.
This is my Utility class
static class XNAUtilities
{
private static RenderTarget2D ssTexture;
private static KeyboardState currentState, previousState;
private static int counter;
public static void TakeScreenShot(GraphicsDevice device, Keys theKey)
{
// Take Screenshot
currentState = Keyboard.GetState();
if (currentState.IsKeyDown(theKey) && previousState.IsKeyUp(theKey))
{
//device.SetRenderTarget(null);
PresentationParameters pparameters = device.PresentationParameters;
ssTexture = new RenderTarget2D(device, pparameters.BackBufferWidth, pparameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None); //??
FileStream fileStream = new System.IO.FileStream(#"Screenshot" + "_" + counter + ".png", System.IO.FileMode.CreateNew);
ssTexture.SaveAsPng(fileStream, pparameters.BackBufferWidth, pparameters.BackBufferHeight);
counter++;
}
previousState = currentState;
}
}
}
This is my Update and Draw from Game1.cs
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
myModelRotation += MathHelper.ToRadians(1f);
// Take a Screenshot
XNAUtilities.TakeScreenShot(GraphicsDevice, Keys.F8);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in myModel.Meshes)
{
foreach (BasicEffect effects in mesh.Effects)
{
effects.EnableDefaultLighting();
effects.World = transforms[mesh.ParentBone.Index]
* Matrix.CreateRotationY(myModelRotation)
* Matrix.CreateTranslation(myModelPosition);
effects.View = Matrix.CreateLookAt(new Vector3(200, 100, 400), Vector3.Zero, Vector3.Up);
effects.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f),
GraphicsDevice.Viewport.AspectRatio, 1, 5000);
}
mesh.Draw();
}
smileySprite.DrawSprites(GraphicsDevice, spriteBatch, new Vector2(10,10), Color.White);
base.Draw(gameTime);
}
You're not actually rendering to your render target. So you're saving the blank target.
You need to wrap your scene drawing like so:
GraphicsDevice.SetRenderTarget(ssTexture);
// Render your scene here
GraphicsDevice.SetRenderTarget(null);
// Now you can save your render target as a texture