Can someone please help me with the code I am writing? (Java) - java

I am writing a small program.
I am a beginner at programming java.
Here is the code:
package dnd;
public class Monster {
static String name;
static int level;
static String rang;
static String Class;
static double[] Strength = {1, 1, 0};
static double[] Endurance = {1, 1, 0};
static double[] Agility = {1, 1, 0};
static double[] Precision = {1, 1, 0};
static double[] Wisdom = {0, 1, 1};
static double[] Intelegence = {0, 1, 1};
public static void Monster(String nameC, int levelC, String classesC, int rangC){
name = nameC;
level = levelC;
switch(rangC){
case 1:
rang = "Civillian";
case 2:
rang = "Soldier";
case 3:
rang = "Veteran";
case 4:
rang = "Commander";
case 5:
rang = "Boss";
case 6:
rang = "Elite";
default:
rang = "Civillian";
}
Class = classesC;
switch(Class){
case "Warrior":
Strength[1] = 2;
Endurance[1] = 2;
Intelegence[1] = 0.5;
case "Archer":
Agility[1] = 2;
Precision[1] = 2;
Endurance[1] = 0.5;
case "Rouge":
Agility[1] = 2;
Intelegence[1] = 2;
Endurance[1] = 0.5;
case "Mage":
Wisdom[1] = 2;
Intelegence[1] = 2;
Strength[1] = 0.5;
case "Priest":
Wisdom[1] = 2;
Strength[1] = 0.5;
case "Druid":
Intelegence[1] = 2;
Agility[1] = 0.5;
}
}
public static void defaultStaringPoints(){
int StP;
int EnP;
int InP;
int AgP;
int PrP;
int WiP;
switch(Class){
case "Warrior":
//Strength
//Endurance
//-Intelegence
StP = (int) (Math.random() * 2);
EnP = (int) (Math.random() * (3-StP));
InP = (int) (Math.random() * (3-(StP+EnP)));
Strength[0] = Strength[0] + StP;
Endurance[0] = Endurance[0] + EnP;
Intelegence[0] = Intelegence[0] + InP;
case "Archer":
//Agility
//Precision
//-Endurance
AgP = (int) (Math.random() * 2);
PrP = (int) (Math.random() * (3-AgP));
EnP = (int) (Math.random() * (3-(AgP+PrP)));
Agility[0] = Agility[0] + AgP;
Precision[0] = Precision[0] + PrP;
Endurance[0] = Endurance[0] + EnP;
case "Rouge":
//Agility
//Intelegence
//-Endurance
AgP = (int) (Math.random() * 2);
InP = (int) (Math.random() * (3-AgP));
EnP = (int) (Math.random() * (3-(AgP+InP)));
Agility[0] = Agility[0] + AgP;
Intelegence[0] = Intelegence[0] + InP;
Endurance[0] = Endurance[0] + EnP;
case "Mage":
//Wisdom
//Intelegence
//-Strength
WiP = (int) (Math.random() * 2);
InP = (int) (Math.random() * (3-WiP));
StP = (int) (Math.random() * (3-(WiP+InP)));
Wisdom[0] = Wisdom[0] + WiP;
Intelegence[0] = Intelegence[0] + InP;
Strength[0] = Strength[0] + StP;
case "Priest":
//Wisdom
//-Strength
WiP = (int) (Math.random() * 3);
StP = (int) (Math.random() * (3-WiP));
Wisdom[0] = Wisdom[0] + WiP;
Strength[0] = Strength[0] + StP;
case "Druid":
//Intelegence
//-Agility
InP = (int) (Math.random() * 3);
AgP = (int) (Math.random() * (3-InP));
Intelegence[0] = Intelegence[0] + InP;
Agility[0] = Agility[0] + AgP;
}
}
}
Then I create a new Monster,
run Goblin.Monster("Goblin", 1, "Rouge", 2)
and Goblin.defaultStaringPoints()
the output for Arrays.toString(Goblin.Wisdom) should be [0, 1, 1],
but instead it is [1, 2, 1], [2, 2, 1] or even [3, 2, 1].
I feel, that I am just overseeing something,
but I checked over 10 times.
Thank You in advance!

In each of your switch statements, you need a break; statement at the end of each case. For example:
switch(rangC){
case 1:
rang = "Civillian";
break;
case 2:
rang = "Soldier";
break;
...
}
Without the break;, execution simply falls through to the next case.
Also, you don't have a case for "Goblin" in any of your switch statements.

When you call defaultStartingPoints(), it randomizes the stats so that the Wisdom stat is no longer [0, 1, 1].

Related

How to draw the middle half of a sphere (in code)

I'm trying to create the middle half of a sphere. Basically to create a sphere, stack numbers and slice numbers are given, and there are two variables phi (for slices) and theta (for stacks) responsible for how much to progress. And the process is divided into creating bottom cap, body, and top cap (as seen below). To achieve middle half (theta of middle 50% as below), we need to omit the caps, and somehow modify the body. I was playing around with stack numbers (1/4*stackNumbers to 3/4*stackNumbers) but didn't give the result I wanted.
How should I modify the sphere generation to achieve the middle half (pi/4 <theta <pi*3/4)? My overall problem is how can I split the sphere into 3 different parts upper 25%, middle 50%, and bottom 25%? (25% in terms of angle, i.e. theta)
Here is the popular code for generating a sphere programmatically:
private void generateSphere(int stackNumber, int sliceNumber, boolean facingOut) {
int capVertexNumber = 3 * sliceNumber;
int bodyVertexNumber = 4 * sliceNumber * (stackNumber - 2);
int vertexNumber = (2 * capVertexNumber) + bodyVertexNumber;
int triangleNumber = (2 * capVertexNumber) + (6 * sliceNumber * (stackNumber - 2));
vertices = new float[3 * vertexNumber];
normals = new float[3 * vertexNumber];
texCoords = new float[2 * vertexNumber];
indices = new char[triangleNumber];
// bottom cap
// createCap(stackNumber, sliceNumber, false, facingOut);
// body
createBody(stackNumber, sliceNumber, facingOut);
// top cap
createCap(stackNumber, sliceNumber, true, facingOut);
}
private void createCap(int stackNumber, int sliceNumber, boolean top, boolean facingOut) {
float stackPercentage0;
float stackPercentage1;
if (!top) {
stackPercentage0 = ((float) (stackNumber - 1) / stackNumber);
stackPercentage1 = 1.0f;
} else {
stackPercentage0 = (1.0f / stackNumber);
stackPercentage1 = 0.0f;
}
float t0 = stackPercentage0;
float t1 = stackPercentage1;
double theta0 = stackPercentage0 * Math.PI;
double theta1 = stackPercentage1 * Math.PI;
double cosTheta0 = Math.cos(theta0);
double sinTheta0 = Math.sin(theta0);
double cosTheta1 = Math.cos(theta1);
double sinTheta1 = Math.sin(theta1);
for (int slice = 0; slice < sliceNumber; slice++) {
float slicePercentage0 = ((float) (slice) / sliceNumber);
float slicePercentage1 = ((float) (slice + 1) / sliceNumber);
double phi0 = slicePercentage0 * 2.0 * Math.PI;
double phi1 = slicePercentage1 * 2.0 * Math.PI;
float s0, s1;
if (facingOut) {
s0 = 1 - slicePercentage0;
s1 = 1 - slicePercentage1;
} else {
s0 = slicePercentage0;
s1 = slicePercentage1;
}
float s2 = (s0 + s1) / 2.0f;
double cosPhi0 = Math.cos(phi0);
double sinPhi0 = Math.sin(phi0);
double cosPhi1 = Math.cos(phi1);
double sinPhi1 = Math.sin(phi1);
float x0 = (float) (sinTheta0 * cosPhi0);
float y0 = (float) cosTheta0;
float z0 = (float) (sinTheta0 * sinPhi0);
float x1 = (float) (sinTheta0 * cosPhi1);
float y1 = (float) cosTheta0;
float z1 = (float) (sinTheta0 * sinPhi1);
float x2 = (float) (sinTheta1 * cosPhi0);
float y2 = (float) cosTheta1;
float z2 = (float) (sinTheta1 * sinPhi0);
vertices[vertexCount + 0] = x0;
vertices[vertexCount + 1] = y0;
vertices[vertexCount + 2] = z0;
vertices[vertexCount + 3] = x1;
vertices[vertexCount + 4] = y1;
vertices[vertexCount + 5] = z1;
vertices[vertexCount + 6] = x2;
vertices[vertexCount + 7] = y2;
vertices[vertexCount + 8] = z2;
if (facingOut) {
normals[vertexCount + 0] = x0;
normals[vertexCount + 1] = y0;
normals[vertexCount + 2] = z0;
normals[vertexCount + 3] = x1;
normals[vertexCount + 4] = y1;
normals[vertexCount + 5] = z1;
normals[vertexCount + 6] = x2;
normals[vertexCount + 7] = y2;
normals[vertexCount + 8] = z2;
} else {
normals[vertexCount + 0] = -x0;
normals[vertexCount + 1] = -y0;
normals[vertexCount + 2] = -z0;
normals[vertexCount + 3] = -x1;
normals[vertexCount + 4] = -y1;
normals[vertexCount + 5] = -z1;
normals[vertexCount + 6] = -x2;
normals[vertexCount + 7] = -y2;
normals[vertexCount + 8] = -z2;
}
texCoords[texCoordCount + 0] = s0;
texCoords[texCoordCount + 1] = t0;
texCoords[texCoordCount + 2] = s1;
texCoords[texCoordCount + 3] = t0;
texCoords[texCoordCount + 4] = s2;
texCoords[texCoordCount + 5] = t1;
if ((facingOut && top) || (!facingOut && !top)) {
indices[indexCount + 0] = (char) (triangleCount + 1);
indices[indexCount + 1] = (char) (triangleCount + 0);
indices[indexCount + 2] = (char) (triangleCount + 2);
} else {
indices[indexCount + 0] = (char) (triangleCount + 0);
indices[indexCount + 1] = (char) (triangleCount + 1);
indices[indexCount + 2] = (char) (triangleCount + 2);
}
vertexCount += 9;
texCoordCount += 6;
indexCount += 3;
triangleCount += 3;
}
}
private void createBody(int stackNumber, int sliceNumber, boolean facingOut) {
for (int stack = 1; stack < stackNumber - 1; stack++) {
float stackPercentage0 = ((float) (stack) / stackNumber);
float stackPercentage1 = ((float) (stack + 1) / stackNumber);
float t0 = stackPercentage0;
float t1 = stackPercentage1;
double theta0 = stackPercentage0 * Math.PI;
double theta1 = stackPercentage1 * Math.PI;
double cosTheta0 = Math.cos(theta0);
double sinTheta0 = Math.sin(theta0);
double cosTheta1 = Math.cos(theta1);
double sinTheta1 = Math.sin(theta1);
for (int slice = 0; slice < sliceNumber; slice++) {
float slicePercentage0 = ((float) (slice) / sliceNumber);
float slicePercentage1 = ((float) (slice + 1) / sliceNumber);
double phi0 = slicePercentage0 * 2.0 * Math.PI;
double phi1 = slicePercentage1 * 2.0 * Math.PI;
float s0, s1;
if (facingOut) {
s0 = 1.0f - slicePercentage0;
s1 = 1.0f - slicePercentage1;
} else {
s0 = slicePercentage0;
s1 = slicePercentage1;
}
double cosPhi0 = Math.cos(phi0);
double sinPhi0 = Math.sin(phi0);
double cosPhi1 = Math.cos(phi1);
double sinPhi1 = Math.sin(phi1);
float x0 = (float) (sinTheta0 * cosPhi0);
float y0 = (float) cosTheta0;
float z0 = (float) (sinTheta0 * sinPhi0);
float x1 = (float) (sinTheta0 * cosPhi1);
float y1 = (float) cosTheta0;
float z1 = (float) (sinTheta0 * sinPhi1);
float x2 = (float) (sinTheta1 * cosPhi0);
float y2 = (float) cosTheta1;
float z2 = (float) (sinTheta1 * sinPhi0);
float x3 = (float) (sinTheta1 * cosPhi1);
float y3 = (float) cosTheta1;
float z3 = (float) (sinTheta1 * sinPhi1);
vertices[vertexCount + 0] = x0;
vertices[vertexCount + 1] = y0;
vertices[vertexCount + 2] = z0;
vertices[vertexCount + 3] = x1;
vertices[vertexCount + 4] = y1;
vertices[vertexCount + 5] = z1;
vertices[vertexCount + 6] = x2;
vertices[vertexCount + 7] = y2;
vertices[vertexCount + 8] = z2;
vertices[vertexCount + 9] = x3;
vertices[vertexCount + 10] = y3;
vertices[vertexCount + 11] = z3;
if (facingOut) {
normals[vertexCount + 0] = x0;
normals[vertexCount + 1] = y0;
normals[vertexCount + 2] = z0;
normals[vertexCount + 3] = x1;
normals[vertexCount + 4] = y1;
normals[vertexCount + 5] = z1;
normals[vertexCount + 6] = x2;
normals[vertexCount + 7] = y2;
normals[vertexCount + 8] = z2;
normals[vertexCount + 9] = x3;
normals[vertexCount + 10] = y3;
normals[vertexCount + 11] = z3;
} else {
normals[vertexCount + 0] = -x0;
normals[vertexCount + 1] = -y0;
normals[vertexCount + 2] = -z0;
normals[vertexCount + 3] = -x1;
normals[vertexCount + 4] = -y1;
normals[vertexCount + 5] = -z1;
normals[vertexCount + 6] = -x2;
normals[vertexCount + 7] = -y2;
normals[vertexCount + 8] = -z2;
normals[vertexCount + 9] = -x3;
normals[vertexCount + 10] = -y3;
normals[vertexCount + 11] = -z3;
}
texCoords[texCoordCount + 0] = s0;
texCoords[texCoordCount + 1] = t0;
texCoords[texCoordCount + 2] = s1;
texCoords[texCoordCount + 3] = t0;
texCoords[texCoordCount + 4] = s0;
texCoords[texCoordCount + 5] = t1;
texCoords[texCoordCount + 6] = s1;
texCoords[texCoordCount + 7] = t1;
// one quad looking from outside toward center
//
// #formatter:off
//
// s1 --> s0
//
// t0 1-----0
// | | |
// v | |
// t1 3-----2
//
// #formatter:on
//
// Note that tex_coord t increase from top to bottom because the
// texture image is loaded upside down.
if (facingOut) {
indices[indexCount + 0] = (char) (triangleCount + 0);
indices[indexCount + 1] = (char) (triangleCount + 1);
indices[indexCount + 2] = (char) (triangleCount + 2);
indices[indexCount + 3] = (char) (triangleCount + 2);
indices[indexCount + 4] = (char) (triangleCount + 1);
indices[indexCount + 5] = (char) (triangleCount + 3);
} else {
indices[indexCount + 0] = (char) (triangleCount + 0);
indices[indexCount + 1] = (char) (triangleCount + 2);
indices[indexCount + 2] = (char) (triangleCount + 1);
indices[indexCount + 3] = (char) (triangleCount + 2);
indices[indexCount + 4] = (char) (triangleCount + 3);
indices[indexCount + 5] = (char) (triangleCount + 1);
}
vertexCount += 12;
texCoordCount += 8;
indexCount += 6;
triangleCount += 4;
}
}
}
The code here is using spherical coordinates to calculate the sphere. theta is the variable that represents the up/down coordinate that you're interested in, and theta goes from 0 to PI. You want to go from PI/4 to 3PI/4. stackNumbers simply represent the number of divisions in the sphere, since you can see that it is used as a denominator for stack, which is the wrong variable to change. So you can make the following changes to the code instead. From:
double theta0 = stackPercentage0 * Math.PI;
double theta1 = stackPercentage1 * Math.PI;
to:
double startTheta = Math.PI / 4;
double endTheta = 3 * Math.PI / 4;
double theta0 = stackPercentage0 * (endTheta - startTheta) + startTheta;
double theta1 = stackPercentage1 * (endTheta - startTheta) + startTheta;
And since you aren't using the caps you need to change the start and end stack numbers to reflect that:
for (int stack = 1; stack < stackNumber - 1; stack++) { // old
for (int stack = 0; stack < stackNumber; stack++) { // new
Also, you because you have more body faces now, you need to update the appropriate container for them. Replace (stackNumber - 2) with (stackNumber - 1).

Breaking an array into quadrants multiple times

My goal is that I have an image which spits out a bitmap. Now I want display the average color of the image as one giant pixel. This is a fairly simple task, just use bufferImage and get the bitmap which I take each red, green, and blue value, add it all up and then divide by the picture's resolution.
The thing is after doing this, I want to break the image into four quadrants and take the average color for each quadrant and display it. And again break each of the four quadrants and do the same. The issue I am facing is that I am using a recursive statement that does the following:
private static void getBlockAverage(int startHeight, int endHeight, int startWidth, int endWidth, BufferedImage img, BufferedImage blockImg, Color oldAvg) {
if(endHeight <= startHeight || endWidth <= startWidth) {
counter++;
return;
}
// get quadrant pixel average and display, I deleted this portion of the code just to keep things compact
getBlockAverage(startHeight, (startHeight + endHeight)/2, startWidth, (startWidth + endWidth)/2, img, blockImg, color);
getBlockAverage((startHeight + endHeight)/2, endHeight, startWidth, (startWidth + endWidth)/2, img, blockImg, color);
getBlockAverage(startHeight, (startHeight + endHeight)/2, (startWidth+endWidth)/2, endWidth, img, blockImg, color);
getBlockAverage((startHeight+endHeight)/2, endHeight, (startWidth+endWidth)/2, endWidth, img, blockImg, color);
}
It is quite easy to see that this is not what I want as the recursive statement will keep executing getBlockAverage(startHeight, (startHeight + endHeight)/2, startWidth, (startWidth + endWidth)/2, img, blockImg, color); first until it is done and then move onto the next one. This is not what I want. I want the image to be broken down into 4 quadrants and then each of those quadrants gets broken down until all quadrants are broken down and continue.
So for example:
Starting off with 1 quadrants, break into 4. Now for quadrant 1, break that into 4, now quadrant 2, break that into 4, now quadrant 3, break that into 4, now quadrant 4, break that into 4.
Now that I am thinking about it, I feel like I should use some sort of for loop with a cap on the number of iterations but I am not sure how to do that.
I tend to agree with you. I think I would place this method into loop as well but also make the method return the average color for each quadrant into an single dimensional Array with the thought of each Array index is a quadrant number and and the actual element for that index contains the color for that particular quadrant. This way you can work with all the pertinent information that is acquired later on. I would at least get it going and then optimize it once it's working the way I want. Well, that's how I do it anyways :P
Of course I'm assuming throughout all this that the Quadrant dissection flow is something similar to what I show in the image below:
Here is what I would do:
Change the getBlockAverage() method so that it returns a Color...
private static Color getBlockAverage(int startHeight, int endHeight, int startWidth,
int endWidth, BufferedImage img, BufferedImage blockImg, Color oldAvg) {
// get quadrant pixel average color and return it
// with whatever code you've been using....
return theQuadrantAverageColor;
}
then I would create another method which contains our loop, image quadrants dissectional dimensions, and calls to the getBlockAverage() method while the loop is well...looping and for every loop cycle place the returned color from the getBlockAverage() method into a per-established Color Array:
private static void getQuadrantsColorAverages(Color[] quadrantColors, BufferedImage img) {
// Decalre and Initialize required variables.
BufferedImage wrkImg = img;
BufferedImage blockImg = null; //?????
int imgWidth = wrkImg.getWidth();
int imgHeight = wrkImg.getHeight();
int startHeight = 0;
int endHeight = 0;
int startWidth = 0;
int endWidth = 0;
Color oldAvg = null;
int quadCount = 1;
// Start our loop and and continue it until our counter
// variable named quadCount goes over 20....
while (quadCount <= 20) {
// Handle dissectional dimensions (in pixels)
// for quadrants 1 to 20 as layed out within
// the supplied image to this forum post.
switch (quadCount) {
// Quadrant 1
case 1:
startHeight = 0; endHeight = (imgHeight / 2);
startWidth = 0; endWidth = (imgWidth / 2);
// Quadrant 2
case 2:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 3
case 3:
startHeight = (endHeight + 1); endHeight = imgHeight;
startWidth = 0; endWidth = (imgWidth / 2);
break;
// Quadrant 4
case 4:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 5
case 5:
startHeight = 0; endHeight = (imgHeight / 4);
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 6
case 6:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 7
case 7:
startHeight = (endHeight + 1); endHeight = (imgHeight / 2);
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 8
case 8:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 9
case 9:
startHeight = 0; endHeight = (imgHeight / 4);
startWidth = (endWidth + 1); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 10
case 10:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 11
case 11:
startHeight = (imgHeight / 4); endHeight = (imgHeight / 2);
startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 12
case 12:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 13
case 13:
startHeight = (imgHeight / 2); endHeight = ((imgHeight / 4) * 3);
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 14
case 14:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 15
case 15:
startHeight = (endHeight + 1); endHeight = imgHeight;
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 16
case 16:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 17
case 17:
startHeight = (imgHeight / 2); endHeight = ((imgHeight / 4) * 3);
startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 18
case 18:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 19
case 19:
startHeight = (endHeight + 1); endHeight = imgHeight;
startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 20
case 20:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
}
// Maintain the oldAvg Color variable
oldAvg = getBlockAverage(startHeight, endHeight, startWidth,
endWidth, img, blockImg, oldAvg);
// We subtract 1 from quadCount below to accomodate
// our Array indexing which must start at 0.
quadrantColors[quadCount - 1] = oldAvg;
// increment our quadrant counter by 1.
quadCount++;
}
}
Then from somewhere in your application I would initiate all this like so:
// We declare our array to handle 20 elements since
// the image will be dissected into 20 quadrants.
Color[] quadrantColors = new Color[20];
BufferedImage img = null;
// Fill our Color Array...
getQuadrantsColorAverages(quadrantColors, img);
// Let's see what we managed to get....
for (int i = 0; i < quadrantColors.length; i++) {
Color clr = quadrantColors[i];
int red = clr.getRed();
int green = clr.getGreen();
int blue = clr.getBlue();
System.out.println("The average color for Quadrant #" +
(i + 1) + " is: RGB[" + red + "," + green + "," + blue + "]");
}
Well...that's it QQCompi. I hope it sort of helps you out a little.

Image over another already drawn image in CAPTCHA

I want to generate a captcha by myself without using any other API, but here are some issues. In my code I have treated each character as an image and then form some operation on them but now I want to rotate whole image with respect to its midpoint.
Here is my sample code:
for (int i = 0; i < charsToPrint; i++)
{
double randomValue =Math.random();
int randomIndex = (int) Math.round(randomValue * (chars.length - 1));
char characterToShow = chars[randomIndex];
finalString.append(characterToShow);
int charWidth = fontMetrics.charWidth(characterToShow);
int charDim = Math.max(maxAdvance, fontHeight);
//int halfCharDim = (int) (charDim / 2);
charImage = new BufferedImage(charDim, charDim,BufferedImage.TYPE_INT_ARGB);
charGraphics = charImage.createGraphics();
charGraphics.setColor(textColor);
charGraphics.setFont(textFont);
int rn = ran.nextInt((20 - 10) + 1) + 10;
x1=charDim/rn;
int rn1 = ran.nextInt((20 - 10) + 1) + 10;
y1=charDim/rn1;
int charX = (int) ((0.5 * charDim - 0.5 * charWidth)+x1);
int charY=(int) ((charDim - fontMetrics.getAscent()) / 2 + fontMetrics.getAscent()+y1);
charGraphics.drawString("" + characterToShow, charX, charY);
float x = horizMargin + spacePerChar * (i) - charDim / 2.0f;
int y = (int) ((height - charDim) / 2);
g.setColor(Color.GREEN);
g.drawImage(charImage, (int) (x+x1), (int)(y+y1), charDim,charDim, null,null);
charGraphics.dispose();
}

Do-While loop in Java

So, as far as I understand it, a do while loop will always run at least once? But if this is the case why do we need to declare and initialise variables outside of the loop?
Take for instance the following code:
do {
int a = (int) (Math.random() * 13);
int b = (int) (Math.random() * 13);
int c = (int) (Math.random() * 13);
int d = (int) (Math.random() * 13);
}
while (a + b + c + d != 24);
Which will throw a compile error that a, b, c, d may not have been initialised. As I'm a java beginner I'm sure there's a simple reason for this, but I cannot seem to find it?!
Much thanks for any help with this.
Look up variable scope because that is your problem: you're trying to access variables outside of their declared scope, here the do-while loop, and this cannot be done.
Note your code will work if you introduce one more variable:
int sum = 0; // scope is *outside* of do-while loop
do {
int a = (int) (Math.random() * 13);
int b = (int) (Math.random() * 13);
int c = (int) (Math.random() * 13);
int d = (int) (Math.random() * 13);
sum = a + b + c + d;
} while (sum != 24);
But note that now if you still need to access the a, b, c, and d values, you cannot. To allow this, again, you should declare your variables before the loop.
This can be re written like this
int a = (int) (Math.random() * 13);
int b = (int) (Math.random() * 13);
int c = (int) (Math.random() * 13);
int d = (int) (Math.random() * 13);
while (a + b + c + d != 24){
a = (int) (Math.random() * 13);
b = (int) (Math.random() * 13);
c = (int) (Math.random() * 13);
d = (int) (Math.random() * 13);
//do something
}
do {
int a = (int) (Math.random() * 13);
int b = (int) (Math.random() * 13);
int c = (int) (Math.random() * 13);
int d = (int) (Math.random() * 13);
}
while (a + b + c + d != 24);
This is a scoping issue. Look at jls 6.3. Scope of a Declaration
You want to re-write the code as so:
int a = 0; //I am being explicit here
int b = 0;
int c = 0;
int d = 0;
do {
a = (int) (Math.random() * 13);
b = (int) (Math.random() * 13);
c = (int) (Math.random() * 13);
d = (int) (Math.random() * 13);
}
while (a + b + c + d != 24);

Convert Vec4i to Java openCV

I'm new to OpenCV in Android. I'm trying to covert a C++ code into Java. I am stuck in some point that I cannot continue.
std::vector<cv::Vec4i> lines;
cv::HoughLinesP(bw, lines, 1, CV_PI/180, 70, 30, 10);
// Expand the lines
for (int i = 0; i < lines.size(); i++)
{
cv::Vec4i v = lines[i];
lines[i][0] = 0;
lines[i][1] = ((float)v[1] - v[3]) / (v[0] - v[2]) * -v[0] + v[1];
lines[i][2] = src.cols;
lines[i][3] = ((float)v[1] - v[3]) / (v[0] - v[2]) * (src.cols - v[2]) + v[3];
}
half way I converted.. upto the TODO
MatOfInt4 lines= new MatOfInt4();
Imgproc.HoughLinesP(bw, lines, 1, Math.PI/180, 70, 30, 10);
int[] lineArray = lines.toArray();
// Expand the lines
//TODO
for (int i = 0; i < lineArray.length; i++)
{
int v = lineArray[i];
lines.[i][0] = 0;
lines[i][1] = ((float)v[1] - v[3]) / (v[0] - v[2]) * -v[0] + v[1];
lines[i][2] = src.cols();
lines[i][3] = ((float)v[1] - v[3]) / (v[0] - v[2]) * (src.cols() - v[2]) + v[3];
}
which I'm confused is inside the for loop. when converted lines in to a Array it gives a int array. But inside the for loop again v is defined which should be a array. I didn't get this point. Can anybody please help me to get through this. Thank you in advance.
Finally I managed to write the working code. with help of this link
Mat lines = new Mat();
int threshold = 70;
int minLineSize = 30;
int lineGap = 10;
Imgproc.HoughLinesP(thresholdImage, lines, 1, Math.PI / 180, threshold,
minLineSize, lineGap);
for (int x = 0; x < lines.cols(); x++) {
double[] vec = lines.get(0, x);
double[] val = new double[4];
val[0] = 0;
val[1] = ((float) vec[1] - vec[3]) / (vec[0] - vec[2]) * -vec[0] + vec[1];
val[2] = src.cols();
val[3] = ((float) vec[1] - vec[3]) / (vec[0] - vec[2]) * (src.cols() - vec[2]) + vec[3];
lines.put(0, x, val);
}

Categories