Material file won't load when an Object file is loaded - java

So I've been trying to play around with Java 3D and, recently, I've been playing around with importing outside 3D models into a program. At this point, I can get the model into the program as an OBJ file, but for whatever reason, the program won't load the corresponding material file and I don't know if the problem is my coding or if the file just wasn't exported properly.
This is the code I wrote:
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import com.sun.j3d.loaders.objectfile.*;
import com.sun.j3d.loaders.Scene;
import java.awt.Color;
import javax.vecmath.*;
public class ModelLoadingTest {
public static void main(String[] args) {
SimpleUniverse universe = new SimpleUniverse();
BranchGroup scene = new BranchGroup();
ObjectFile loader = new ObjectFile(ObjectFile.LOAD_ALL);
loader.setFlags(ObjectFile.RESIZE);
Scene modelScene = null;
try{
modelScene = loader.load("paintedcar.obj");
}
catch(Exception e){
}
DirectionalLight lighting = new DirectionalLight(new Color3f(Color.WHITE), new Vector3f(0f, 0f, -1f));
lighting.setInfluencingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 1.0), 100));
scene.addChild(modelScene.getSceneGroup());
scene.addChild(lighting);
universe.addBranchGraph(scene);
universe.getViewingPlatform().setNominalViewingTransform();
}
}
If it helps, the models I'm testing with were made in Maya and exported as Wavefront files.

... You are not loading the texture what so ever in your code. The matirl file is not coded in a obj file, you need to import it as a texture, You can do this the same as you would for a sphere except when you assign the texture to the mesh. When you assign it you need to use
"mesh name".setAppearance("your Appearance name");
for example
model.setAppearance(ap);

Related

Resource loading problen in Java

I am new to Java and am having a problem with my JAR file loading some graphic resources. I am using IntelliJ IDEA 2021.2 (Ultimate Edition) to write my code. While in the IDE, the code work fine and the resources are loaded, but once I create the JAR file, and run it, thew graphics don't show. I should mention I do have some graphics that show, as explained below:
package com.troymarkerenterprises;
import javax.swing.*;
import java.awt.*;
import java.util.Objects;
public class ClassTopFrame extends JPanel{
public ClassTopFrame(int screenWidth, int screenHeight) {
this.setBackground(Color.BLACK);
this.setBounds(160, 0, screenWidth-160, screenHeight/2);
JLabel jl=new JLabel();
jl.setIcon(new javax.swing.ImageIcon(Objects.requireNonNull(getClass().getResource("/i_TMEA-0002-J_Logo.png"))));
this.add(jl);
}
}
This class load the image as expected. However when I try to load button images for my menu, the buttoin images do not load. Here is the class I use to create my buttons.
package com.troymarkerenterprises;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
public class TMButton extends JButton {
Font okuda;
public TMButton(String text, String color) {
try {
okuda = Font.createFont(Font.TRUETYPE_FONT, new File("res/f_okuda.ttf")).deriveFont(16.0F);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("res/f_okuda.ttf")));
} catch (IOException | FontFormatException ignored){
}
ImageIcon defaultIcon = new ImageIcon("res/b_" + color + "_default.png");
ImageIcon hoverIcon = new ImageIcon("res/b_" + color + "_selected.png");
ImageIcon disabledIcon = new ImageIcon("res/b_disabled.png");
this.setForeground(Color.BLACK);
this.setFont(okuda);
this.setText("<html><body><br><br>"+text+"</body><html>");
this.setHorizontalTextPosition(SwingConstants.CENTER);
this.setVerticalTextPosition(SwingConstants.CENTER);
this.setIcon(defaultIcon);
this.setSelectedIcon(hoverIcon);
this.setDisabledIcon(disabledIcon);
this.setMinimumSize(new Dimension(124,70));
this.setBorder(null);
this.setBorderPainted(false);
this.validate();
}
}
As I said, I am baffled as to why this is not working. I am not sure if my project structure is wrong, or if I am creating my JAR file incorrectly. To create the JAR file I am going to IntelliJ's project structure option and creating an Artifact. After I build the Artifact, I set the executable bit in my systems file explorer, and run the far file.
I would appreciate any help anyone can suggest. If I have left something out in my explanation of the problem, I have my entire app post on my GitHub page at: https://github.com/troy-marker/TMEA-0002-J.
Thanks for any assistance, Troy.
Problem is solved. Thank you to g00se for pointing out my error.
In one case, you're filling the image icons with resources, and the other you're filling them with files. You should use the former method at all times
I was trying to load the graphics two different ways, and did not realize it. Thank you for the help.

How to plot many datapoints efficiently using javafx or other libraries?

I have 4 series that I want to plot in one linechart. Each series has around 100.000 datapoints. Now, using javafx linechart, it takes several seconds to build the Chart. Not only that it is slow, it also consumes very much memory.
Does anybody has an idea how to build the desired chart faster. If necessary I would use other java libraries too.
Here is some working code. For numDataPoints = 10000 it runs fine, but for numDataPoints = 70000 it's very slow or even crashes.
Thank you in advance.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Chart extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
prepareStage(stage);
}
private void prepareStage(Stage stage) {
stage.setTitle("JavaFX Chart Demo");
StackPane pane = new StackPane();
NumberAxis xAxis = new NumberAxis();
NumberAxis yAxis = new NumberAxis();
ScatterChart ac = new ScatterChart(xAxis, yAxis);
ac.setTitle("Segregation");
ac.setAnimated(false);
ac.setHorizontalGridLinesVisible(false);
ac.setVerticalGridLinesVisible(false);
int numDataPoints = 70000;
for (int k=0; k<4; k++) {
ObservableList<XYChart.Data<Number, Number>> data = FXCollections.<XYChart.Data<Number, Number>>observableArrayList();
for (int i = 0; i < numDataPoints; i++)
data.add(new XYChart.Data<>(i,Math.random() ));
XYChart.Series series = new XYChart.Series(data);
ac.getData().add(series);
}
pane.getChildren().add(ac);
Scene scene = new Scene(pane, 400, 200);
scene.getStylesheets().add("chart.css");
stage.setScene(scene);
stage.show();
}
}
This question has been asked here alread quite often. It is not so much a question of performance but a question of a reasonable tool usage. Do you know how many pixels in a row your monitor device has? If yes, then you can ask yourself the question what you think will remain from your 100.000 data points.
The solution to your problem is to just reduce the amount of data to something reasonable. You can also try to virtualize your display by externally managing the amount and portion of the data that is displayed at a certain point in time, e.g. for scrolling, in a similar way as a TableView would do it.
https://github.com/GSI-CS-CO/chart-fx
was specifically designed having this in mind. Have a look at -- or much better -- try out the examples.
N.B. We use this internally for our accelerator-related data acquisition/display purposes with 10k to 1M data points updated in real-time had a fair Swing-based solution (JDataViewer) but couldn't find anything equivalent compatible with JavaFX ... give it a try... it's free ... and open for contributions and comments.

Morphological closing using java

I am trying to use this source code to do a morphologial closing on an image (with canny edge detected)
https://code.google.com/p/doccrop/source/browse/DocCrop/src/imageanalysis/morphology/Closing.java?r=3
I've created an instance of Closing and I've applied it on a BufferedImage and then draw it but I get a black image as a result!!
At first I had an error saying that the image must be type_byte_gray so I used this to change type but I guess it doesn't work
BufferedImage img1= ImageIO.read(new File(path));
BufferedImage imge = new BufferedImage(img1.getHeight(),img1.getWidth(),BufferedImage.TYPE_BYTE_GRAY);
You might use an image processing framework, like Marvin or ImageJ, for such purpose, as shown below.
input:
output:
source code:
import marvin.image.MarvinColorModelConverter;
import marvin.image.MarvinImage;
import marvin.io.MarvinImageIO;
import marvin.math.MarvinMath;
import static marvin.MarvinPluginCollection.*;
public class ClosingDemo {
public ClosingDemo(){
MarvinImage image = MarvinImageIO.loadImage("./res/closingIn.png");
image = MarvinColorModelConverter.rgbToBinary(image, 127);
morphologicalClosing(image.clone(), image, MarvinMath.getTrueMatrix(60, 60));
MarvinImageIO.saveImage(image, "./res/closingOut.png");
}
public static void main(String[] args) {
new ClosingDemo();
System.exit(0);
}
}

Can't get lists working using Slick2d and Java

Could anyone help me with why I'm unable to add to either of the lists created? The second list is only created to illustrate the problem. Thanks for any help!
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.Image;
public class Snake {
private Image head = new Image("data/head.png");
private static Image body = new Image("data/body.png");
ArrayList<Point> snakeCoords = new ArrayList<Point>(3);
Point headCoord = new Point(300, 300);
Point firstBody = new Point(300, 325);
Point secondBody = new Point(300, 350);
snakeCoords.add(headCoord);
List<String> list = new ArrayList<String>();
list.add("Does this work");
}
If the code you posted is accurate, you need to have the method calls placed in a method or the constructor. You can initialize variables outside of methods, but that's it.

How to use Jzy3d java 3d chart library?

Can someone please give me some extra basic example of how jzy3d should be used?
(The source site's examples don't seam to work for me)
I tried the following code:
import org.jzy3d.chart.Chart;
import org.jzy3d.colors.Color;
import org.jzy3d.colors.ColorMapper;
import org.jzy3d.colors.colormaps.ColorMapRainbow;
import org.jzy3d.maths.Range;
import org.jzy3d.plot3d.builder.Builder;
import org.jzy3d.plot3d.builder.Mapper;
import org.jzy3d.plot3d.builder.concrete.OrthonormalGrid;
import org.jzy3d.plot3d.primitives.Shape;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
Chart chart = getChart();
frame.add((javax.swing.JComponent) chart.getCanvas());
frame.setSize(800, 800);
frame.setLocationRelativeTo(null);
frame.setTitle("test");
frame.setVisible(true);
}
public static Chart getChart() {
// Define a function to plot
Mapper mapper = new Mapper() {
public double f(double x, double y) {
return 10 * Math.sin(x / 10) * Math.cos(y / 20) * x;
}
};
// Define range and precision for the function to plot
Range range = new Range(-150, 150);
int steps = 50;
// Create the object to represent the function over the given range.
org.jzy3d.plot3d.primitives.Shape surface = (Shape) Builder.buildOrthonormal(new OrthonormalGrid(range, steps, range, steps), mapper);
surface.setColorMapper(new ColorMapper(new ColorMapRainbow(), surface.getBounds().getZmin(), surface.getBounds().getZmax(), new Color(1, 1, 1, .5f)));
surface.setWireframeDisplayed(true);
surface.setWireframeColor(Color.BLACK);
//surface.setFace(new ColorbarFace(surface));
surface.setFaceDisplayed(true);
//surface.setFace2dDisplayed(true); // opens a colorbar on the right part of the display
// Create a chart
Chart chart = new Chart();
chart.getScene().getGraph().add(surface);
return chart;
}
}
But when I try to run it, I get that exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jogl in java.library.path
Can anyone help?
You should add jogl.jar to classpath and jogl.dll to PATH.
For more info look here and here.
You can read jogl Installation Instructions here.
You should run your program or demo where the JOGL native libraries stand, i.e. ./bin/{platform}. If you are working with Eclipse, right click on the project, choose Propeties, Java Build Path then the Libraries tab. Under the item "jogl.jar - ..." select "Native library location: (None)" and click the Edit button. Press the Workspace... button and select the ./bin/{platform} folder.

Categories