Java Help! paintComponent woes

Discussion in 'General Off-Topic Chat' started by jonthedit, Apr 19, 2014.

  1. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    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 !
     
  2. Ericthegreat

    Ericthegreat Not New Member

    Member
    1,798
    313
    Nov 8, 2008
    United States
    Vana'diel
    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.
     
  3. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
  4. grossaffe

    grossaffe GBAtemp Addict

    Member
    2,706
    2,086
    May 5, 2013
    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).
     
  5. matt123337

    matt123337 Advanced Member

    Newcomer
    62
    16
    Mar 25, 2014
    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.
     
  6. Foxi4

    Foxi4 On the hunt...

    pip Reporter
    23,528
    21,456
    Sep 13, 2009
    Poland
    Gaming Grotto
    Graphics2D P1GFX = (Graphics2D)frogp1;
    Graphics2D P2GFX = (Graphics2D)frogp1;

    You're rotating the same canvas, maybe? :)
     
  7. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    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.
     
  8. grossaffe

    grossaffe GBAtemp Addict

    Member
    2,706
    2,086
    May 5, 2013
    United States
    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);
     
  9. jonthedit
    OP

    jonthedit GBAtemp Advanced Maniac

    Member
    1,691
    438
    May 30, 2011
    Bangladesh
    I wish it worked like that. But thankfully someone solved it on stack overflow.
    I just need to use .create() and .dispose()