I'm making a game which involves multiple "balls" using the same class, I have everything working the way I want it to using a ArrayList but my problem is that the more of the balls I add the slower it renders them. This makes the game look like it is going slow and it will flicker, I can't provide a video sorry, but I can provide the code I am using the draw the balls:
code:
for(int i=0;i<balls.size(); i++){
Ball tmp = (Ball) balls.get(i);
g2d.drawImage(tmp.getImage(), tmp.getX(),tmp.getY(),null);
}
Could you give me examples or direction for a better way to render the balls?
Thanks.
For the flickering, you'll want to learn about double buffering. If the slowness is something aside from perception due to not double buffering, you'll have to profile it or maybe show some more code.
I'm not sure of the speed ramifications (probably not much), but you should be specifying a type when using an ArrayList (i.e ArrayList<Ball>) instead of typecasting everything as you get it.
Related
How do you draw elements with JBox2D? I'm fine using the DebugDraw, I just want to find a quick way to do this in Java since I haven't worked much with graphics.
Do I need to use a Canvas? Or a JFrame? And how does the world know when I call
world.drawDebugData()
where to draw it to?
How could I devise a class that just drew points where I want them, and integrate this with JBox2D?
...
while(true)
world.step(timeStep, velocityIterations, positionIterations);
Vec2 position = body.getPosition();
float angle = body.getAngle();
System.out.printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
}
I imagine I could put this code somewhere inside this while loop, but I'm not exactly sure where. Is there a function that's called every time a World steps? Can I put a draw function in there? I'm even ok using the DebugDraw if I could figure it out...
Could you please help me understand what kind of class would take all the objects in the World object and draw them continually? (and where to draw to?)
You can easily create new testbed tests, this is the best way to test out your new physics ideas and not worry about drawing and GUI stuff. It even supports adding extra real-time options. Check it out here:
https://code.google.com/p/jbox2d/wiki/Testbed
It's difficult to know where to organize everything if you're not used to programming games and physics engines.
Dyn4j is another great physics library that has great examples explaining where to put everything and even how the user can interact with the world. A lot about the dyn4j library is identical to box2d/jbox2d, so if you just try out their examples real quick, it will lend a hand to figuring out how to render graphics in box2d.
Hey dudes and dudettes.
High-school Comp Sci student reporting in.
Right now we're doing some work with plotting and polygons, and one of the challenges is to make it move across the screen in a variety of ways. (left to right, etc.) See, this would be no issue if I knew the language or had the means to study it at a basic level. But our teacher just hands us some environment and some code to copy and paste and look off of, none of which really helps in the long run, except for the most basic stuff.
So, my friends, I can make a polygon through plotting points as shown here:
int xPoints[]={ xP*684/1000,xP*706/1000, xP*661/1000, xP*687/1000,
xP*735/1000,xP*760/1000, xP*723/1000, xP*713/1000,
xP*698/1000,xP*686/1000, xP*669/1000, xP*653/1000,
xP*639/1000,xP*639/1000, xP*641/1000, xP*648/1000};
int yPoints[]={ yP*354/1000, yP*354/1000, yP*472/1000, yP*472/1000,
yP*354/1000, yP*354/1000, yP*452/1000, yP*471/1000,
yP*487/1000, yP*498/1000, yP*503/1000, yP*504/1000,
yP*492/1000, yP*473/1000, yP*455/1000, yP*440/1000};
int numPoints=16;
But I cannot figure out for the life of me what kind of specific input or code is required to make it move. (by that I mean translate across the the screen)
Any hints to get me on the right track would be greatly appreciated. I don't exactly have the means of figuring this out on my own.
Edit: Whoops, yep it's Java.
Welcome to SO! You need to break this down into small parts that you need to figure out how to do. Some that come to mind are
Create a window to draw in.
Draw a line in that window.
Draw a polygon in the window.
Move a point from (x, y) in a given direction.
Each of these can likely be broken down even further. Often solving a programming problem starts with this kind of thinking. As you try to do each of these, and any others that you come up with, please feel free to come back and ask more questions.
I don't know IBM VisualAge but if you have any draw method/loop, you should increase xP to move to right, decrease it to move left etc. with a for loop, for example.
Maybe a for loop?
for(int i=0;i<xPoints.length;i++)
xPoints[i]+=1;// shift 1 unit
for(int i=0;i<yPoints.length;i++)
yPoints[i]+=1;// shift 1 unit
Assuming that it's a java.awt.Polygon, have you looked at
Polygon.translate(int dx, int dy); ???
If it is some other kind of Polygon, check to see if it has a similar method.
I'm currently working on my 2D game project (Java), but so far any kind of game logic or AI has been crudely implemented. For instance, say I need to randomly position a bunch of sprites a long the top of the screen, I'd be using the Random class to do this. I'd simply use Random.nextInt( size of x axis on which to spawn ); Although this does work I'd be interested to hear how I should really be going about this kind of thing.
As a second scenario (this is why I put AI in the title, although it's not really AI), say I want to have my characters randomly blink in a life-like fashion. What I'd do here is use the Random class to calculate a % (say 20% chance) of blinking and call it every second.
Any suggestions on how I should really be going about this would be greatly appreciated.
Google for a paper titled "Steering Behaviors" by Craig Reynolds. It address just this and you'll find great ideas to start with specifically some nice ideas for giving groups of sprites the appearance of 'intelligent' movement. The key for him in his different behaviors, i.e. flocking, etc. is making properties of any given sprite dependent on those of some other sprite. You could even go so far as to say, like -- any given sprite will blink only if it's two neighbors just have blinked. Something or other along those lines.
Hope this helps!
Are you using an OOP (Object-Oriented approach)? If not, you should definitely look into it. It's really simple with java and can speed up your development time and neaten your code.
I would make a sprite class, and give them a function, say actionSpawn, or actionMove (I like to start my "action" functions with the word action so they are easily identifiable). In this function you would encapsulate the Random.nextInt function, to set the sprite's x and/or y position.
You could use the same approach to make them blink.
I've looked on Google and still couldn't find anything. I had an idea for a simple Snake type game or like a 'Coin Collection' game using 2D graphics, but if a coin is a graphic and the moving character is a graphic, how do I check if the character goes over the coin? I'm stumped. Any ideas?
For a crude implementation have all your sprites backed by a Rectangle2D object, and use the intersects method to test for collision. Caveat, this is very crude!
Yes, the classic Picking and Selection problem. It's a bit long to explain here - please read http://download.oracle.com/javase/tutorial/2d/advanced/user.html . And also, the easiest is to use contains(MousePoint) .
See this Picking in java 2d .
I can't think of any way to do this using the graphics package; moreover, I think this is something you should do in your model rather than your graphics.
The problem you're looking at is generally called "collision detection". There are many different approaches to this; looking around online for some guides would be useful. However, I think one simple approach is to think of each object (coin, snake...etc) as a rectangle, making the math really simple. Circles (for the coin) should not be too bad either.
I'm looking at creating a program with Processing (processing.org) in Java. The program will involve graphing a large amount of 2D data. I would like for the points to be displayed to fill the window. I've looked at their libraries and I don't see anything for data visualization. Am I missing something?
I've always used JFreechart or, for more complex graphing exporting to a text flie and then gnuplot.
another vote for JFreeChart. Although for more complex graphing I've written my own (AWT).
JUNG Is a favorite of mine.
Processing is really powerful and can be considered a "raw" language given how close you can get to actual graphic programming. I've myself created many graphs and can tell you that you have to be very careful when using this library. It's great but you have to do everything from scratch. This means creating lines for the x and y axis, creating your labels, creating the space, etc.
My suggestion is to set the number of points you'll most likely have, say 1000, and always display with that much data. If you have too little or too much, just adjust it before sending it to graph. This way you'll always have a set number. From here what you do is the following:
pushMatrix();
scale(widthOfGraph/1000, heightOfGraph/numberOfPointsUp);
beginShape(LINES);
for (int i = 0; i < 1000; i++) {
vertex(x0,y0);
vertex(x1,y1);
endShape();
popMatrix();
This will create all your lines in a single drawing operation meaning you'll save a lot of opening and closing shapes. You are also using a stack matrix to use the scale operation to adjust the displaying size of your canvas. Everything else is up to you. Hope that helps.