Java Help! paintComponent woes

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
Hi! 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.
Code:
    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!
Code:
    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.
Code:
    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 !
 

Ericthegreat

Not New Member
Member
Joined
Nov 8, 2008
Messages
3,455
Trophies
2
Location
Vana'diel
XP
4,279
Country
United States
Hi! I am new to Java and am learning new things everyday.
I am trying to learn how to take a .png and rotate it using the paint component.
Should work... But I've never tryed to do this, anyway try asking somewhere like stack overflow, or a java related forum, you'll get a lot more help.
 

grossaffe

Well-Known Member
Member
Joined
May 5, 2013
Messages
3,007
Trophies
0
XP
2,799
Country
United States
With the first implementation, I think that P1GFX and P2GFX are both pointers to the same piece of data, so whatever happens to one happens to the other (I'm not a fan of how Java tries to hide pointers through abstraction, only makes it harder to figure out what's going on behind the scenes).
 

matt123337

Well-Known Member
Member
Joined
Mar 25, 2014
Messages
151
Trophies
0
XP
623
Country
Canada
Perhaps it's because P1GFX and P2GFX are the exact same object, as such any rotate method call on them effects both?
Try:
Code:
    public void paintComponent(Graphics gfx) {
                 Graphics2D g = (Graphics2D)gfx;
                g.rotate(90, 150 / 2, 150 / 2); // first rotate
                g.drawImage(p1.getImage1(), p1x, p1y,this);
                g.rotate(40, 50, 50); // second rotate
                g.drawImage(p2.getImage2(), p2x, p2y, this);
         
           
    }

This is rather basic Java, so I assume that you're rather new to it.
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
With the first implementation, I think that P1GFX and P2GFX are both pointers to the same piece of data, so whatever happens to one happens to the other (I'm not a fan of how Java tries to hide pointers through abstraction, only makes it harder to figure out what's going on behind the scenes).
Perhaps it's because P1GFX and P2GFX are the exact same object, as such any rotate method call on them effects both?
Try:
Code:
    public void paintComponent(Graphics gfx) {
                 Graphics2D g = (Graphics2D)gfx;
                g.rotate(90, 150 / 2, 150 / 2); // first rotate
                g.drawImage(p1.getImage1(), p1x, p1y,this);
                g.rotate(40, 50, 50); // second rotate
                g.drawImage(p2.getImage2(), p2x, p2y, this);
         
           
    }

This is rather basic Java, so I assume that you're rather new to it.
Graphics2D P1GFX = (Graphics2D)frogp1;
Graphics2D P2GFX = (Graphics2D)frogp1;

You're rotating the same canvas, maybe? :)

I thought I am making new variable which inherits the properties of frogp1??
And if I am not I still can not get the other parts to work.

Edit: Someone on stack overflow answered the question for me!

.create() will allow me to create multiple instances of the Graphics frogp1!!
I just have to .dispose() when I am finished with it.
 

grossaffe

Well-Known Member
Member
Joined
May 5, 2013
Messages
3,007
Trophies
0
XP
2,799
Country
United States
I thought I am making new variable which inherits the properties of frogp1??
And if I am not I still can not get the other parts to work.
I don't know the constructor(s) for Graphics2D, but I think what you're trying to do is something more along the lines of this:
Code:
Graphics2D P1GFX = new Graphics2D(frogp1);
Graphics2D P2GFX = new Graphics2D(frogp2);
 

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,009
Country
Bangladesh
I don't know the constructor(s) for Graphics2D, but I think what you're trying to do is something more along the lines of this:
Code:
Graphics2D P1GFX = new Graphics2D(frogp1);
Graphics2D P2GFX = new Graphics2D(frogp2);
I wish it worked like that. But thankfully someone solved it on stack overflow.
I just need to use .create() and .dispose()
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    K3Nv2 @ K3Nv2: Least they got head in the end