Posts

Create code-first Java program in JPA/Hibernate

main\java\App.Java
============
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class App {
public static void main(String[] args) {
System.out.println("Hibernate one to many (Annotation)");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();

Category cat = new Category("Phone");
session.save(cat);
Product iPhone = new Product(cat, "iPhone8");
Product galaxy = new Product(cat, "Galaxy S9");
cat.getProducts().add(iPhone);
session.save(iPhone);

session.getTransaction().commit();
System.out.println("Done");
}
}


Category.java ============== import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDEN…

How to create scrollable ListView with controls above and below in fixed position?

Layout.xml
=======
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.philip.MainActivity"> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="Happy Independence Day!" android:textAppearance="?android:attr/textAppearanceLarge"/> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" a…

Draggable Rectangle in JavaFX

import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class DraggableRectangle extends Application {

private double x = 0;
private double y = 0;
private double sceneX = 0;
private double sceneY = 0;
private Rectangle rect;
public static Pane root;

@Override
public void start(Stage primaryStage) throws Exception {
rect = new Rectangle();
rect.setWidth(95);
rect.setHeight(55);
rect.setFill(Color.GRAY);
rect.setStroke(Color.BLACK);
rect.setStrokeWidth(1);
rect.setArcHeight(15);
rect.setArcWidth(15);

rect.setOnMousePressed(e -> {
if (e.getButton() == MouseButton.PRIMARY) {
// Get the current mouse coordinates relative to the scene.
sceneX …

How to display a label with connecting line between two circles?

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class CircleWithConnectedLine extends Application {
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
Circle c1 = new Circle(40, 40, 20);
Circle c2 = new Circle(200, 250, 20);
Line line = new Line();
Text text = new Text(60, 60, "");

line.startXProperty().bind(c1.centerXProperty());
line.startYProperty().bind(c1.centerYProperty());
line.endXProperty().bind(c2.centerXProperty());
line.endYProperty().bind(c2.centerYProperty());

Pane pane = new Pane();
pane.getChildr…

How to create draggable Circle on mouse Click in JavaFX?

import java.util.Random;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.Event;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class GenerateCircleOnClick extends Application {

Random rng = new Random();

@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
pane.setOnMouseClicked(e -> {
Circle c = createDraggableCircle(e.getX(), e.getY(), 25, pane);
pane.getChildren().add(c);

});

primaryStage.setScene(new Scene(pane, 600, 600));
primaryStage.show();
}

private Circle createDraggableCircle(double radius, double x, double y, Pane parent) {
Color fill = new Color(rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), 1);
Circle c = new Circle(radius, x, y, fill);
ObjectProperty<Point2D> lo…

How to move the android view by touch drag drop action?

public class MainActivity extends AppCompatActivity implements View.OnTouchListener{ ViewGroup root; Button btn; float offsetX; float offsetY; @Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); root = (ViewGroup)findViewById(R.id.activity_main); btn = new Button(this); btn.setText("Drag Me!"); btn.setWidth(140); btn.setHeight(70); btn.setOnTouchListener(this); root.addView(btn); } public boolean onTouch(View view, MotionEvent event) { float x = event.getRawX(); float y = event.getRawY(); float maxX = root.getWidth() - view.getWidth(); float maxY = root.getHeight() - view.getHeight(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: offsetX = view.getX() - x; offsetY = view.getY() - y; break; case MotionEvent.ACTION_MOVE: …

How to create a bouncing ball in Android?

import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RadialGradient; import android.graphics.Shader; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; class BouncingBall extends View { public Paint p; private static final int R = 30; private int x, y; private int stepX = 50, stepY = 30; public BouncingBall(Context context) { super(context); p = new Paint(); p.setShader(new RadialGradient(x, y,R, Color.GREEN, Color.BLUE, Shader.TileMode.REPEAT)); } @Overrideprotected void onSizeChanged(int w, int h, int oldW, int oldH) { x = w / 2; y = h / 2; } protected void onDraw(Canvas c) { int w = getWidth(); int h = getHeight(); x += stepX; y += stepY; int maxX = w - R; int maxY = h - R; if (x >= maxX) { x = maxX; stepX *= -1; …