Using eclipse, I'm trying to write a simple hello world program in processing that simply draws a rectangle on the screen then has gravity drop it as seen in this Tutorial.
The problem is that when I try to import the p5 package, it's not resolving so I can't declare my Physics object. I tried two things.
Download the zip, unzip it, then import the 3 jars (library, serialization, & testbed)
a. import org.jbox2d.p5.*; doesn't resolve but the others do
b. Physics physics; doesn't resolve
Download the older standalone testbed jar then import it
a. Physics physics; doesn't resolve;
Here is basically where I'm starting
import org.jbox2d.util.nonconvex.*;
import org.jbox2d.dynamics.contacts.*;
import org.jbox2d.testbed.*;
import org.jbox2d.collision.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.p5.*;
import org.jbox2d.dynamics.*;
import processing.core.PApplet;
public class MyFirstJBox2d extends PApplet {
Physics physics;
public void setup() {
size(640,480);
frameRate(60);
initScene();
}
public void draw() {
background(0);
if (keyPressed) {
//Reset everything
physics.destroy();
initScene();
}
}
public void initScene() {
physics = new Physics(this, width, height);
physics.setDensity(1.0f);
physics.createRect(300,200,340,300);
}
}
Everything is in the ZIP file from the tutorial.
All classes in org.jbox2d.p5 are contained in boxwrap2d.jar. Also add Physics.java (plus the other dependent java files) to your source path.
Related
I' am trying to replace this code:
public void doThing(){
if(User.getInstance().isLoggin()){
.....
}
}
by this
#withUser
public void doThing(){
....
}
I saw about the interceptors and annotation, but I cannot do it.
Is this possible?
It is absolutely possible. Project Lombok provides the exact feature what you are looking for. You just need to create your own annotation by extending EclipseAnnotationHandler or JavacAnnotationHandler depending upon your requirement.
A sample annotation handler (WithUser) is given below for reference. If used on method, the entire method will become enclosed by if(false){...} block. Replace false with your own expression on Handler (HandleWithUser.java).
#WithUser
public void doThing(){
System.out.println("Hello World");
}
After annotation processing
public void doThing(){
if (false){
System.out.println("Hello World");
}
}
Setup Project Lombok
git clone https://github.com/rzwitserloot/lombok.git
cd lombok
ant eclipse
Open the Lombok project on Eclipse and create the following files.
WithUser.java
package lombok;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
#Retention(RetentionPolicy.SOURCE)
public #interface WithUser {
}
HandleWithUser.java
package lombok.javac.handlers;
import static lombok.javac.handlers.JavacHandlerUtil.setGeneratedBy;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.IfStatement;
import org.eclipse.jdt.internal.compiler.ast.Statement;
import org.mangosdk.spi.ProviderFor;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCIf;
import com.sun.tools.javac.tree.JCTree.JCLiteral;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import org.eclipse.jdt.internal.compiler.ast.FalseLiteral;
import lombok.WithUser;
import lombok.core.AnnotationValues;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacNode;
import lombok.javac.JavacTreeMaker;
#ProviderFor(JavacAnnotationHandler.class)
public class HandleWithUser extends JavacAnnotationHandler<WithUser>{
#Override
public void handle(AnnotationValues<WithUser> annotationValues, JCAnnotation ast, JavacNode annotationNode) {
JavacNode annotation = annotationNode.up();
JCMethodDecl method = (JCMethodDecl)annotation.get();
List<JCStatement> contents = (List<JCStatement>) method.body.stats;
JCTree source = annotation.get();
JavacTreeMaker maker = annotationNode.getTreeMaker();
JCLiteral falseLiteral = maker.Literal(false);
JCIf ifStatement = maker.If(falseLiteral, maker.Block(0, contents), null);
Context context = annotationNode.getContext();
JCStatement ifBlock = setGeneratedBy(ifStatement, source, context);
method.body.stats = List.of(ifBlock);
annotation.rebuild();
}
}
Generate lombok.jar
ant dist
Test
Make sure you are including lombok.jar in classpath
javac -cp lombok.jar Test.java
For more info, visit https://projectlombok.org/contributing/contributing
I have two maven projects "Bonita-engine"and "activity-engine". these projects are the code source of two BPM engine. My project is to find the common method of these two engine code sources.So I create an API Java to call this Java method.
as these methods are on a different project I can not call this method. in fact, I added this two project to the library of my API Java but it doesn't work.
call method getDescription() from bonita-engine maven project
* Copyright (C) 2015 BonitaSoft S.A.
package org.bonitasoft.engine.bpm.process.impl.internal;
import java.util.Date;
import org.bonitasoft.engine.bpm.internal.NamedElementImpl;
import org.bonitasoft.engine.bpm.process.ProcessInstance;
/**
* #author Baptiste Mesta
* #author Matthieu Chaffotte
* #author Celine Souchet
*/
public class ProcessInstanceImpl extends NamedElementImpl implements ProcessInstance {
#Override
public String getDescription() {
return description;
}
}
call method getDescription() from activiti-engine maven project
package org.activiti.engine.impl.persistence.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.impl.bpmn.data.IOSpecification;
import org.activiti.engine.impl.context.Context;
public class ProcessDefinitionEntityImpl extends AbstractEntity implements ProcessDefinitionEntity, Serializable {
public ProcessInstanceImpl(final String name) {
super(name);
}
public String getDescription() {
return description;
}
}
API JAVA : call the common method from the two maven project
import org.activiti.bpmn.model.*;
import org.bonitasoft.engine.bpm.process.impl.internal.* ;
import org.bonitasoft.engine.bpm.*;
import java.util.*;
import java.util.Date;
import org.bonitasoft.engine.bpm.internal.*;
import org.bonitasoft.engine.bpm.process.* ;
public class apicommon {
public activitiProcess = new ProcessDefinitionEntityImpl() ;
public String name;
public bonitaProcess = new ProcessInstanceImpl(name) ;
public enum bpm {
activiti , bonita
}
bpm chose ;
public apicommon() {
}
public String getProcessDescription() {
if(chose==bpm.activiti){
return activitiProcess.getDescription() ;
}else if(chose==bpm.bonita){
return bonitaProcess.getDescription();
}
}
i import the package "org.bonitasoft.engine.bpm.process.impl.internal" and the package "org.activiti.engine.impl.persistence.entity" but i can not get access to ProcessInstanceImpl method and ProcessDefinitionEntityImpl method !
#sara, you have to add the 2 projects to the build path of the API java(api common).
Right click on the (api common) java project => Build path => Configure Build Path
Click on the projects tab
Click Add button
add your project1 (bonita) by checking the box next to it
Again add project2 (activiti-engine) usign the same method
Click Apply and OK to close the dialog.
Now your imports should be working.
Edit:
You have not been clear about which import is the problem. More information is necessary to get to the root of the problem.
As regarding the import error, you can Ctrl + click the offending import. This will take you to a page Source Not Found and a button labelled Attach Source. Click the button to search for the location of what I am suspect is an external jar.
package com.test.model.listener;
import org.osgi.service.component.annotations.Component;
import com.google.gson.InstanceCreator;
import com.liferay.portal.kernel.exception.ModelListenerException;
import com.liferay.portal.kernel.model.BaseModelListener;
import com.liferay.portal.kernel.model.ModelListener;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.model.*;
#Component(immediate = true, service = ModelListener.class)
public class InsertInstanceModelListener extends BaseModelListener<Instance??> {
#Override
public void onAfterCreate(Instance?? model) throws ModelListenerException {
System.out.println("InsertInstanceModelListener.onAfterCreate()");
super.onAfterCreate(model);
}
}
I'm newbie of liferay.I think it's may be something like this,but don't know how to make it right.
You are on your way. This OSGi component needs to be specific,
you are probably looking for com.liferay.portal.kernel.model.VirtualHost
If I understood correctly.
You do not need to call supper though.
I've two classes under the same package Class names are "TestPlugin" and "Pokemon". The error I get is in the class TestPlugin at line 7 where there's written "New Pokemon". The error is "Cannot be resolved to a variable". I want the TestPlugin to acces the code in Pokemon so it can be used. What should I do to fix this problem? New to bukkit plugin creation so don't make the answer too advanced please. "I don't own this code/plugin. I've it for educational purposes only!". If you wonder what bukkit libary I'm using, it's the recommended build "craftbukkit-1.6.4-R2.0".
TestPlugin's code:
package com.hotmail.marrunsilkeborg.plugins.testplugin;
import org.bukkit.plugin.java.JavaPlugin;
public class TestPlugin extends JavaPlugin{
public void onEnable(){
getServer().getPluginManager().registerEvents(new Pokemon, this);
}
}
Pokemon's code:
package com.hotmail.marrunsilkeborg.plugins.testplugin;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class Pokemon implements Listener{
#EventHandler
public void onBlockPlace(BlockPlaceEvent event){
Player p = event.getPlayer();
Block bp = event.getBlockPlaced();
p.sendMessage("You've placed a " + bp.getType().toString());
}
}
Change line 7 to
this.getServer().getPluginManager().registerEvents(new Pokemon(this), this);
also think about adding a on disable
You wanted to call Pokemon's constructor, so use
new Pokemon() with the parentheses.
As #Welsar55 mentioned, use new Pokemon(this) if you are referencing your plugin in the Pokemon constructor (common-practice for Java plugins), i.e. where your Pokemon constructor is:
public Pokemon(TestPlugin myPlugin) {
this.plugin = myPlugin;
}
I just downloaded the play framework from their site and am working through this tutorial.
I've noticed the framework creates the folders app/controllers and app/views, but not a models folder. I created it manually and added Task.java to it. When I get to the section entitled "Rendering the first page" and open localhost:9000/tasks I get a compilation error that says package play.models does not exist. Here is what my Task.java looks like:
package models;
import java.util.*;
public class Task {
public Long id;
#Required
public String label;
public static List<Task> all() {
return new ArrayList<Task>();
}
public static void create(Task task) {
}
public static void delete(Long id) {
}
}
Here is application.java, the file generating the compilation error:
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
import play.data.*;
import play.models.*; // COMPILATION ERROR: "package play.models does not exist"!
public class Application extends Controller {
static Form<Task> taskForm = Form.form(Task.class);
public static Result index() {
//return ok(index.render("Your new application is ready."));
return redirect(routes.Application.tasks());
}
public static Result tasks() {
return ok(views.html.index.render(Task.all(), taskForm));
}
public static Result newTask() {
return TODO;
}
public static Result deleteTask(Long id) {
return TODO;
}
}
I believe it's supposed to be import models.Task; as opposed to import play.models.*;
That's quite confusing (IMHO) step in this tutorial, instead scroll down to Persist the tasks in a database section which describes preparing a model to cooperate with DB :) (it extends Model class, uses proper annotations, etc)
As you recognized it yet, you need to create a models package yourself.
Also as cYn wrote: you should import models like models.SomeModel into your controller
You are correct HukeLau_DABA , the Play will not create the models package for you. you have to create it.
I got these imports in my Application controller class. I got this sample play application running.
import play.api._
import play.api.mvc._
import play.api.data.Form
import play.api.data.Forms._
import models.Task
and another thing in Eclipse is it will not import the necessary imports automatically.
it is bit pain now, once the IDE support get better I hope this will change.