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();
}
}
`
Related
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.
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.
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.
What I'm trying to do is simple. I have a JLayeredPane with two panels inside of it. Panel 2 (higher Z index) has a transparent background and only displays a Line2D element that goes from Y = 0 to Y = max. I need the X value to increment every so many milliseconds, and then redraw the line. I have everything set up to do so, except I can't figure out how to do the bar movement via timing.
I've done some research and every time I saw mentions of the timer class (Which I feel would be able to accomplish what I'm trying to do) people recommend not using it. I can't figure out an alternative to using the timer class in order to slide my bar across the screen.
Hope this code helps you. It does exactly what you want.
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.Arrays;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class FloorPlaner extends JFrame {
public int x=0;
public FloorPlaner(){
super("FloorPlaner");
requestFocus();
setContentPane(new DrawingPane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setResizable(true);
setVisible(true);
while (true) {
x++;
repaint();
try {
Thread.sleep(40); //25 FPS
} catch(InterruptedException bug) {
Thread.currentThread().interrupt();
System.out.println(bug);
}
}
}
class DrawingPane extends JPanel { //Where you actually draw on
public void paintComponent(Graphics g) { //Drawing method
g.drawLine(x,0,x,400);
}
}
public static void main(String args[]) {
new FloorPlaner(); //Start it
}
}
Working with GXT and Java trying to learn. Just testing it through eclipse takes me to a blank page no button, I have used the buttons sample from sencha and it shows like there, then when I take what I have so far learned to be necessary I still am unable to produce a single text button.
package com.custom.test.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.sencha.gxt.cell.core.client.ButtonCell.ButtonScale;
import com.sencha.gxt.core.client.resources.ThemeStyles;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.ContentPanel.ContentPanelAppearance;
import com.sencha.gxt.widget.core.client.FramedPanel.FramedPanelAppearance;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.button.CellButtonBase;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class Test implements EntryPoint {
#Override
public void onModuleLoad() {
VerticalPanel vp = new VerticalPanel();
ContentPanel cp = new ContentPanel();
vp.setSpacing(10);
vp.setWidth("400px");
HorizontalPanel hp = new HorizontalPanel();
hp.setSpacing(5);
Buttons b = new Buttons();
CellButtonBase<?> small = b.createButton(Buttons.Category.NORMAL, Buttons.Type.TEXT);
b.configureButton(small, Buttons.Type.TEXT, ButtonScale.SMALL);
hp.add(small);
vp.add(hp);
cp = new ContentPanel(GWT.<ContentPanelAppearance> create(FramedPanelAppearance.class));
cp.addStyleName("margin-10");
cp.setPixelSize(500, 400);
cp.getBody().getStyle().setBackgroundColor("white");
cp.getBody().addClassName(ThemeStyles.getStyle().border());
cp.add(vp);
cp.show();
}
}
Not sure if this is needed, but try this
cp.add(b);
? maybe ?
EDIT:
package com.custom.test.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.sencha.gxt.cell.core.client.ButtonCell.ButtonScale;
import com.sencha.gxt.core.client.resources.ThemeStyles;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.ContentPanel.ContentPanelAppearance;
import com.sencha.gxt.widget.core.client.FramedPanel.FramedPanelAppearance;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.button.CellButtonBase;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class Test implements EntryPoint {
#Override
public void onModuleLoad() {
VerticalPanel vp = new VerticalPanel();
ContentPanel cp = new ContentPanel();
vp.setSpacing(10);
vp.setWidth("400px");
HorizontalPanel hp = new HorizontalPanel();
hp.setSpacing(5);
Buttons b = new Buttons();
CellButtonBase<?> small = b.createButton(Buttons.Category.NORMAL, Buttons.Type.TEXT);
b.configureButton(small, Buttons.Type.TEXT, ButtonScale.SMALL);
hp.add(small);
vp.add(hp);
vp.add(b); //you are adding everything except the actual button
cp = new ContentPanel(GWT.<ContentPanelAppearance> create(FramedPanelAppearance.class));
cp.addStyleName("margin-10");
cp.setPixelSize(500, 400);
cp.getBody().getStyle().setBackgroundColor("white");
cp.getBody().addClassName(ThemeStyles.getStyle().border());
cp.add(vp);
cp.show();
}
}