I'm trying to create a jslider that moves withing the following ranges.
[-x,-1)[1,x]
In short I don't want the values -1 and 0 to be valid values for the JSlider.
But the values from x to -1 should be allowed, and 1 to x should be allowed.
I'm trying to not write hacky code, so I don't want to write a function in the UI code that just gets the value from a different (continuous) range, and then transforms it to the range I want with a bunch of it statements.
Ideally, I should just be able to call slider.getValue() and know that the return value will be in the range I described above.
I think you must do this value adjustment yourself perhaps within overridden method setValue()?
Try out this code:
int x = 10;
#Override
public void setValue(int n)
{
if((n >= -x && n < -1)|| (n =< x && n >= 1))
{
super.setValue(n);
System.out.println("OI in setValue");
}
}
A slider's value is just the ratio between the thumb's current position and the sliders width, in pixels. IIUC, you have three ranges, [-x,-1), [-1,1) and [1,x], so you'll need two thumbs. You might look at JXMultiThumbSlider, which supports a MultiThumbModel.
Addendum: For a related use-case, I started with How to Write a Custom Swing Component. It's laborious, but it may produce a cleaner result.
Related
I'm trying to write a Java program to calculate the square root of an integer x, without using in-built functions like Math.pow() . This is the approach I tried -
class Solution {
public int mySqrt(int x) {
if(x==0 || x==1)
return x;
// if(x>=2147395600)
// return 46340;
int i;
for(i=1 ; i*i<=x ; i++) {}
return i-1;
}
}
Without the commented part, I start getting errors if x is in the range 2147395600 <= x <= 2^31-1 (which is the upper limit of an int's value range in Java). For instance, for the input x=2147395600, the expected output is 46340 but the actual output is 289398. Why is this happening? Thanks to all in advance.
PS - I am aware there are other (better) methods to solve this problem, but I'd really like to know why this code behaves this way.
Since 46340 * 46340 = 2147395600, when i=46340, x=2147395600 and you reach the condition i*i<=x it evaluates to true since 2147395600 = 2147395600. So the loop counter will incremnet by 1 and in the next iteration we will get i=46341 and i * i will cause an overflow - 46341*46341 = -2147479015.
The loop condition will still be true, since -2147479015 <= 2147395600, and the loop will not stop.
You can replace the <= with =, and check for edge cases that may occur now.
I have created a gameboard (5x5) and I now want to decide when a move is legal as fast as possible. For example a piece at (0,0) wants to go to (1,1), is that legal? First I tried to find this out with computations but that seemed bothersome. I would like to hard-code the possible moves based on a position on the board and then iterate through all the possible moves to see if they match the destinations of the piece. I have problems getting this on paper. This is what I would like:
//game piece is at 0,0 now, decide if 1,1 is legal
Point destination = new Point(1,1);
destination.findIn(legalMoves[0][0]);
The first problem I face is that I don't know how to put a list of possible moves in an array at for example index [0][0]. This must be fairly obvious but I am stuck at this for some time. I would like to create an array in which there is a list of Point objects. So in semi-code: legalMoves[0][0] = {Point(1,1),Point(0,1),Point(1,0)}
I am not sure if this is efficient but it makes logically move sense than maybe [[1,1],[0,1],[1,0]] but I am not sold on this.
The second problem I have is that instead of creating the object at every start of the game with an instance variable legalMoves, I would rather have it read from disk. I think that it should be quicker this way? Is the serializable class the way to go?
My 3rd small problem is that for the 25 positions the legal moves are unbalanced. Some have 8 possible legal moves, others have 3. Maybe this is not a problem at all.
You are looking for a structure that will give you the candidate for a given point, i.e. Point -> List<Point>.
Typically, I would go for a Map<Point, List<Point>>.
You can initialise this structure statically at program start or dynamically when needing. For instance, here I use 2 helpers arrays that contains the possible translations from a point, and these will yield the neighbours of the point.
// (-1 1) (0 1) (1 1)
// (-1 0) (----) (1 0)
// (-1 -1) (0 -1) (1 -1)
// from (1 0) anti-clockwise:
static int[] xOffset = {1,1,0,-1,-1,-1,0,1};
static int[] yOffset = {0,1,1,1,0,-1,-1,-1};
The following Map contains the actual neighbours for a Point with a function that compute, store and return these neighbours. You can choose to initialise all neighbours in one pass, but given the small numbers, I would not think this a problem performance wise.
static Map<Point, List<Point>> neighbours = new HashMap<>();
static List<Point> getNeighbours(Point a) {
List<Point> nb = neighbours.get(a);
if (nb == null) {
nb = new ArrayList<>(xOffset.length); // size the list
for (int i=0; i < xOffset.length; i++) {
int x = a.getX() + xOffset[i];
int y = a.getY() + yOffset[i];
if (x>=0 && y>=0 && x < 5 && y < 5) {
nb.add(new Point(x, y));
}
}
neighbours.put(a, nb);
}
return nb;
}
Now checking a legal move is a matter of finding the point in the neighbours:
static boolean isLegalMove(Point from, Point to) {
boolean legal = false;
for (Point p : getNeighbours(from)) {
if (p.equals(to)) {
legal = true;
break;
}
}
return legal;
}
Note: the class Point must define equals() and hashCode() for the map to behave as expected.
The first problem I face is that I don't know how to put a list of possible moves in an array at for example index [0][0]
Since the board is 2D, and the number of legal moves could generally be more than one, you would end up with a 3D data structure:
Point legalMoves[][][] = new legalMoves[5][5][];
legalMoves[0][0] = new Point[] {Point(1,1),Point(0,1),Point(1,0)};
instead of creating the object at every start of the game with an instance variable legalMoves, I would rather have it read from disk. I think that it should be quicker this way? Is the serializable class the way to go?
This cannot be answered without profiling. I cannot imagine that computing legal moves of any kind for a 5x5 board could be so intense computationally as to justify any kind of additional I/O operation.
for the 25 positions the legal moves are unbalanced. Some have 8 possible legal moves, others have 3. Maybe this is not a problem at all.
This can be handled nicely with a 3D "jagged array" described above, so it is not a problem at all.
I am creating a platformer and I have a class called Platform that takes an x position, and y position, a width, a height and a color, then sets them inside of that platform.
platform = new Platform(100, 250, 100, 10, Color.BLUE);
Like this.
I can call the variables as such:
g.fillRect(platform.x, platform.y, platform.width, platform.height);
and when I want to get the color of the platform I do this:
g.setColor(platform.color);
I also have a player that works very similarly.
I have a collision method:
int LXOff = (platform.x - player.width); // boundaries
int LYOff = (platform.y - player.height); // boundaries
int RXOff = (platform.x + platform.width); // boundaries
int RYOff = (platform.y + platform.height); // boundaries
int LXOff2 = (platform2.x - player.width);
int LYOff2 = (platform2.y - player.height);
int RXOff2 = (platform2.x + platform2.width);
int RYOff2 = (platform2.y + platform2.height);
if(x <= 5 || y <= 29) // numbers are specific to the perfect border of the screen
{
return true;
}
if(x >= 495 - player.width || y >= 495 - player.height) // numbers are specific to the perfect border of the screen
{
return true;
}
if(x >= LXOff && y >= LYOff && x <= RXOff && y <= RYOff)
{
return true;
}
if(x >= LXOff2 && y >= LYOff2 && x <= RXOff2 && y <= RYOff2)
{
return true;
}
return false;
My problem is that I need to create a whole extra set of variables that represent the boundaries that a second platform uses. I'm wondering that for future use if I were to use a larger amount of platforms, how would I simplify it using an array or something like that of Platforms, then I could draw all the platforms in the array using some loop.
I believe for a platform you will use you will create a new Platform object. create a Map to create a map of platform names as key and platform objects as values. Retrieve the Platform object by the platform name when required
Platform android = new Platform(....);
Platform windows = new Platform(....);
Map<String, Platform> map = new HashMap<String, Platform>();
map.put("android", android);
map.put("windows", windows);
//for retrieval
map.get("android");
If the borders are variable the I would suggest to create an abstract class with the common logic and a platform specific class for each Platform.
Platform[] platforms = new Platform[N]
where N is size of array.
If you do not no size of array at the moment of creation of array, you should better use
List<Platform> platforms = new ArrayList<Platform>()
If I understand your question, you have several Platforms to draw, but you also want to draw some Boundaries? The O-O way would be to define a common interface for both.
One option would be to define an interface, perhaps RectangleLike, with getX(), getWidth(), getColor() etc... Then you loop over a big array (note: better to use a List) of RectangleLike and call the appropriate code.
Alternatively, and, IMO better, define an interface, perhaps CanDrawIntoGraphics, with one method, drawYourself(Graphics g). Loop over a List of them calling drawYourself().
As Java docs states that
A Collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers).
So In order to Maintain Number of Platform for future use , You can make a collection of Platform instead of Array , because Collection has the ability to expand and shrink dynamically .
Coming to Coding side either use Map or List of Platforms and if you want to remove duplicay Use Set Interface
Suppose You are having 2 Platform then you can use this
List<Platform> platformList=Arrays.asList(platform1,platform2);
To Iterate through any collection Implementation Use , Iterator Interface
For Map You can see example posted by #dc.sashwat
I got this solution you have to send an arraylist of Platform
List <Platform> platformList =new ArrayList<Platform> ();
platformList.add(platform1);
platformList.add(platform2);
Send this platformList to your method than there
foreach(Platform platform:platformList){
int LXOff = (platform.x - player.width);
int LYOff = (platform.y - player.height);
int RXOff = (platform.x + platform.width);
int RYOff = (platform.y + platform.height);
if(x >= LXOff && y >= LYOff && x <= RXOff && y <= RYOff)
{
return true;
}
}
I am generating my world (random, infinite and 2d) in sections that are x by y, when I reach the end of x a new section is formed. If in section one I have hills, how can I make it so that in section two those hills will continue? Is there some kind of way that I could make this happen?
So it would look something like this
1221
1 = generated land
2 = non generated land that will fill in the two ones
I get this now:
Is there any way to make this flow better?
This seems like just an algorithm issue. Your generation mechanism needs a start point. On the initial call it would be say 0, on subsequent calls it would be the finishing position of the previous "chunk".
If I was doing this, I'd probably make the height of the next point plus of minus say 0-3 from the previous, using some sort of distribution - e.g. 10% of the time it's +/1 3, 25% of the time it is +/- 2, 25% of the time it is 0 and 40% of the time it is +/- 1.
If I understood your problem correctly, here is a solution:
If you generated the delta (difference) between the hills and capped at a fixed value (so changes are never too big), then you can carry over the value of the last hill from the previous section when generating the new one and apply the first randomly genenarted delta (of the new section) to the carried-over hill size.
If you're generating these "hills" sequentially, I would create an accessor method that provides the continuation of said hill with a value to begin the next section. It seems that you are creating a random height for the hill to be constrained by some value already when drawing a hill in a single section. Extend that functionality with this new accessor method.
My take on a possible implementation of this.
public class DrawHillSection {
private int index;
private int x[50];
public void drawHillSection() {
for( int i = 0; i < 50; i++) {
if (i == 0) {
getPreviousHillSectionHeight(index - 1)
}
else {
...
// Your current implementation to create random
// height with some delta-y limit.
...
}
}
}
public void getPreviousHillSectionHeight(int index)
{
return (x[49].height);
}
}
I need to write a method that checks how many possible ways there are to finish a grid (a 2D array).
the movement inside the grid is like this:
start with [0][0] take the number inside there (for instance 14) then go to either
[array.[0][0]%10][array.[0][0]/10] or [array.[0][0]/10][array.[0][0]%10]
for our example:
[1][4] or [4][1]
until you get to the end of the array (bottom right corner).
I can get to the end of the array (all possible ways) - my problem is to count how many times I actually finished the array - I can not use a variable outside of the method, and the method has to be recursive.
this is the code :
private static int howMany(int[][] array, int y, int x, int count) {
if(y+(array[y][x]%10) < array.length && x+(array[y][x]/10)< array[y].length && array[y][x]!=0) {
System.out.println("["+y+"]["+x+"] is: "+array[y][x]);
howMany(array, y+(array[y][x]%10), x+(array[y][x]/10),count);
}
if(y+(array[y][x]/10) < array.length && x+(array[y][x]%10)< array[y].length && array[y][x]!=0) {
System.out.println("["+y+"]["+x+"] is: "+array[y][x]);
howMany(array, y+(array[y][x]/10), x+(array[y][x]%10),count);
}
if(y==array.length-1 && x==array[y].length-1) count++;
return count;
}
this is obviously wrong and will return what count was in the first place, I tried many other ways but to no avail...
here's the full class (with an array to test):
link to full class
edit: a big Thanks to everyone for their help!
The count is already returned from each call to howMany. I think you just need to save it:
count = howMany(array, y + (array[y][x] % 10), x + (array[y][x] / 10), count);
Do this inside both if blocks. I made this change in your linked code and got the expected result (3).
You are already on the right track, since your method signature returns an int. You should define a variable to hold the count, increment it for the primary recursive call, and add to it the result of the recursive method itself. Passing the count into the each recursive call is unnecessary and should be removed.
Return 1 if you've reached the end of the array (bottom right corner) and 1+howMany(array, newY, newX) otherwise. You don't need to keep the count and pass it every time. The function will work so:
1 + returned value of 2nd call =
1 + 1 + returned value of 3rd call =
1 + 1 + 1 + returned value of 4th call =... and so on.
Finally as result you'll get number of calls which is what you want.