string should be drawn - no erros, using vector - java

I want a game over screen to appear on a conditional statement, the below code produces no errors or white letters. It is supposed to be taking a white text and displaying it on a black background but it isn't working. I have even made sure the coordinates are right by using Vector3.
package com.mygdx.game;
import java.util.Random;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Input;
//import com.mygdx.game.IsFinished;
import com.badlogic.gdx.Screen;
import java.util.Random;
import java.util.Timer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.mygdx.game.*;
class GameOverScreen implements Screen{
Label gameoverstring;
private Stage stage;;
SpriteBatch spritebatch;
int placex=0, placey=0;
#Override
public void show() {
stage = new Stage();
BitmapFont white = new BitmapFont(Gdx.files.internal("new.fnt"), false);
LabelStyle headingStyle = new LabelStyle(white, Color.WHITE);
gameoverstring = new Label("game ovaaaa!", headingStyle);
gameoverstring.setFontScale(3);
stage.addActor(gameoverstring);
}
#Override
public void render(float delta) {
Vector3 placeholder = new Vector3(placex, placey, 0);
show();
//gameoverstring.setPosition(0, 0);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameoverstring.setPosition(placeholder.x, placeholder.y);
stage.act(delta);
stage.draw();
System.out.println(gameoverstring.getX()+" "+gameoverstring.getY());
}

After cleaning up the code it is working as it should with my font.
Relevant code:
private Stage stage;
// Called automatically once for init objects
#Override
public void show() {
stage = new Stage();
stage.setDebugAll(true); // Set outlines for Stage elements for easy debug
BitmapFont white = new BitmapFont(Gdx.files.internal("new.fnt"), false);
LabelStyle headingStyle = new LabelStyle(white, Color.WHITE);
Label gameoverstring = new Label("game ovaaaa!", headingStyle);
stage.addActor(gameoverstring);
}
// Called every frame so try to put no object creation in it
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
stage.act(delta);
}
Notice that the Label should be visible in the bottom left corner.
So if you still won't be able to see text, then I guess it is something wrong with your font. Maybe your font in your *.png is black, then I think libGdx isn't able to tint the characters the white color and they stay black. Guess libGdx tint them by multiply the values and 0*x is always 0. To test it you could change the Gdx.gl.glClearColor(0, 0, 0, 1); to green or something.
Also you can look if the green bounding lines from the setDebugAll(true) method are visible.

Related

Fan Orientation Animation Javafx

import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.shape.ArcType;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Arc;
import javafx.scene.paint.Color;
import javafx.scene.control.Button;
import javafx.scene.Group;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.animation.PathTransition;
import javafx.util.Duration;
import javafx.scene.shape.Ellipse;
public class MovingFourFans extends Application
{
private Button btPause = new Button("Pause");
private Button btResume = new Button("Resume");
private Button btReverse = new Button("Reverse");
private Circle fanPanel;
private Ellipse fanPath;
private Arc fan1;
private Arc fan2;
private Arc fan3;
private Arc fan4;
private BorderPane uiContainer;
private HBox buttonContainer;
private PathTransition pt = new PathTransition();
#Override
public void start(Stage primaryStage)
{
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane, 400, 500);
buttonContainer = new HBox(btPause, btResume, btReverse);
buttonContainer.setAlignment(Pos.CENTER);
Group group = new Group();
pane.setCenter(group);
pane.setBottom(buttonContainer);
// include a path transition with a circle inside instead
fanPanel = new Circle(Math.min(pane.getWidth(), pane.getHeight()) / 4);
fanPanel.setFill(Color.WHITE);
fanPanel.setStroke(Color.BLACK);
fanPath = new Ellipse(fanPanel.getRadius() / 100, fanPanel.getRadius() / 100);
fanPath.setVisible(true);
fanPath.setFill(Color.WHITE);
fanPath.setStroke(Color.BLACK);
fan1 = new Arc();
fan1.setType(ArcType.ROUND);
fan1.setRadiusX(fanPanel.getRadius() - 10);
fan1.setRadiusY(fanPanel.getRadius() - 10);
fan1.setStartAngle(80);
fan1.setLength(-10);
fan1.setFill(Color.RED);
group.getChildren().addAll(fanPanel, fanPath, fan1);
pt.setPath(fanPath);
pt.setNode(fan1);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt.setDuration(Duration.seconds(3));
pt.setAutoReverse(false);
pt.play();
btPause.setOnAction(e -> pt.pause());
btResume.setOnAction(e -> pt.play());
primaryStage.setTitle("Control Moving Fan");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
Application.launch(args);
}
}
I'm having a problem trying to create an animation of a fan with four wings with Javafx. I know how to make the buttons work but for the animation every time I start it, the fans moving along the circle path that I created with its middle section not the tip of the section. Because of this the animation doesn't look correct. Does anyone know a fix to this or what method I can use to create the correct animation for this? In this code I only made one wing for test and there are 3 wings left. The screenshot for what I currently have is below.

Painting shapes in JavaFX [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I created a simple paint program which allows the user to choose between 4 shapes(line,circle,rectangle,ellipse) , the user can change the width height and stroke width , he can save the design he makes , undo and redo , the user can also choose the stroke type (solid or dashed) , I'm facing one problem , when the user picks dashed as the stroke type , I want him to be able to choose the thickness of the stroke , it works perfectly fine with the solid , but with the dashed it's just increasing or decreasing the number of dashed lines , any idea on how I can fix the number of dashed lines and increase the thickness of the dashed lines.
NOTE : I'm using the rectangle shape in the code to make the code small.
Here's the CODE :
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
public class GFG extends Application {
#Override
public void start(Stage primaryStage) {
Image image2 = new
Image("C:\\Users\\Mhamd\\Desktop\\laol\\src\\resources\\pngegg.png",100,100,false,false);
ImageView view2 = new ImageView(image2);
view2.setFitHeight(40);
view2.setPreserveRatio(true);
ToggleButton rectbtn = new ToggleButton();
rectbtn.setGraphic(view2);
ToggleButton[] toolsArr = {rectbtn};
ToggleGroup tools = new ToggleGroup();
for (ToggleButton tool : toolsArr) {
tool.setMinWidth(50);
tool.setToggleGroup(tools);
tool.setCursor(Cursor.HAND);
}
ColorPicker cpLine = new ColorPicker(Color.BLACK);
ColorPicker cpFill = new ColorPicker(Color.TRANSPARENT);
TextField textWidth = new TextField("32");
TextField textHeight = new TextField("32");
TextField contouring = new TextField("2");
Label line_color = new Label("Line Color");
Label fill_color = new Label("Fill Color");
Label line_width = new Label("3.0");
Label imgWidth = new Label("Width");
Label imgHeight = new Label("Height");
String week_days[] =
{ "Solid", "Dotted"};
ChoiceBox choiceBox = new ChoiceBox(FXCollections
.observableArrayList(week_days));
VBox btns = new VBox(10);
btns.getChildren().addAll(imgWidth,textWidth,imgHeight,textHeight, line_color, cpLine,
fill_color, cpFill, line_width, contouring,choiceBox);
btns.setPadding(new Insets(5));
btns.setStyle("-fx-background-color: #999");
btns.setPrefWidth(100);
Canvas canvas = new Canvas(1080, 790);
GraphicsContext gc;
gc = canvas.getGraphicsContext2D();
gc.setLineWidth(1);
Rectangle rect = new Rectangle();
canvas.setOnMouseClicked(e->{
if(rectbtn.isSelected()){
double widthSize = Double.parseDouble(textWidth.getText());
double heightSize = Double.parseDouble(textHeight.getText());
double strokeWidth = Double.parseDouble(contouring.getText());
gc.setStroke(cpLine.getValue());
if(choiceBox.getSelectionModel().isSelected(0)){
gc.setLineWidth(strokeWidth);
}
else if(choiceBox.getSelectionModel().isSelected(1)){
//gc.setLineDashes(strokeWidth);
gc.setLineDashOffset(10);
}
gc.setFill(cpFill.getValue());
rect.setX(e.getX() - 20);
rect.setY(e.getY() - 20);
rect.setWidth(widthSize);
rect.setHeight(heightSize);
gc.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
gc.strokeRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
undoHistory.push(new Rectangle(rect.getX(), rect.getY(), rect.getWidth(),
rect.getHeight()));
}
});
}
public static void main(String[] args) {
launch(args);
}
}
You have to set a width for the dashed line too. Setting line dashes to the line width does not make much sense unless you want something that looks more like a dotted line. And you need the dash offset only in specific cases but not in general.

Java3D: I can't see my triangle

I'm very new to Java3D and try to show a triangle, but it does not show up, the frame is completely black.
If i add
bg.addChild(new ColorCube(0.3));
it shows a red square in the middle (so generally showing shapes should work, shouldn't it?)
I don't really know if the problem is with the construction of the triangle itself or with some other part of the View, e.g. the triangle is not in focus, too small, not lit, etc. Do polygons from TriangleArray have to be lit by a source, or do they appear as matt objects?
Here is the code:
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import javax.swing.JFrame;
import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.Color4f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
public class Simulator extends Frame {
Point3f[] testTrianglePoints = {
new Point3f(0.0f ,0.0f, 1.0f),
new Point3f(0.0f, 0.0f, 1.2f),
new Point3f(0.2f, 0.2f, 1.2f)};
Simulator() {
System.out.println("Simulator window initiated");
}
public void run() {
SimpleUniverse u = new SimpleUniverse();
BranchGroup bg = new BranchGroup();
//bg.addChild(new ColorCube(0.3));
TriangleArray t_geo = new TriangleArray(9, TriangleArray.COORDINATES);
t_geo.setCoordinates(0,testTrianglePoints);
GeometryArray t_geoArray = (new GeometryInfo(t_geo)).getGeometryArray();
Shape3D t_shape = new Shape3D(t_geoArray,new Appearance());
bg.addChild(t_shape);
u.addBranchGraph(bg);
u.getViewingPlatform().setNominalViewingTransform();
}
}
`

Moving circle changing color in javafx

I want my ball to change color everytime i click on it, but i wont get it to work. Also im wondering about the movement of my ball. I wonder how you can change the path its going. so it can down up and down and other ways instead of just from left to right.
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.Bloom;
import javafx.scene.effect.Effect;
import javafx.scene.effect.Glow;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.effect.MotionBlur;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.util.Duration;
public class TimelineSample extends Application {
Timeline timeline;
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 280, 120));
Circle circle = new Circle(25, 25, 20, Color.BLUE);
Light.Distant light = new Light.Distant();
light.setAzimuth(-135.0);
Lighting lighting = new Lighting();
lighting.setLight(light);
lighting.setSurfaceScale(5.0);
circle.setEffect(lighting);
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
timeline.getKeyFrames().addAll
(new KeyFrame(Duration.ZERO, new KeyValue(circle.translateXProperty(),
0)),
new KeyFrame(new Duration(4000), new KeyValue(circle
.translateXProperty(), 205)));
root.getChildren().add(circle);
root.requestFocus();
root.setOnKeyPressed(e -> {
if (e.getCode().equals(KeyCode.ENTER)) {
timeline.play();
circle.setFill(Color.PINK);
}
});
root.setOnMousePressed(event -> {
if (circle.contains(event.getX(), event.getY())) {
circle.setFill(Color.BLACK);
if (circle.getFill().equals(Color.BLACK))
circle.setFill(Color.YELLOW);
else if (circle.getFill().equals(Color.BLUE))
circle.setFill(Color.BROWN);
else if (circle.getFill().equals(Color.YELLOW))
circle.setFill(Color.BROWN);
else if (circle.getFill().equals(Color.BROWN))
circle.setFill(Color.BLACK);
}
});
}
#Override
public void stop() {
timeline.stop();
}
#Override
public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
It does not change the color (just once to yellow) because before your check you set it to black and then it is converted to yellow.
if (circle.contains(event.getX(), event.getY())) {
circle.setFill(Color.BLACK); // <-- so it is black
if (circle.getFill().equals(Color.BLACK)) // <-- uhh..it is black..let's change to yellow
circle.setFill(Color.YELLOW);
else if (circle.getFill().equals(Color.BLUE))
circle.setFill(Color.BROWN);
else if (circle.getFill().equals(Color.YELLOW))
circle.setFill(Color.BROWN);
else if (circle.getFill().equals(Color.BROWN))
circle.setFill(Color.BLACK);
}
And it is getting from left to right because your using the X-Property instead of the Y-Property.

Double colored oval

I am a bit new in java and i have this stuff for homework. I have to make something like this
The thing is that i have no idea how to make the circle double colored with yellow and black stuff. Also after that with use of Threads i have to make it rotate counter clock wise. Here is my code for the circle, i know how to create it, just don't know how to multi color it >.< .
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.JComboBox;
import javax.swing.JApplet;
import javax.swing.JSlider;
import java.awt.Color;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
public class Lab4a extends JApplet implements Runnable {
public void init() {
Thread t = new Thread(this);
t.start();
}
public void paint(Graphics g){
super.paint(g);
int w = getWidth();
int h = getHeight();
g.drawOval(25, 35, 200, 200);
g.drawOval(45, 55, 160, 160);
}
}
Have a look at drawArc instead of drawOval. With this, you can paint arcs -- parts of ovals. You can specify the start and end angle of the arc, which can then also be used for drawing it in different states when it needs to rotate.

Categories