user3553104
user3553104

Reputation:

Java Help! paintComponent woes

I am new to Java and am learning new things everyday. I am trying to learn how to take TWO DIFFERENT .png files and rotate them SEPARATELY using the paint component. I am able to make both images paint and have separate movement, however the rotate function does NOT seem to recognize each 2DGraphic variable as independent.

Both images will be rotated to the last '.rotate' angles. I have looked up online but every tutorial referring to rotating a graphic are only dealing with one image. This works fine. I just can not get two images to rotate differently. I want P1GFX to be rotated separately from P2GFX.

Here is the code. THIS CODE WORKS, however they are BOTH rotated to whatever the last .rotate specifies.

public void paintComponent(Graphics frogp1) {
 
 
            Graphics2D P1GFX = (Graphics2D)frogp1;
            Graphics2D P2GFX = (Graphics2D)frogp1;
            P1GFX.rotate(90, 150 / 2, 150 / 2);
            P2GFX.rotate(40, 50, 50);
            P1GFX.drawImage(p1.getImage1(), p1x, p1y,this);
            P2GFX.drawImage(p2.getImage2(), p2x, p2y, this);
       
         
}

So, I tried making multiple parameters in paintComponent! That should work right? NOPE! THIS CODE Makes NO IMAGE appear at all! Nothing is drawn on the screen when there are more than one parameter inside paintComponent!

public void paintComponent(Graphics frogp1, Graphics frogp2) {
 
 
            Graphics2D P1GFX = (Graphics2D)frogp1;
            Graphics2D P2GFX = (Graphics2D)frogp2;
            P1GFX.rotate(90, 150 / 2, 150 / 2);
            P2GFX.rotate(40, 50, 50);
            P1GFX.drawImage(p1.getImage1(), p1x, p1y,this);
            P2GFX.drawImage(p2.getImage2(), p2x, p2y, this);
       
         
}

So I thought, hey! Maybe I need to make more than one paintComponent! Well, of course that is NOT possible without recreating my own instance of the repaint() method.

public void paintComponent1(Graphics frogp1) {
            Graphics2D P1GFX = (Graphics2D)frogp1;
            P1GFX.rotate(90, 150 / 2, 150 / 2);
            P1GFX.drawImage(p1.getImage1(), p1x, p1y,this);
                                     
}
public void paintComponent2(Graphics frogp2) {
            Graphics2D P2GFX = (Graphics2D)frogp2;
            P2GFX.rotate(90, 150 / 2, 150 / 2);
            P2GFX.drawImage(p2.getImage2(), p2x, p2y,this);
                                     
}

This makes repaint() do nothing, therefore nothing is drawn.

Please help me be able to rotate more than one image / Graphics2D variable !

Upvotes: 0

Views: 100

Answers (2)

John
John

Reputation: 3797

You could just rotate, then unrotate and reposition:

public void paintComponent(Graphics graphics) {
        Graphics2D g2d = (Graphics2D)graphics;
        g2d.rotate(90, 150 / 2, 150 / 2);
        g2d.drawImage(p1.getImage1(), p1x, p1y,this);
        g2d.rotate(-90, 150 / 2, 150 / 2);
        g2d.rotate(40, 50, 50);
        g2d.drawImage(p2.getImage2(), p2x, p2y, this);
}

Upvotes: 0

camickr
camickr

Reputation: 324078

Graphics2D P1GFX = (Graphics2D)frogp1;
Graphics2D P2GFX = (Graphics2D)frogp1;

Casting an Object means you are still using the same Object reference.

If you want two separate Graphics objects then you need to do:

Graphics2D p1gfx = (Graphics2D)frogp1.create();
Graphics2D p2gfx = (Graphics2D)frogp1.create();

then when you finish with the Graphics object you use:

p1gfx.dispose();
p2gfx.dispose();

And I change the variable names to follow Java naming conventions. Don't use all upper case characters for variable names.

Upvotes: 0

Related Questions