Passing an Enum to a method as a parameter in Java? - java

I am trying to call a docx4j method "setAlgn" in the interface CTTextParagraphProperties, which, per the docx4j jar I am using and the compiler takes an Enum type as a parameter. I am passing the actual argument STTextAlignType.CTR which I believe should resolve to the value 'ctr' (citation: http://grepcode.com/file/repo1.maven.org/maven2/org.docx4j/docx4j/3.0.1/org/docx4j/dml/STTextAlignType.java?av=f, I am running this same code).
Here is my code:
import java.lang.Enum;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFChildAnchor;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
import org.apache.poi.xssf.usermodel.XSSFShapeGroup;
import org.apache.poi.xssf.usermodel.XSSFTextBox;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.docx4j.dml.*;
public static XSSFTextBox createTextBox(XSSFSheet sh, String message, int row1, int col1, int row2, int col2, boolean is_bold, boolean is_italics, boolean is_underline, boolean centered, int fontSize){
//Various apache-poi stuff
XSSFWorkbook wb = sh.getWorkbook();
XSSFDrawing drawing = sh.createDrawingPatriarch();
XSSFClientAnchor clientanchor = new XSSFClientAnchor(0,0,0,0,(short)col1,row1,(short)col2,row2);
XSSFChildAnchor childanchor = new XSSFChildAnchor(0,0,0,0);
XSSFShapeGroup group = drawing.createGroup(clientanchor);
XSSFTextBox textbox = group.createTextbox(childanchor);
XSSFRichTextString richMessage = new XSSFRichTextString(message);
XSSFFont textFont = wb.createFont();
textFont.setFontHeightInPoints((short)fontSize);
textFont.setFontName("Verdana");
if(is_bold){
textFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
}
textFont.setItalic(is_italics);
if(is_underline){
textFont.setUnderline(XSSFFont.U_SINGLE);
}
if(centered){
//Here is the code in question.
textbox.getCTShape().getTxBody().getPArray(0).getPPr().setAlgn(STTextAlignType.CTR);
}
richMessage.applyFont(textFont);
textbox.setText(richMessage);
return textbox;
}
My compiler returns the following error message:
com\tem\POIStuff.java:1105: error: method setAlgn in interface CTTextParagraphProperties cannot be applied to given types;
textbox.getCTShape().getTxBody().getPArray(0).getPPr().setAlgn(STTextAlignType.CTR);
Ultimately, my question is how can I get the "setAlgn" method to accept 'STTextAlignType.CTR' as an Enum and not as object type 'STTextAlignType'? Thank you in advance very much for your help!

The problem is actually on the first line of your code snippet! Your issue is with
import java.lang.Enum;
CTTextParagraphProperties.setAlgn does take a class of type Enum, but it's not that kind of Enum. It has to be a org.openxmlformats.schemas.drawingml.x2006.main.STTextAlignType.Enum
I'd suggest you switch your imports to be:
import org.openxmlformats.schemas.drawingml.x2006.main.STTextAlignType;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextAlignType.Enum;
You can then set the alignment with things like STTextAlignType.L and it'll work fine

Related

Have a problem with packages not getting recognized [duplicate]

This question already has answers here:
The declared package does not match the expected package ""
(25 answers)
Closed 3 years ago.
I am trying to create a puzzle solving algorithm with an A* search. everything should be right but I got a problem when I try to use one of the new "MoveUp MoveDown MoveLeft MoveRight" classes that I implemented i get an error that (the declared package "rushhour" does not match the expected package)
MoveDown
package rushhour;
import search.Action;
import search.State;
public class MoveDown implements Action{
int carNum;
int nPositions;
public MoveDown(int carNum, int nPositions){
this.carNum = carNum;
this.nPositions = nPositions;
}
public int getCost() {
return 1;
}
public String toString(){
return "move down";
}
}
then the class I use it in looks like this
package rushhour;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.lang.model.element.Element;
import search.Action;
import search.State;
public class GameState implements search.State {
boolean[][] occupiedPositions;
List<Car> cars; // target car is always the first one
int nrRows;
int nrCols;
...
...
...
public boolean isLegal(Action action) {
if(action instanceof MoveDown){
Car car = cars.get(((MoveDown) action).carNum);
int nextPos = car.getRow() + car.getLength() + ((MoveDown) action).nPositions;
Is is expected that your folder hierarchy matches your package hierarchy, (and vice-versa).
If you classpath is /src/main/java/ your files should look like this:
/src/main/java/rushour/GameState.java
/src/main/java/rushour/MoveDown.java
/src/main/java/search/Action.java
/src/main/java/search/State.java

libgdx not initialized yet

I have been coding my libgdx application (for desktop only) for a while and after deciding to cleanup the code part by part iv'e gotten into a problem i cant seem to solve myself..
exception:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.UnsatisfiedLinkError: com.badlogic.gdx.physics.box2d.PolygonShape.newPolygonShape()J
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)
Caused by: java.lang.UnsatisfiedLinkError: com.badlogic.gdx.physics.box2d.PolygonShape.newPolygonShape()J
at com.badlogic.gdx.physics.box2d.PolygonShape.newPolygonShape(Native Method)
at com.badlogic.gdx.physics.box2d.PolygonShape.<init>(PolygonShape.java:29)
at com.mygdx.game.handler.BodyEditorLoader.<init>(BodyEditorLoader.java:41)
at com.mygdx.game.util.GameUtils.init(GameUtils.java:23)
at com.mygdx.game.DungeonLife.create(DungeonLife.java:168)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
after googling for a while i figured my error is as mentioned in this thread
as in
Another problem might be that you instantiate a SpriteBatch (or something else which internally uses a SpriteBatch) too early (looked a bit like this in the stacktrace).
but as the answer mentions
Instead, create such things in the create/show methods of your game.
I cant seem to understand when libgdx is initialized and ready for use, and where to place my GameUtils.init() method to assure libgdx is initialized
My code is as follows: (i have taken out e-relevant methods)
Application Class
package com.mygdx.game;
import box2dLight.RayHandler;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
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.TextureRegion;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.mygdx.game.entity.MobEntity;
import com.mygdx.game.entity.PhysicalEntity;
import com.mygdx.game.entity.Player;
import com.mygdx.game.entity.Weapon;
import com.mygdx.game.handler.*;
import com.mygdx.game.util.CollisionConstants;
import com.mygdx.game.util.GameUtils;
import com.mygdx.game.util.TileObjectUtil;
import com.mygdx.game.util.WorldConstants;
import com.mygdx.game.valtype.WeaponDefinition;
public class DungeonLife extends ApplicationAdapter implements WorldConstants {
OrthographicCamera camera;
float width , height;
Texture texture;
TextureRegion[] enttex;
//TEMP
MobEntity demo;
Player thePlayer;
PlayerInputProcessor playerInputProcessor;
ScreenUI ui;
int mapWidth , mapHeight;
//========================================
GameMap gameMap;
//========================================
#Override
public void create () {
texture = new Texture("maps/img/tileset_entity.png");
enttex = new TextureRegion[(int) ((texture.getWidth()*texture.getHeight()) / (BLOCK*BLOCK))];
enttex[0] = new TextureRegion(texture , 0 , 0 , (int)BLOCK , (int)BLOCK);
enttex[1] = new TextureRegion(texture , (int)BLOCK , 0 , (int)BLOCK , (int)BLOCK);
width = Gdx.graphics.getWidth()/5;
height = Gdx.graphics.getHeight()/5;
camera = new OrthographicCamera(width,height);
camera.position.set(width / 2, height / 2, 0);
camera.update();
GameUtils.init(); // <------this guy
//init();
} ...
GameUtils
package com.mygdx.game.util;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.World;
import com.mygdx.game.entity.PhysicalEntity;
import com.mygdx.game.handler.*;
import java.util.PriorityQueue;
public class GameUtils {
public static void init(){
weapon_bodyEditorLoader = new BodyEditorLoader(Gdx.files.internal("textures/weapons/dungeonlife_weapons.json"));
resourceManager = new ResourceManager();
resourceManager.addRes("friendlyhealth" , new Texture("textures/ui/friendlyhealth.png"));
resourceManager.addRes("enemyhealth" , new Texture("textures/ui/enemyhealth.png"));
tmxMapLoader = new TmxMapLoader();
gameContactListender = new GameContactListender();
}
//GLOBAL
public static BodyEditorLoader weapon_bodyEditorLoader;
public static GameContactListender gameContactListender;
public static ResourceManager resourceManager;
public static TmxMapLoader tmxMapLoader;
//CURRENTS ============================
public static GameMap CURRENT_GAMEMAP;
}
Desktop launcher (the usual)
package com.mygdx.game.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.DungeonLife;
import com.mygdx.game.util.GameUtils;
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.useGL30 = false;
config.width=640;
config.height=360;
DungeonLife dungeonLife = new DungeonLife();
new LwjglApplication(dungeonLife, config);
}
}
Help is much appriciated! :D
As I've already wrote in the other linked answer, make sure that your project is correctly set up. Box2D is an extension and needs its own native libraries to be able to run.
It seems that it's a specific problem with the Box2D natives which are not loaded yet. They are not loaded automatically when LibGDX is initializing, but you have to trigger that yourself (see wiki).
The code you've posted looks correct, but before you can use anything Box2D related, you have to call Box2D.init(). Alternatively, creating a World object does the same, but isn't the nicest way to do it.

org.apache.struts.ServletException :JavaBean is not found

import java.sql.SQLException;
import java.util.ArrayList;
public class UserSearchJB {
public UserSearchJB(){
System.out.println("UserSearchJB");
}
public ArrayList searchRecords(UserDTO udto) throws SQLException{
UserSearchDAO usdao = new UserSearchDAO();
ArrayList list = usdao.searchRecords(udto);
return list;
}
}
//This is the code for javabean using struts flow.
This is for model to transfer the data one component to another component
What is the problem in this program i think it is a basic program in which import two spacial package import java.sql.SQLException; and
import java.util.ArrayList;

Syntax error on token for a "private static final"

I'm so sick of android errors in eclipse.. this has been happening for too long but here it goes.
here's the code
import android.app.Activity;
import android.os.Bundle;
import android.R;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
//constants used when saving/restoring state
private static final String BILL_TOTAL = "BILL_TOTAL";
private static final String CUSTOM_PERCENT = "CUSTOM_PERCENT";
I just threw the imports in to see where the code starts
the first private static final (BILL_TOTAL) errors like this:
Multiple markers at this line
- Syntax error on token(s), misplaced
construct(s)
- Syntax error on tokens, delete these
tokens
- Syntax error on tokens, delete these
tokens
I delete the first private static final then the error jumps to the next static method declaration... ugh.. the code is 90% done and I know there are no errors.. (if anything a curly brace somewhere)..
Please help me understand these random eclipse errors.
thanks in advance
Also... in the midst of all this i lost access to my main layout
main cannot be resolved or is not a field
also one last thing
NONE of my variables "can be resolved as a variable"
Those private static final variables need to be inside of a class context:
import android.app.Activity;
import android.os.Bundle;
import android.R;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
class MyClass {
//constants used when saving/restoring state
private static final String BILL_TOTAL = "BILL_TOTAL";
private static final String CUSTOM_PERCENT = "CUSTOM_PERCENT";
...
}

Importing enums in GWT

I have the following code.
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
public class LayoutPanelExample implements EntryPoint{
#Override
public void onModuleLoad() {
Widget childone = new HTML("left"),childtwo=new HTML("right");
LayoutPanel p = new LayoutPanel();
p.add(childone);
p.add(childtwo);
p.setWidgetLeftWidth(childone, 0, PCT, 50, PCT);
p.setWidgetRightWidth(childtwo, 0, PCT, 50, PCT);
RootLayoutPanel rp = RootLayoutPanel.get();
rp.add(p);
}
}
But it shows me this error:
C:\XAMPP\xampp\htdocs\LayoutPanelExample\src\java\LayoutPanelExample.java:19: cannot find symbol
symbol : variable PCT
location: class LayoutPanelExample
p.setWidgetLeftWidth(childone, 0, PCT, 50, PCT);
But I have seen on the Internet that it is possible to declare PCT like this. Should I import some addition header or what to do?
You've forgotten to import PCT.
import static com.google.gwt.dom.client.Style.Unit.PCT;
You should do a static import:
import static com.google.gwt.dom.client.Style.Unit.*;
But like I mentioned in the comment - it's better IMHO to explicitly refer to enums - at least when their names are short ;)

Categories