I have a table in libgdx which is supposed to have 6 buttons, organised into 2 rows. Here's my code:
elementsTable.clear();
String[] colors = new String [6];
colors[0] = "red";
colors[1] = "orange";
colors[2] = "yellow";
colors[3] = "purple";
colors[4] = "blue";
colors[5] = "green";
String category = currentCategory.name();
category = category.substring(0, category.length()-1).toLowerCase(); //removes the last character because otherwise there would be an extra 's'
int count = 0;
for(String color : colors) {
ButtonStyle buttonStyle = new ButtonStyle();
buttonStyle.up = cellsSkin.getDrawable(category + "_" + color);
Button button = new Button(buttonStyle);
button.addListener(toolBoxListener);
button.setName(category + "_" + color);
button.setSize(toolBoxButtonSize, toolBoxButtonSize);
elementsTable.add(button).pad(toolBoxButtonPadding);
count ++;
if (count == Math.ceil((colors.length/2d))) elementsTable.row();
}
That's the filling. My buttons are of 6 different colors so I loop over the array with the color names to access the skin to get the drawable for the buttons. This part seems to work because when I debug and look at the table, it has the 6 buttons in there with the right size.
Then I set the position and size of my table and add it to the stage.:
elementsTable.setSize(toolBoxWidth, 500/(float)(3*toolBoxButtonSize+6*toolBoxButtonPadding)*elementsTable.getHeight());
elementsTable.setPosition(195, 30);
stage.addActor(elementsTable);
The stage has an orthographic camera set that should scale things down just fine.
However, what I get is this:
Another strange thin is that when I look at the table's height and width in debug mode, it says 0 even thoug there are elements in it that all have the corrct size.
Can anyone help me?
If you have any further questions on the problem, please ask! I tried to give a detailed description, but I am happy to answer your questions.
It seemsits just the position. 30 is too low for the Y:
elementsTable.setPosition(195, 30);
Try 150 for example:
elementsTable.setPosition(195, 150);
Remember the coordinate system that Libgdx (and OpenGL) use is with Y going up.
Related
I am trying to implement grouped MPAndroid Bar chart. I have a group of 2 datasets that i want to display. The problem is that the xaxis values are not center aligned with the bar chart (as per the screenshot). I checked other questions as well and implemented the following answers provided.
I want to make the labels center aligned with the grouped bars.
float barSpace = 0.02f;
float groupSpace = 0.3f;
int groupCount = 2;
data.setBarWidth(0.155f);
pvaAmount_chart.getXAxis().setAxisMinimum(0);
pvaAmount_chart.getXAxis().setAxisMaximum(0 + pvaAmount_chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
pvaAmount_chart.groupBars(0, groupSpace, barSpace);
pvaAmount_chart.getXAxis().setCenterAxisLabels(true);
pvaAmount_chart.notifyDataSetChanged();
When entering groupcount=2 as 2 types of bars:
When entering groupcount=4 number of grouped charts:
i'm doing it like this :
String [] values = new String[insert the lenght of your array]
for (int blabla=0;i<values[lenght];blabla++){
String dayOfTheWeek = dateFormat.format(mydate);
vales[blabla] = dayOfTheWeek //in my case
}
xAxis.setGranularity(1.0f); //
xAxis.setCenterAxisLabels(false);
xAxis.setGranularityEnabled(true);
xAxis.setLabelCount(values.length,false); //
List<String> stringList = new ArrayList<String>(Arrays.asList(values));
xAxis.setGranularityEnabled(true);
xAxis.setLabelCount(values.length,false); //
xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues((ArrayList<String>) stringList)));
i'm using : 'com.github.PhilJay:MPAndroidChart:v3.1.0'
I'm practice developing an app but I met a problem. I want remove two elements from an array but android studio only can remove one and if I adding one more the app will crash, below is my code and now I only put one code to remove the first element and i need to remove last element otherwise the choice for answer will mix the array.
ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
String quizData[][] = {
{"quadrilateral","quadrilateral","triangle","heptagon","pentagon","hexagon","decagon","Which one is the green polygon just now ?"},
{"triangle","triangle","heptagon","quadrilateral","pentagon","octagon","decagon","Which one is the red polygon just now ?"},
{"circle","circle","triangle","quadrilateral","pentagon","nonagon","decagon","Which one is the black polygon just now ?"},
{"star","star","circle","octagon","pentagon","hexagon","decagon","Which one is the brown polygon just now ?"},
{"decagon","decagon","triangle","circle","pentagon","hexagon","star","Which one is the pink polygon just now ?"},
{"heptagon","heptagon","decagon","quadrilateral","pentagon","hexagon","star","Which one is the yellow polygon just now ?"},
{"hexagon","hexagon","decagon","heptagon","pentagon","octagon","star","Which one is the sky blue polygon just now ?"},
{"nonagon","nonagon","circle","quadrilateral","triangle","hexagon","star","Which one is the orange polygon just now ?"},
{"pentagon","pentagon","nonagon","octagon","heptagon","hexagon","circle","Which one is the purple polygon just now ?"},
{"octagon","octagon","triangle","quadrilateral","pentagon","hexagon","star","Which one is the dark blue polygon just now ?"}
};
public void Show_Next_Quiz(){
Random random = new Random();
int Random_Num = random.nextInt(quizArray.size());
ArrayList<String> quiz = quizArray.get(Random_Num);
Question1.setText(quiz.get(7));
iV1.setImageResource(
getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
);
iV2.setImageResource(
getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
);
iV3.setImageResource(
getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
);
iV4.setImageResource(
getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
);
iV5.setImageResource(
getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
);
iV6.setImageResource(
getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
);
Right_Answer = quiz.get(1);
quiz.remove(0);
//quiz.remove(7);
Collections.shuffle(quiz);
ans1.setText(quiz.get(1));
ans2.setText(quiz.get(2));
ans3.setText(quiz.get(3));
ans4.setText(quiz.get(4));
ans5.setText(quiz.get(5));
ans6.setText(quiz.get(6));
quizArray.remove(Random_Num);
}
Your program is crash because you're trying to remove an item from the List by using an incorrect item position.
For example, assume that you have an ArrayList with size of 7:
ArrayList<String> quizzes = new ArrayList<>(7);
the quiz items from quizzes will be in 0 - 6 range of index position. When you're removing the first item with:
quizzes.remove(0);
the quizzes size is now 6. Now you can only access the items from 0 to 5 index position. It will gives error when you're trying to remove an item with item position beyond that. So, the following code won't work and gives you a crash:
quizzes.remove(7);
because you're trying to access a non-exist item position.
So, you can't depends on the item position to remove the item in your case. You can use public boolean remove(Object o) instead.
quizzes.remove("triangle");
Probably what you need is an another ArrayList for ansX views to match the size of the questions. Something like this:
int sizeOfTheQuiz = 6;
ArrayList<String> quizzes = new ArrayList<>(sizeOfTheQuiz);
ArrayList<TextView> tvAnswers = new ArrayList<>();
tvAnswers.add(ans1);
tvAnswers.add(ans2);
tvAnswers.add(ans3);
tvAnswers.add(ans4);
tvAnswers.add(ans5);
tvAnswers.add(ans6);
// size of the quizzes must not larger than tvAnswers
for(int i = 0; i < quizzes.size(); i++) {
tvAnswers.get(i).setText(quizzes.get(i));
}
I am currently making a program for school where I have to lock the ratio while adjusting JSliders.
I could not figure out how to make one slider change with the same value while changing the first slider.
I want to change the sliders at a 1:1 ratio, so if I slide width up 5, the length will also go up 5, but I couldn't figure out how to find a constant difference to calculate when I change the value.
In your code you are setting length value based on length.getValue, but you want length to be set as width changes and vise versa. So I suggest that you set length like length.setValue(width.getValue());
if(lkRatio.isSelected() !=true){
tempw = width.getValue();
templ = length.getValue();
diff = width.getValue() - length.getValue();
}
if(lkRatio.isSelected()){
if(source == width){
length.setValue(width.getValue() - diff);
}
if(source == length){
width.setValue(length.getValue() + diff);
}
}
To answer "how can you create 2 sliders with same model" :
DefaultBoundedRangeModel brm = new DefaultBoundedRangeModel();
brm.setMaximum(100);
width = new JSlider(brm);
length = new JSlider(brm);
I would like to create a table that has a list of dots. I don't know ahead of time how many dots I have, but if they overflow the cell, I want them to wrap, just like text would. My code is something like this:
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(new float[]{80});
table.setLockedWidth(true);
Phrase listOfDots = new Phrase();
for (int i = 0; i < 40; i++) {
listOfDots.add(new Chunk(pdf.correct, 0, 0));
listOfDots.add(new Chunk(" "));
}
table.addCell(listOfDots);
outerCell.addElement(table);
The dots wrap, like I expect, but they don't all have the same size. There are 7 rows of 5 dots each, and all 35 dots have the same size. The last row of 5 dots are roughly half the size of the others.
(I tried to post an image, but I'm not veteran enough on this site.)
Is there a way to make all the images the same size?
Please take a look at the ImagesInChunkInCell example. Instead of a bullet, I took the image of a light bulb. I was able to reproduce the problem you described, but as you can see in list_with_images.pdf, I was able to add one extra line:
Image image = Image.getInstance(IMG);
image.setScaleToFitHeight(false);
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(new float[]{120});
table.setLockedWidth(true);
Phrase listOfDots = new Phrase();
for (int i = 0; i < 40; i++) {
listOfDots.add(new Chunk(image, 0, 0));
listOfDots.add(new Chunk(" "));
}
table.addCell(listOfDots);
The extra line is:
image.setScaleToFitHeight(false);
This prevents that the image is scaled.
I'm making a simple currency converter GUI (nothing fancy) as I am going to try and incorporate live exchange rate updates for each time the user opens the application. When creating the layout, I have decided to simple convert 3 currencies (GBP, USD and EUR). I have the respective flags in 2 columns, each column has one of the 3 flags. One column is for the user to select the initial currency and the other is the desired currency to exchange to; as seen below
I have created a String array which contains the words "Pounds", "Dollars" and "Euros" and I am wanting to put these labels to the left of the flags (for clarity of the application for the user as not every user may know which currency belongs to which country.
I created a loop which would create a label and assign it to the left of the flags, it is supposed to make a "pound" label, then a "Dollar" then a "euro" each time traversing the Y axis south so that they aligns with the flags and it will then reset the array count to go back to the correct string, move along the x-axis and repeat it again. It is however not doing this at all, the only result I get is the text "Pounds " to the left of the first United Kingdom flag; as seen below:
below is my code if anyone can see as to why this is happening.
This is the code which adds the flags to the panel
addToMain(GBP1, mainPage, 100,100,100,100); //alligns a United Kingdom Flag to left Column
addToMain(GBP2, mainPage, 375,100,100,100); //alligns a United Kingdom Flag to right Column
addToMain(USD1, mainPage, 100,200,100,100); //alligns a United States Flag to left Column
addToMain(USD2, mainPage, 375,200,100,100); //alligns a United States Flag to right Column
addToMain(EUR1, mainPage, 100,300,100,100); //alligns a European Union Flag to left Column
addToMain(EUR2, mainPage, 375,300,100,100); //alligns a European Union Flag to right Column
This is the loop which should add the text labels to the left of the flags
currencyName = new String [] {"Pounds", "Dollars", "Euros"};
for(int i = 0; i <= 7; i++)
{
int count = 0; //declares a counter for the position in the currencyName array to grab the correct text for label
xLabelAlign = 50;
yLabelAlign = 100;
if(count == 3)
{
count = 0; //resets to create both columns of labels in the application moves to the next column.
xLabelAlign = 325;
yLabelAlign = 100;
}
JLabel temp = new JLabel(currencyName[count]); //creates a new label and names it the string at position count
temp.setFont(new Font("SERIF", Font.BOLD, 20));
temp.setForeground(Color.WHITE);
addToMain(temp, mainPage, xLabelAlign, yLabelAlign ,100,100); //adds it to the panel
yLabelAlign +=100; //moves position ready for the next text label.
count++; //grabs the next label in the currencyName string array.
}
This is the method which adds things to the panel. I have used the set bounds methods to add things to the panel so i can position them where i want easily
private void addToMain(JComponent c, JPanel p, int x, int y, int w, int h)
{
c.setBounds(x,y,w,h);
p.add(c);
}
Any help would be greatly appreciated.
Fast solution: Move your int count = 0; xLabelAlign = 50; yLabelAlign = 100; out of the for loop. Loop in the range of [0,5].
Good solution: Java layouts tutorial