I'm using Processing 2.0.3 on Windows 8. I tried to use the following code but I've no idea why my sketch couldn't run when I'm using the processing.video.* library:
import processing.video.*;
Capture cam;
void setup() {
size(200, 200);
cam = new Capture(this);
cam.start();
}
void draw() {
if (cam.available()) {
// Reads the new frame
cam.read();
}
image(cam, 0, 0);
}
I notice that the sketch will get stuck and will not open the sketch applet window at all if I call anything related to the Capture class. Calling println(Capture.list()); for example will cause the sketch to stuck at where ever that line was called.
What do I have to do to resolve this problem?
Related
After many years with Processing IDE, I missed Intellij IDEA too much so I went back for it but Processing stays on :wink:
However, the shapes drawn are way less sharp than in the real PDE, for example, a simple circle is rendered differently using both time the latest version of Processing available 4.1.2, Java 17, the same PC and the same monitor :
In PDE :
public void setup() {
size(500, 500);
}
public void draw() {
background(40);
noStroke();
fill(255);
circle(width/2, height/2, 400);
saveFrame("./PDE.png");
noLoop();
}
and the result is :
With Intellij however :
import processing.core.PApplet;
public class Main extends PApplet {
public void settings() {
size(500, 500);
}
public void draw() {
background(40);
noStroke();
fill(255);
circle(width/2, height/2, 400);
saveFrame("Intellij IDEA.png");
noLoop();
}
public static void main(String... args) {
Main pt = new Main();
PApplet.runSketch(new String[]{"testRendering"}, pt);
}
}
the saveFrame() is exactly the same as with Processing IDE but the real view in the sketch is :
I guess that it is a problem of renderer but I can't change it using fullScreen(P2D) for example because it throws errors.
The only solution I found were using Maven but I am not so I'd rather find a solution for my problem.
This problem arises because the window is being scaled (according to windows scaling settings) but its content is not rendered in a higher resolution (hence the "jaggies").
It's a problem only with the default (Java AWT) renderer. To fix it:
Call System.setProperty("sun.java2d.uiScale", "1") before PApplet.runSketch() -- this will prevent the window from being scaled.
An alternative solution is to use the JavaFX renderer (size(500, 500, FX2D)), which seems to behave correctly (the content renders at a higher resolution).
If however high DPI scaling is not desired with the FX2D renderer, you can call System.setProperty("prism.allowhidpi", "false")to disable it.
I have spent the past couple of days trying to figure out what is wrong with my code. Im trying to render a tile map (.tmx) in libgdx but it does not render and it just shows a red screen with no error. I have re-written the code a few times and I always getting the same result.
my code:
public class Main extends ApplicationAdapter {
SpriteBatch batch;
TiledMap map;
TmxMapLoader loader;
OrthogonalTiledMapRenderer renderer;
OrthographicCamera camera;
#Override
public void create () {
batch = new SpriteBatch();
loader = new TmxMapLoader();
map = loader.load("TiledMaps/TestMap.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
renderer.setView(camera);
renderer.render();
batch.begin();
batch.end();
}
#Override
public void dispose () {
batch.dispose();
renderer.dispose();
map.dispose();
}
}
Can anybody find what is happening here I have the tmx file with the tilesheet in the TileMaps folder in the android assets folder.
Any help is appreciated, Thanks in advance.
If I understand correctly, you're attempting something complex (map rendering) but encountering a simple problem (not rendering anything).
Have you got a simple program where you do have something rendering? Built it up so you have a few hard coded tiles rendering? Built it up a bit more to correctly read and display the first tile from the map? Before finally extending it further to read and display all the tiles, once you know the basics are working?
That's how I would tackle this problem. Been programming for years, and just used the same process to get CreateProcess working in C++ on Windows. Wasn't working within my code so I built an empty project that just runs CreateProcess on notepad.exe and tweaked things with answers from StackOverflow.com till I got it working, now it's about adding back in the other complications bit by bit so I'm not completely lost by taking on too much at once.
Have been trying to work with 'lwjgl' on linux and am running into an issue when I run my compiled code from the terminal. I am using the stable release of lwjgl 3.
I downloaded the lwjgl.jar from the website and run the command javac -cp lwjgl.jar: Main.java
which compiles the code fine. Then I run: java -cp lwjgl.jar: Main after and it throws this error;
[LWJGL] Failed to load a library. Possible solutions:
a) Set -Djava.library.path or -Dorg.lwjgl.librarypath to the directory that contains the shared libraries.
b) Add the JAR(s) containing the shared libraries to the classpath.
[LWJGL] Enable debug mode with -Dorg.lwjgl.util.Debug=true for better diagnostics.
Exception in thread "EndlessRunner" java.lang.UnsatisfiedLinkError: Failed to locate library: liblwjgl.so
at org.lwjgl.system.Library.loadSystemRelative(Library.java:100)
at org.lwjgl.system.Library.loadSystem(Library.java:71)
at org.lwjgl.system.Library.<clinit>(Library.java:43)
at org.lwjgl.system.MemoryAccess.<clinit>(MemoryAccess.java:17)
at org.lwjgl.system.Pointer.<clinit>(Pointer.java:22)
at org.lwjgl.glfw.GLFW.<clinit>(GLFW.java:562)
at Main.init(Main.java:31)
at Main.run(Main.java:78)
at java.lang.Thread.run(Thread.java:745)
I'm not sure if I missed some files that I needed too or if I'm going about this entirely the wrong way. Here is the code I am using, it's just some that I found online and I'm using as a test.
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
import java.nio.ByteBuffer;
import org.lwjgl.glfw.GLFWVidMode;
public class Main implements Runnable{
private Thread thread;
public boolean running = true;
private long window;
private int width = 1200, height = 800;
public static void main(String args[]){
Main game = new Main();
game.start();
}
public void start(){
running = true;
thread = new Thread(this, "EndlessRunner");
thread.start();
}
public void init(){
// Initializes our window creator library - GLFW
// This basically means, if this glfwInit() doesn't run properlly
// print an error to the console
if(glfwInit() != true){
// Throw an error.
System.err.println("GLFW initialization failed!");
}
// Allows our window to be resizable
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
// Creates our window. You'll need to declare private long window at the
// top of the class though.
// We pass the width and height of the game we want as well as the title for
// the window. The last 2 NULL parameters are for more advanced uses and you
// shouldn't worry about them right now.
window = glfwCreateWindow(width, height, "Endless Runner", NULL, NULL);
// This code performs the appropriate checks to ensure that the
// window was successfully created.
// If not then it prints an error to the console
if(window == NULL){
// Throw an Error
System.err.println("Could not create our Window!");
}
// creates a bytebuffer object 'vidmode' which then queries
// to see what the primary monitor is.
//ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// Sets the initial position of our game window.
glfwSetWindowPos(window, 100, 100);
// Sets the context of GLFW, this is vital for our program to work.
glfwMakeContextCurrent(window);
// finally shows our created window in all it's glory.
glfwShowWindow(window);
}
public void update(){
// Polls for any window events such as the window closing etc.
glfwPollEvents();
}
public void render(){
// Swaps out our buffers
glfwSwapBuffers(window);
}
#Override
public void run() {
// All our initialization code
init();
// Our main game loop
while(running){
update();
render();
// Checks to see if either the escape button or the
// red cross at the top were pressed.
// if so sets our boolean to false and closes the
// thread.
if(glfwWindowShouldClose(window) == true){
running = false;
}
}
}
}
Any help you guys can give would be very much appreciated.
Thanks.
I can only speak from using NetBeans 8 to run LWJGL 3, but I also got the same error. The problem I found had to do with the "native" jar files needing to be added to the "classpath" tab when setting up LWJGL initially. The reason for this is that it enables LWJGL to automatically find the native jars. Then under your VM settings you will want to set it to:
-Djava.library.path="Path to where you extracted JAR files"
Only include quotes if your path name includes any spaces
You can add lwjgl.jar to module path and lwjgl-native-\*.jar to classpath. Other too. For example: add lwjgl-opengl.jar to module path and lwjgl-opengl-native-\*.jar to classpath. That's work.
I am working by way through a tutorial: http://www.kilobolt.com/day-4-enter-the-robot.html
and have been having a problem getting a simple image to display in the applet. I am using IntelliJ 13 Community Edition. The main for loading the images is here:
It does the image setup in the init method:
public void init() {
setSize(800, 480);
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("Q-Bot Alpha");
try {
base = getDocumentBase();
} catch (Exception e) {
// TODO: handle exception
}
// Image Setups
character = getImage(base, "data/character.png").toString());
}
where character is a sprite I obtained from the tutorial website. I saved it in a folder called data. The file structure can be seen here:
When I run this I just see a black background and character.png is not displayed. However if I change the getImage line to:
character = getImage(base, new URL("http://www.kilobolt.com/uploads/1/2/5/7/12571940/character.png").toString());
and point at the URL directly it works. I suspect this must be a path issue but I have not been able to get it working.
I am working on the same program and had the same problem. When run, the document base is actually in KiloboltGame/bin so you need to add your data/character.png here.
I am having trouble with ImageIcon().GetImage()
The strange thing is, is that it sometimes works and shows me the window with the maps on it, and other times it doesn't. It also works on other computers flawlessly but not on mine!
I have tried everything, reinstalling Java, reinstalling IntelliJ, also disabling my firewall, but to no avail. I have also written a similar program in C# which works perfectly, which leads me to believe it isn't a permissions error. I have also tested it on a basic Windows XP system with an on board graphics card which also works perfectly.
Here is my code:
public class main {
public static void main(String[] args) {
System.out.println("Running main..");
try
{
URL url = new URL("http://maps.googleapis.com/maps/api/staticmap?center=-33.80382155278416,18.567184266922002&zoom=17&size=1024x1024&maptype=hybrid&sensor=false&format=png&key=AIzaSyCVnp9iTXRSS3ZE5FjzF7uNZavazWhLko4");
Image img=new ImageIcon(url).getImage();
System.out.println("INFO :"+img);
new ImageFrame(img);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static class ImageFrame extends JFrame{
public ImageFrame(Image img){
setPreferredSize(new Dimension(540, 480));
setaImg(img);
ImagePanel somePanel = new ImagePanel(540, 480);
add(somePanel);
setVisible(true);
}
private Image aImg;
public Image getaImg() {
return aImg;
}
public void setaImg(Image aImg) {
this.aImg = aImg;
}
public class ImagePanel extends JPanel{
public ImagePanel(int width, int height){
setPreferredSize(new Dimension(width, height));
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(getaImg(), 0, 0, null); // see javadoc for more info on the parameters
}
}
}
}
I have ran it through the step through debugger and it stops at this line:
Image img=new ImageIcon(url).getImage();
But with no error, it just hangs forever.
I am totally confused as to why it isn't working on my system, and only my system. Any help would be GREATLY appreciated.
Works fine for me
Google's not blocking you are they? From memory you have something like 10,000 queries a day or something.
Try downloading the URL manually using the InputStream (URL.openStream()) and see if you're getting some kind of response other than an image binary.
UPDATED
After investigation, found to be a problem with Java 7 and IPv6 as documented here Downloading files using Java randomly freezes
Always start and update the GUI on the EDT. See Concurrency in Swing for more details.
g.drawImage(getaImg(), 0, 0, null); // see javadoc for more info on the parameters
That comment is very good advice, since a 4 char edit should fix the problem.
g.drawImage(getaImg(), 0, 0, this); // Observer is good for asynchronous image load