5 Replies - 235 Views - Last Post: 23 February 2012 - 07:37 PM Rate Topic: -----

#1 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 437
  • Joined: 03-January 11

Rectangle won't update(Graphics)

Posted 23 February 2012 - 02:55 PM

Hi, I have this problem where I have a box created and want to update the color after it is created but it stays black.

Here is the code:
       private static int ID;
       private static String user;
       private static int level;
       private static  int exp;
       private static  int r;
       private static int gr;
       private static int b;


    public void paintComponent(Graphics g){
         super.paintComponent(g);
        Color clr = new Color(r,gr,B)/>;
      
        g.fillRect(50, 50, 100, 100);   
        g.setColor(clr);
     super.repaint();
    }

    public static void setValues(String username,int ID,int level,int exp,int r,int g,int B)/>{
        createChar.user = username;
        createChar.ID = ID;
        createChar.level = level;
        createChar.exp = exp;
        createChar.r = r;
        createChar.gr = g;
        createChar.b = b;
        System.out.println(username +" " + ID + " " + level + " " + exp + " " + r + " " + g +" " + " " + b );
        System.out.println(createChar.user + createChar.ID + createChar.level+ createChar.exp + createChar.r + createChar.gr + createChar.B)/>;
    }



when I run it it just stays black, any help ?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Rectangle won't update(Graphics)

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Rectangle won't update(Graphics)

Posted 23 February 2012 - 03:05 PM

The setValues() is not related to GUI part, so you will need to repaint after setting new values for your variables
Was This Post Helpful? 1
  • +
  • -

#3 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 437
  • Joined: 03-January 11

Re: Rectangle won't update(Graphics)

Posted 23 February 2012 - 03:09 PM

View Postsmohd, on 23 February 2012 - 03:05 PM, said:

The setValues() is not related to GUI part, so you will need to repaint after setting new values for your variables

like this:
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    System.out.println(username + ID + level + exp + r +g +B)/>;
    jPanel1.repaint();
    } 


Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 996
  • Posts: 2,211
  • Joined: 05-April 11

Re: Rectangle won't update(Graphics)

Posted 23 February 2012 - 03:18 PM

Why are you calling super.repaint() from your paintComponent method?
Was This Post Helpful? 2
  • +
  • -

#5 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8030
  • View blog
  • Posts: 31,176
  • Joined: 06-March 08

Re: Rectangle won't update(Graphics)

Posted 23 February 2012 - 06:10 PM

View Postcornetto456, on 23 February 2012 - 05:09 PM, said:

View Postsmohd, on 23 February 2012 - 03:05 PM, said:

The setValues() is not related to GUI part, so you will need to repaint after setting new values for your variables

like this:
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
    System.out.println(username + ID + level + exp + r +g +B)/>;
    jPanel1.repaint();
    } 


What will paint() or paintComponent() have to paint differently ?
Your jButton1MouseClicked() will have to change instance variables that paint()/paintComponet() will use to draw a different shapes/states

View PostCasiOo, on 23 February 2012 - 05:18 PM, said:

Why are you calling super.repaint() from your paintComponent method?

Ya looks like an infinie loop to me :^: CasiOo
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9039
  • View blog
  • Posts: 33,531
  • Joined: 27-December 08

Re: Rectangle won't update(Graphics)

Posted 23 February 2012 - 07:37 PM

Looks to me like use of the NetBeans GUI Builder. Please- write your own GUI by hand. Don't rely on the horrid code generated by the GUI Builder. Also, static variables are not good OO practice.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1