Disposing Graphics2D drawing after it is drawn - java

For a game that I'm working on I need to draw a rectangle that gets smaller and smaller. I have figured out how to draw the rectangle smaller by using a swing Timer like this:
timer = new Timer(100, new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics2D g2d = (Graphics2D) panel.getGraphics();
if(width > 64){
g2d.drawRect(x,y,width,height);
x += 1;
y += 1;
width -= 1;
height -= 1;
}
}
});
timer.start();
The problem I am having is that it wont remove the rectangle that was drawn before so it wont look like it's shrinking but more like it's filling in. So how would I remove the previously drawn rectangle right after the smaller rectangle have been drawn?

You might start with:-
Change:
Graphics2D g2d = (Graphics2D) panel.getGraphics();
to:
repaint();
The Graphics instance from getGraphics() is transient, the window might be repainted whenever the JVM feels it is necessary.
The overridden method might look like this.
#Override
public void paintComponent(Graphics g){
super.paintComponent(g); // Effectively clears the BG
Graphics2D g2d = (Graphics2D)g;
if(width > 64){
g2d.drawRect(x,y,width,height);
x += 1;
y += 1;
width -= 1;
height -= 1;
}
// Toolkit.getDefaultToolkit().sync();
// g2d.dispose(); NO! Don't dispose of this graphics instance
}

Related

Graphics in one object is influencing another object's graphics - JAVA

First post, so I'll try to be as clear as I can.
Basically, what I'm trying to do is create a little game, where you have a ship, and you can shoot bullets from it. The ship rotates accordingly to the player's deltaX and deltaY using a tangent formula. I use a standard game loop like this one:
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
#SuppressWarnings("unused")
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >=1){
tick();
delta--;
}
if(running)
repaint();
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
frames = 0;
}
}
stop();
}
My "game" runs on a child of a JPanel, a class called Board. In the loop, I call the methods tick(), which updates the info, and repaint(), that works as a render method. This is my paintComponent(Graphics g) method:
public void paintComponent(Graphics g) {
//This is for background
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
g2d.fill(new Rectangle(0, 0, Constants.width, Constants.height));
//This is where actual game rendering occurs
handler.render(g);
g.dispose();
}
As can be seen, I dont render everything on my Board class. I do so on my handler. This is how I deal with the handler:
public void render(Graphics g) {
for (int i = 0; i < handlerList.size(); i++) {
//handlerList is a LinkedList
handlerList.get(i).render(g);
}
}
The LinkedList handlerList contains Entities.
Entities is an abstract class, which is parent of Creature, which is parent of Player and Bullet.
This is the code for the rendering of a Player instance:
public void render(Graphics g) {
float centerX = x + (width / 2);
float centerY = y + (height / 2);
double theta = findAngle(deltaX, deltaY);
Graphics2D g2d = (Graphics2D) g;
if(!stopped) g2d.rotate(theta, centerX, centerY);
else g2d.rotate(stoppedTheta, centerX, centerY);
g2d.drawImage(shipImage, (int)x, (int)y, (int)width, (int)height, null);
}
There is a boolean "stopped" which keeps track of the objects condition. I use Graphics2D instead of Graphics due to the fact that I wanna be able to rotate my ship.
Here's the code for the bullet's rendering:
public void render(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.yellow);
g2d.fillOval((int)x, (int)y, (int)width, (int)height);
}
It looks right, as far as I'm aware. Whenever I don't have a bullet on, the game runs fine, as you can see in this GIF:
DISCLAIMER: Sorry for low quality and the watermark, I've just formatted the computer and havent had time to install proper stuff....
When I add a bullet this happens:
The x and y position of the bullet doesn't change, but the bullet rotates with the ship. I'm assuming it has something to do with the misuse of the "dispose()" method, but I'm not sure what can be done to fix it.
Thank you in advance.
Don't ever call dispose on a Graphics context you did not explicitly create (or snapshot with create), this can cause issues further down the rendering pipeline.
Graphics is a shared context, so you need to be mindful of the changes you make to it and undo any "significant" changes you make, especially transformations
If it was me, I'd create a snapshot of the Graphics context before each call to render and the dispose of it after, for example
public void render(Graphics g) {
for (int i = 0; i < handlerList.size(); i++) {
//handlerList is a LinkedList
Graphics2D g2d = (Graphics2D)g.create();
handlerList.get(i).render(g2d);
g2d.dispose();
}
}
This ensures that what ever changes that render makes to the Graphics context are undone before the next element is rendered
If the changes are compounding, then I'd make the snapshot before the start of the loop and dispose of it after it.
In either case, it means you control the changes been made and how they affect other elements down the line.
Also, remember, transformations are compounding

The angle of rotation of the object is wrong

I'm trying to rotate an object in java but i noticed that something is wrong.
When i rotate it of 180 degrees,i get a value of the angle of '90°',so in order to get an angle of 360 degrees i have to rotate it twice.
What's wrong?
0°,90°,180°
The code:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (rotate == true) {
ship.increaseDegress();
}
ship.draw(g);
}
Ship.java
public void increaseDegress() {
rotationAngle += 10;
if(rotationAngle>360) {
rotationAngle = 0;
}
}
public void draw(Graphics g) {
this.g = g;
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = new AffineTransform();
Rectangle rect = this.getBounds();
at.rotate(Math.toRadians(rotationAngle), rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
g2.setColor(Color.BLUE);
g2.setTransform(at);
g2.draw(at.createTransformedShape(this));
}
So, let's take a quick look at your code...
public void draw(Graphics g) {
this.g = g;
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = new AffineTransform();
Rectangle rect = this.getBounds();
at.rotate(Math.toRadians(rotationAngle), rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
g2.setColor(Color.BLUE);
g2.setTransform(at);
g2.draw(at.createTransformedShape(this));
}
So,
First you create AffineTransform, nice
You rotate the transformer, also nice
You apply the transformer to the Graphics context ... okay, there's a problem here, but let's move on
You apply the transformer to the shape!
So, on a single draw pass, you will rotate the shape by rotationAngle * 2! So, when the angle is 10°, the shape will be rendered at 20°, when it's 20°, it will rendered at 40°!
Okay, but there's another problem. Transformations applied to a Graphics context are compounding, this means, based on the available code, each time you call draw, the Graphics context is been rotated by rotationAngle. So if rotationAngle is 10°
On pass #1, the shape will be rotated to 20°
On pass #2, the shape will be rotated to 30°
On pass #3, the shape will be rotated to 40°
... so on and so forth ...
So, what's the answer?
When ever I pass a Graphics context off to some other method, I first create a copy it, because I don't trust anybody!
Graphics gCopy = g.create();
shape.draw(gCopy);
gCopy.dispose();
This ensures that the state of the Graphics context is returned to the state it was before I called draw, that will get rid of the compounding transformation.
The other solution is, don't transform the Graphics context. If you're transforming the shape, what's the point?
public void draw(Graphics g) {
this.g = g;
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = new AffineTransform();
Rectangle rect = this.getBounds();
at.rotate(Math.toRadians(rotationAngle), rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
g2.setColor(Color.BLUE);
g2.draw(at.createTransformedShape(this));
}

Java Animation Rotation

I have very little experience with Java, and I am an amateur programmer. So mind my vocabulary.
I want to be able to stick a static rectangle on top of a rotating rectangle.
So far when I try to add another object it spins with the other image. I have tried setting the rotation to zero but that doesn't seem to work. I have also tried to create another class that draws components separately and added them to the frame using frame.add. I have also tried creating another part to the Draw class that has no effect on the GUI. Here is my current Draw class. Any help is appreciated.
class DrawRectangle extends JPanel {
#Override
public void paintComponent(Graphics g) {
int h = this.getHeight();
int w = this.getWidth();
Graphics2D g2 = (Graphics2D) g;
//draw background
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, w, h);
//draw roatiing rectangle
g2.setColor(Color.CYAN);
Rectangle rRec = new Rectangle(w / 4, h / 4, 2 * w / 4, 2 * h / 4);
double wr = rRec.getX() + rRec.getWidth() / 2;
double hr = rRec.getY() + rRec.getHeight() / 2;
g2.rotate(Math.toRadians(count), wr, hr);
g2.fill(rRec);
g2.fillRect(w / 3, h / 3, 2 * w / 3, 2 * h / 3);
}
public void paintComponent2(Graphics g) {
int h = this.getHeight();
int w = this.getWidth();
Graphics2D g2 = (Graphics2D) g;
}
}
So far when I try to add another object it spins with the other image.
Create a separate Graphics object to do the rotation so you don't affect the properties of the Graphics object passed into the painting method:
//Graphics2D g2 = (Graphics2D) g;
Graphics2D g2 = (Graphics2D)g.create();
// painting code
g2.dispose();
Move your g2.fill(rRec); BEFORE the rotate call, and it should work (I just tested it out).
This way, you will draw your static rectangle before the rotation, perform the rotation, THEN draw your second rectangle. Assuming your count variable is incremented somewhere, it should show the second rectangle being rotated.

Current Coordinate of bufferedImage Object in java

Is there anyway to find the current position of a buffered image on jpanel?
I draw an image on buffer, named it currentImage. Now I want to move it around a Panel by using affine transform.
To do translation by using mouse, I have to know the current position, new position and update the new position.
But I have trouble in getting the current position of the BufferedImage. There is no method in the IDE's suggestion working.
Can anyone give me an idea how to do this ?? Thanks!
EDIT
this is my draw and paintComponent method:
private void draw(){
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC);
int w = this.getWidth(); int h = this.getHeight();
if (currentImage == null || width != w || height != h){
width = w;
height = h;
currentImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
graphics = currentImage.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
drawClearAndGradedArea(graphics);
drawActualRunway(graphics, width, height, width, height);
drawLeftToRight(graphics);
drawRightToLeft(graphics);
drawCentralLine(graphics);
graphics.setComposite(ac);
}
}
paintComponent()
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.clearRect(0,0, this.getWidth(), this.getHeight());
RenderingHints rh = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
/*
* main components on panel
*/
this.draw();
this.animation();
g2d.drawImage(currentImage, transform, this);
drawNote(g2d);
g2d.dispose();
}

Repaint() not repainting a certain object/updating xy coordinates of a shape

Essentially, in this program bugs move around the screen eating food.
The issue that I am having is that graphics "circle" repaints fine and moves around the frame, but none of the bugs are repainted (However they are drawn initially).
I've tested it with console outputs and the x and y coordinates of the bugs do update, but the graphic in the frame does not.
So my question is, why is it the x/y of the circle is updated and repainted, but the bugs are not?
(As far as I can see, I'm approaching the task of changing the bugs x/y in the same way as with the circle)
(I've removed some of the things like the world getters and setters, and cut the code down a bit. The actual program has a movebug() method which handles the moving of the bugs in a correct fashion with hit detection.)
public class Run extends JPanel implements ActionListener{
double x, y, velX, velY, newx, newy;
public Timer t;
private static AWorld world1;
ArrayList<Ellipse2D> graphicBugArrayList = new ArrayList<Ellipse2D>();
public Run(){
x = 150;
y = 150;
velX = 2;
velY = 2;
t = new Timer(5, this);
setWorld1(new AWorld());
getWorld1().populatemap();
AWorld.direction directionchecker;
repaint();
t.start();
}
public void paintComponent(Graphics g){
//Creating circle that move
g.setColor(Color.BLACK);
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D circle = new Ellipse2D.Double(x, y, 10, 10);
//creating and drawing the bugs
for(int i=0; i<world1.getNumberofbugs(); i++)
{
Ellipse2D obscircle = new Ellipse2D.Double(getWorld1().bugArrayList.get(i).getHPosition(), getWorld1().bugArrayList.get(i).getVPosition(), 14, 15);
graphicBugArrayList.add(obscircle);
g2.setPaint(Color.RED);
g2.fill(graphicBugArrayList.get(i));
g2.draw(graphicBugArrayList.get(i));
}
//Drawing the circle that moves
g2.setPaint(Color.BLACK);
g2.draw(circle);
}
public void actionPerformed(ActionEvent arg0) {
//Moving the circle
if(x<0 || x > 500)
{
velX = -velX;
}
if(y<0 || y > 500)
{
velY = -velY;
}
x += velX;
y -= velY;
//Test moving the bugs
getWorld1().bugArrayList.get(i).setHPosition(getWorld1().bugArrayList.get(i).getHPosition()+1);
repaint();
}
}
You never clear your graphicBugArrayList, but only draw the first few elements over and over. Try this in your loop:
for(int i=0; i<world1.getNumberofbugs(); i++)
{
Ellipse2D obscircle = new Ellipse2D.Double(getWorld1().bugArrayList.get(i).getHPosition(), getWorld1().bugArrayList.get(i).getVPosition(), 14, 15);
graphicBugArrayList.add(i, obscircle);
g2.setPaint(Color.RED);
g2.fill(graphicBugArrayList.get(i));
g2.draw(graphicBugArrayList.get(i));
}

Categories

Resources