public static HashMap<Integer, Point> fillCellsCreatures = new HashMap<Integer, Point>();
for(int i = 0; i < 4; i++){
Creature cre = new Creature();
cre.x = ((int)(Math.random() * ((30 - 10) +1)));
cre.y = ((int)(Math.random() * ((30 - 10) +1)));
cre.energy_level = 10;
//fillCellsCreatures.add(new Point(cre.x, cre.y));
fillCellsCreatures.put(cre.energy_level, new Point(cre.x, cre.y));
}
The code above is me trying to add to a hashmap that contains an int as the key and a Point as the value. When i add to the hashmap via the for loop it appears i am constantly rewriting over the same position and rather not moving to the next position to add the new value. Could someone please help clarify what i am doing wrong and point me in the right direction.
Cheers
If you need to change use following code,
public static HashMap<Integer, Point> fillCellsCreatures = new HashMap<Integer, Point>();
for(int i = 0; i < 4; i++){
Creature cre = new Creature();
cre.x = ((int)(Math.random() * ((30 - 10) +1)));
cre.y = ((int)(Math.random() * ((30 - 10) +1)));
cre.energy_level = 10 * i+1;
//fillCellsCreatures.add(new Point(cre.x, cre.y));
fillCellsCreatures.put(cre.energy_level, new Point(cre.x, cre.y));
}
Related
I am using 1D array to get the final answer, but I also need to get selected items. How to achieve that?
private static int UnboundedKnapsack(int capacity, int n, int[] itemValue, int[] itemWeight)
{
int[] dp = new int[capacity + 1];
for (int i = 0; i <= capacity; i++)
{
for (int j = 0; j < n; j++)
{
if (itemWeight[j] <= i)
{
dp[i] = Math.Max(dp[i], dp[i - itemWeight[j]] + itemValue[j]);
}
}
}
return dp[capacity];
}
Let's introduce a new path function that gives the optimal selcetions of items using the previously calculated dp array.
private static void path(int capacity, int n, int[] itemValue, int[] itemWeight, int[] dp){
if(capacity == 0) return; // here you handle when the function will end. I assume capacity should be empty at the last
int ans = 0, chosenItem;
for(int j = 0; j < n; j++){
int newAns = dp[capacity - itemWeight[j]] + itemValue[j];
if(newAns > ans){
ans = newAns;
chosenItem = j;
}
}
printf("%d ",chosenItem); // here you get the current item you need to select;
path(capacity - itemWeight[chosenItem], n, itemValue, itemWeight, dp);
}
I am generating a powerpoint presentation using apache POI - XSLF, on the fly when the user clicks a certain link on my website. I have a few tables with data on my presentation file and also an image (Line chart) generated using jfreechart. When I open the PPTX on my machine it seems to work fine. However when I open the file on another machine that has the powerpoint 2013, I get the following error.
"powerpoint found a problem with content powerpoint can attempt to repair the presentation".
I want to get rid of this error. I read on the internet that the solution is to "UNBLOCK" the powerpoint, which can be done through the properties section of the file. I am wondering if there's something I can do programmatically to suppress this errors for my users. This error message is annoying at the least.
My last thread on this was deleted - https://stackoverflow.com/questions/41163148/how-to-unblock-pptx-using-apache-poi
Hence re-creating this thread here again. A bug is also entered in bugzilla for apache POI. Bug Id - 60633 (https://bz.apache.org/bugzilla/show_bug.cgi?id=60633).
XSLFTableCell cell
XSLFTextParagraph p
XSLFTextRun line
XSLFTable tbl = slide.createTable();
tbl.setAnchor(new Rectangle(X, Y, WIDTH, HEIGHT));
XSLFTableRow headerRow = tbl.addRow();
headerRow.setHeight(45);
//Loop through the data collection and populate rows and columns.
for(int i = 0; i < numberOfCols; i++) {
XSLFTableCell th = headerRow.addCell();
p = th.addNewTextParagraph();
p.setTextAlign(TextAlign.CENTER);
line = p.addNewTextRun();.....}
for (int item=0; item < 8; item++)
{
XSLFTableRow itemRow = tbl.addRow();.....}
//finally write the file
File pptFile = File.createTempFile("fileName", ".ppt")
FileOutputStream out = new FileOutputStream(pptFile)
ppt.write(out)
out.close()
If one provides code along with a bug report, then this code must be complete and verifiable. Your code is not complete and verifiable. And if i do completing it, then it works without problems.
import java.io.FileOutputStream;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
import org.apache.poi.sl.usermodel.TableCell.BorderEdge;
import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
import java.awt.Rectangle;
import java.awt.Point;
import java.awt.Color;
public class CreatePPTX {
public static void main(String[] args) throws Exception {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFTableCell cell;
XSLFTextParagraph p;
XSLFTextRun line;
XSLFTable tbl = slide.createTable();
tbl.setAnchor(new Rectangle(new Point(100, 100)));
XSLFTableRow headerRow = tbl.addRow();
headerRow.setHeight(45);
for(int i = 0; i < 5; i++) {
XSLFTableCell th = headerRow.addCell();
p = th.addNewTextParagraph();
p.setTextAlign(TextAlign.CENTER);
line = p.addNewTextRun();
line.setText("Header " + i);
th.setBorderWidth(BorderEdge.bottom, 2.0);
th.setBorderColor(BorderEdge.bottom, Color.black);
}
for (int item=0; item < 8; item++) {
XSLFTableRow itemRow = tbl.addRow();
for (int i = 0; i < 5; i++) {
XSLFTableCell td = itemRow.addCell();
p = td.addNewTextParagraph();
p.setTextAlign(TextAlign.CENTER);
line = p.addNewTextRun();
line.setText("Cell " + item + ":" +i);
}
}
FileOutputStream out = new FileOutputStream("fileName.pptx");
ppt.write(out);
out.close();
}
}
So your problem is not reproducible using the code you have provided.
But one thing can lead to your issue. If cells shall be empty in the table, then do not create empty runs but let the cell totally empty.
Example with the above code, if cell 1:1 shall be empty, then do not:
...
for (int item=0; item < 8; item++) {
XSLFTableRow itemRow = tbl.addRow();
for (int i = 0; i < 5; i++) {
XSLFTableCell td = itemRow.addCell();
p = td.addNewTextParagraph();
p.setTextAlign(TextAlign.CENTER);
line = p.addNewTextRun();
if (!(item==1 && i==1)) {
line.setText("Cell " + item + ":" +i);
}
}
}
...
This leads to the error.
Instead do:
...
for (int item=0; item < 8; item++) {
XSLFTableRow itemRow = tbl.addRow();
for (int i = 0; i < 5; i++) {
XSLFTableCell td = itemRow.addCell();
p = td.addNewTextParagraph();
p.setTextAlign(TextAlign.CENTER);
if (!(item==1 && i==1)) {
line = p.addNewTextRun();
line.setText("Cell " + item + ":" +i);
}
}
}
...
I need to create board game that can be dynamically change.
Its size can be 5x5, 6x6, 7x7 or 8x8.
I am jusing JavaFX with NetBeans and Scene builder for the GUI.
When the user choose board size greater than 5x5 this is what happens:
This is the template on the scene builder before adding cells dynamically:
To every cell in the GridPane I am adding StackPane + label of the cell number:
#FXML
GridPane boardGame;
public void CreateBoard()
{
int boardSize = m_Engine.GetBoard().GetBoardSize();
int num = boardSize * boardSize;
int maxColumns = m_Engine.GetNumOfCols();
int maxRows = m_Engine.GetNumOfRows();
for(int row = 0; row < maxRows ; row++)
{
for(int col = maxColumns - 1; col >= 0 ; col--)
{
StackPane stackPane = new StackPane();
stackPane.setPrefSize(150.0, 200.0);
stackPane.getChildren().add(new Label(String.valueOf(num)));
boardGame.add(stackPane, col, row);
num--;
}
}
boardGame.setGridLinesVisible(true);
boardGame.autosize();
}
The problem is the stack panes's size on the GridPane are getting smaller.
I tried to set them equal minimum and maximum size but it didn't help they are still getting smaller.
I searched on the web but didn't realy find same problem as mine.
The only similar problem to mine was found here:
Dynamically add elements to a fixed-size GridPane in JavaFX
But his suggestion is to use TilePane and I need to use GridPane because this is a board game and it more easier to use GridPane when I need to do tasks such as getting to cell on row = 1 and column = 2 for example.
EDIT:
I removed the GridPane from the FXML and created it manually on the Controller but now it print a blank board:
#FXML
GridPane boardGame;
public void CreateBoard()
{
int boardSize = m_Engine.GetBoard().GetBoardSize();
int num = boardSize * boardSize;
int maxColumns = m_Engine.GetNumOfCols();
int maxRows = m_Engine.GetNumOfRows();
boardGame = new GridPane();
boardGame.setAlignment(Pos.CENTER);
Collection<StackPane> stackPanes = new ArrayList<StackPane>();
for(int row = 0; row < maxRows ; row++)
{
for(int col = maxColumns - 1; col >= 0 ; col--)
{
StackPane stackPane = new StackPane();
stackPane.setPrefSize(150.0, 200.0);
stackPane.getChildren().add(new Label(String.valueOf(num)));
boardGame.add(stackPane, col, row);
stackPanes.add(stackPane);
num--;
}
}
this.buildGridPane(boardSize);
boardGame.setGridLinesVisible(true);
boardGame.autosize();
boardGamePane.getChildren().addAll(stackPanes);
}
public void buildGridPane(int i_NumOfRowsAndColumns)
{
RowConstraints rowConstraint;
ColumnConstraints columnConstraint;
for(int index = 0 ; index < i_NumOfRowsAndColumns; index++)
{
rowConstraint = new RowConstraints(3, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, VPos.CENTER, true);
boardGame.getRowConstraints().add(rowConstraint);
columnConstraint = new ColumnConstraints(3, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true);
boardGame.getColumnConstraints().add(columnConstraint);
}
}
Changed your code slightly with explanations in comments. HTH.
GridPane boardGame;
public void CreateBoard()
{
int boardSize = m_Engine.GetBoard().GetBoardSize();
int num = boardSize * boardSize;
int maxColumns = m_Engine.GetNumOfCols();
int maxRows = m_Engine.GetNumOfRows();
boardGame = new GridPane();
boardGame.setAlignment(Pos.CENTER);
Collection<StackPane> stackPanes = new ArrayList<StackPane>();
for(int row = 0; row < maxRows ; row++)
{
for(int col = maxColumns - 1; col >= 0 ; col--)
{
StackPane stackPane = new StackPane();
// To occupy fixed space set the max and min size of
// stackpanes.
// stackPane.setPrefSize(150.0, 200.0);
stackPane.setMaxSize(100.0, 100.0);
stackPane.setMinSize(100.0, 100.0);
stackPane.getChildren().add(new Label(String.valueOf(num)));
boardGame.add(stackPane, col, row);
stackPanes.add(stackPane);
num--;
}
}
// No need to add column and row constraints if you want just a uniform
// rigid grid view. So commented the line below.
// this.buildGridPane(boardSize);
boardGame.setGridLinesVisible(true);
boardGame.autosize();
// Here you are adding all stackpanes, those are added to the gridpane 'boardGame'
// before, to the another gridpane with name 'boardGamePane'. So all stackpanes are moved
// to this second gridpane. This is the reason of blank board you are seeing.
// So commenting this out also.
// boardGamePane.getChildren().addAll(stackPanes);
}
I want to make something like Terraria item sidebar thing. (the Left-top rectangles one). And here is my code.
Variables are
public Rectangle InventorySlots;
public Item[] Quickbar = new Item[9];
public Item mouseItem = null;
public Item[] Backpack = new Item[49];
public int selectedBar = 0;
Here is the initialization
inventory[0] = Content.Load<Texture2D>("Contents/Overlays/InventoryBG");
inventory[1] = Content.Load<Texture2D>("Contents/Overlays/InventoryBG2");
update method
int a = viewport.Width / 22;
for (int b = 0; b <= Quickbar.Length; ++b)
{
InventorySlots = new Rectangle(((a/10)*b)+(b),0,a,a);
}
draw method
spriteBatch.Begin();
for (int num = 0; num <= Quickbar.Length; ++num )
spriteBatch.Draw(inventory[0], InventorySlots, Color.White);
spriteBatch.Draw(inventory[1], InventorySlots, Color.White);
spriteBatch.End();
Yes it is not done, but when i try to run it, the texture didn't show up.
I am unable to find out what is wrong in my code.
is it in with SpriteBatch? In the draw method? or In the Update?
Resolved
The problem isnt at the code Itself. the Problem is in this:
int a = viewport.Width / 22;
The thing is, i trought that viewport in here (I've used a Starter Kit) is the Game Window!
You are assigning InventorySlots overwriting its content...
also it seems that you want to draw two sprites... but you are drawing only one inside the loop... and your looping over Quickbar when seems that its not related with your drawing calls.
And it seems that your slot layout calculations have few sense...
You should use an array or a list:
public List<Rectangle> InventorySlots = new List<Rectangle>();
// You put this code in update... but it's not going to change..
// maybe initialize is best suited
// Initialize
int a = viewport.Width / 22;
InventorySlots.Clear();
for (int b = 0; b < Quickbar.Length; ++b)
{ // Generate slots in a line, with a pixel gap among them
InventorySlots.Add( new Rectangle( 1 + (a+2) * b ,0,a,a) );
}
//Draw
spriteBatch.Begin();
for (int num = 0; num < InventorySlots.Count; ++num )
{
spriteBatch.Draw(inventory[0], InventorySlots[num], Color.White);
spriteBatch.Draw(inventory[1], InventorySlots[num], Color.White);
}
spriteBatch.End();
I want to create Dynamic grid of edittext but it gives me an IllegalStateException:
The specific child already has a prent, You must call removeView()
on child parent first.
Here is my code:
for (int x = 0; x < no_of_rows; x++)
{
ll = new LinearLayout(this.getApplicationContext());
ll.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
ll.setOrientation(0);
for(int y = 0; y < no_of_columns; y++){
LinearLayout(this.getApplicationContext());
this.edittext2.add(new EditText(this.getApplicationContext()));
this.edittext2.get(y).setId(y);
this.edittext2.get(y).setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT
,LinearLayout.LayoutParams.WRAP_CONTENT));
this.edittext2.get(y).setMaxLines(1);
this.edittext2.get(y).setText("Something");
ll.addView(this.edittext2.get(y));
}
tlayout.addView(ll);
}