How to Name Painted Graphics? [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
So I was wondering if you could give a name to something that was painted.
Example:
`public void paint(Graphics g) {
g.drawRect(int,int,int,int);
}`
Is it possible to give that rectangle a name so I can reference it in later code.

I am not sure for what exactly your are asking, but here's how you can save a 2 dimensional grid of variables:
String[][] rectangles = new String[100][100];
This above creates a field of Strings, 100*100 Strings big.
If you want to set the value for the field at x and y you do:
String[x][y] = "Hello World";
I did this with a String field, but you can use your own classes or something like int[][] as well. Depending on if you choose String[][], int[][] or yourClassName[][] you can store Strings, ints or yourClassNames in the field.

Related

Converting between a specific String and Color [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If given a String specifically in the format "Color.RED", "Color.BLUE", "Color.GREEN", etc. How can I convert these into the Color Object equivalents, and from Color Objects to Strings of the same format? I have seen how to translate to and from Integers and rgb code, but not this format.
You might need to use reflection on this one. Try the following:
String value = "Color.RED";
String colorId = value.replace("Color.", "");
Field field = Color.class.getField(colorId);
Color color = (Color) field.get(null);

How to generate a random font in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm completely new to java and coding in general and I need to make a program display certain text using different fonts each time randomly.
This question should be split into two parts. How can I list all available font family names in Java? and How to pick a random index of an array?. You should always try to split your problems into separate smaller sub-problems.
Here are the answers to both problems: A and B.
I made a neat function out of it which picks you the name of a random font family. If the language of the text that should be rendered is not English, replace Locale.ENGLISH with your language.
private static final Random RANDOM = new Random();
private static String pickRandomFontFamily() {
String[] availableFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(Locale.ENGLISH);
return availableFonts[RANDOM.nextInt(availableFonts.length)];
}

If A is an array of type int, what command would you use to put the value 50 in the first position of the array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
If A is an array of type int, what command would you use to put the value 50 in the first position of the array
A[0] = 50;
This will store 50 in the first element of A.
A[0] = 50; This assign first element to 50.
There is a lot of resource about java array online, i think all the basic tutorial would cover about this.

GetType does not accept itemStack [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm writing a crafting plugin and I need to check if getResult().GetType() is equal to ItemStack, but getType() probably does not accept the ItemStack.
ItemStack its = new ItemStack(Material.getMaterial(s.getInt("item")),
1, (byte) s.getInt("data"));
if(e.getInventory().getResult().getType().equals(its)) { //this don't work
It won't work because e.getInventory().getResult().getType() returns Material object and its in your case is ItemStack object. So if you want to check if just the items' types are equal, you have to write:
if(e.getInventory().getResult().getType().equals(its.getType())) {
//code here
}
or if you want to do such a comparison like you presented in your post you have to compare ItemStack objects as you cannot compare ItemStack object to Material one. Then you have to write something like this:
if(e.getInventory().gtResult().equals(its)) {
//code here
}

how to get values from a Text view android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to store values from text view for example i am performing the following plus function like 5+6.Now i want to store 5 as value-1 and 6 as value-2.I only have one text view where i am displying like 5+6 in my project.I search a lot about my issue but could not found any proper solution. thanks
try this:
String string =text.getText().toString();
String[] separated = string.split("+");
String firstValue=separated[0];
String secondValue=separated[1];
You can try this and you will able to get the addition of two numbers at once. .
String[] getText = textview.getText().split("\\+");
int total=0;
for(String s:getText){
total+=Integer.parseInt(s);
}
in the end of iteration total will give the final result.From this method it gives the total of any numbers of numbers.

Categories