CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Nov 2003
    Posts
    21

    changing an int across a class :S

    Hi there, im having real problems with something in my application, as ive never done this before. Ive tried looking for examples but cant find any really. Searched the API, also with no luck.

    Anyway, heres the problem.
    Ive got 2 classes, base and gui

    In base, at the moment ive got int size = 2;

    this effects all the other sizes within the base class.

    In the gui, via a combo box, ive got base.size = 8;

    you should be able to see what im trying to do. base is just a panel, with code in, which is added to the mainPanel of the gui class..

    Ive tried removing the panel, setting base.size = 8; and then re-adding, ive tried repaint() ive tried pack..

    Nothing seems to work, any ideas as im completely stuck

    Thanks

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    does the panel get it's preferred width from the base class? then pack itself?

    im not entirely sure why you would design things this way, as the width is an attribute of the gui panel, rather than the base class, so it should only be present in the gui class as an attribute.. the baseclass may call a set method and set the width, and then invoke a resizing operation
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Nov 2003
    Posts
    21
    well, the int im changing is not for the width, its actually for creating a shape on a panel using paint.. as i said, ive tried repaint()
    the baseclass may call a set method and set the width, and then invoke a resizing operation
    how would i go about it, cant work out how

    Cheers

  4. #4
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    I'm not sure actically sure what your asking here, can you post some code that might clear things up?

  5. #5
    Join Date
    Nov 2003
    Posts
    21
    Well lets take the base class:
    at the moment ive got:

    public static int size= 2;

    then later i have int size = (200 / size);

    i also have lots of other things depending on that size..


    Now, in the gui class ive got:

    private void sizeJComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
    (other code...............)

    then

    if (sizeJComboBox.getSelectedIndex()==3 ) {
    JLabel.setText("size = 8");
    base.size = 8;
    }

    the idea is it changes the int of that other class, then re-does that class.
    What i mean by this is, i begin by loading the gui class, this adds a Jpanel to my gui, this jPanel = the base class which contains all the code. Ive tried removing the panel, changing the int then re adding, but no luck. Ive tried repaint(), pack(), initComponents() again... and lots of other things. What i think i really need is a way to reload that class(base - the jPanel) onto the gui but in its changed state, i.e depending what the user chooses/the value int size is set to..

    Hope this clarifies a bit more, all this help is apreciated

    Cheers

  6. #6
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    I'm confused.

    So base is a class, gui inherites from base? But in gui you have an Object of base, and you want to change the size member variable for all instances of class base?

    I'm probably missing something here, but if all you want to do is change the size in one spot, but have it effect all instances of the class, make size static.


  7. #7
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    the idea is it changes the int of that other class, then re-does that class.
    What i mean by this is, i begin by loading the gui class, this adds a Jpanel to my gui, this jPanel = the base class which contains all the code. Ive tried removing the panel, changing the int then re adding, but no luck. Ive tried repaint(), pack(), initComponents() again... and lots of other things. What i think i really need is a way to reload that class(base - the jPanel) onto the gui but in its changed state, i.e depending what the user chooses/the value int size is set to..
    This sounds like a layout manager issue rather than a member variable issue
    well, the int im changing is not for the width, its actually for creating a shape on a panel using paint.. as i said, ive tried repaint()
    So are you trying to draw a shape with the Graphics class?

    ie
    Code:
    public void paint(Graphics g)   {
       g.drawOval(25, 25, 30, 40);
    }
    // ...or maybe
    public void paintComponent(Graphics g)   {
       g.drawOval(25, 25, 30, 40);
    }
    if so, try filling in a rectangle before redrawing the shape.

    ie
    Code:
    g.fillRect(0, 0, this.getWidth, this.getHeight);
    g.drawOval(25, 25, 30, 40);
    If I'm still missing something, I appoligise it's been a long day !!!
    Last edited by Goodz13; November 18th, 2003 at 09:52 PM.

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    a quick guide to making a simple post:

    "hi. i'm making a simple Paint program. I have an integer, called SIZE that defines the size of my painted shape, but when i change the int, using a combobox, it does not change the size of the shape on the canvas. How do i do this?"

    now, that might not be what you are asking, but it's a heck of a lot clearer than what you've written.. from it we know what is being attempted (make a paint program), how youre trying to do it, and where you are getting stuck.. its a lot easier to offer help in this situation.

    can you re-write your first post using some of these ideas?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    Join Date
    Nov 2003
    Posts
    21
    ok ill try and explain a little better. I have 2 classes(base and gui) base contains code which paints shapes onto its jPanel. The shape depends on a value which is currently set to: public static int size= 2;

    the gui class contains a jPanel, in which i add the base class too.
    So when the gui class loads, the base class is added straight away set by that integer. So now i need to be able to change that int from the gui class - combo box and reload and redesign the base class depending on that value.

    Hope this is clearer, i certainly understood it better myself this time

  10. #10
    Join Date
    Apr 2003
    Location
    Israel
    Posts
    398
    what is the problem ?
    you create a method that gets an int value and paints the shape again :

    Code:
    public void repaint(int x){
      size=x;
      paintMethod();
    }
    
    
    and you shouldnt make the size variable public as it is not something for everyone to use and see at will .
    Vision.

  11. #11
    Join Date
    Nov 2003
    Posts
    21
    its not just painting the shape again though as i said.. that int size effects that whole class, how things are drawn, how things are calculated, how things are added etc etc, so i cant just do that..

  12. #12
    Join Date
    Apr 2003
    Location
    Israel
    Posts
    398
    so the only way to accomplish it ,is by making this class dynamical enough for it to be completley changed by the change of that variable.

    the simple way for you (although you can use active rendering) is
    to close the panel and open it again giving the number to the constructor,but Swing supports active rendering using double buffering,if you want to do it proffesionally do it that way.
    Vision.

  13. #13
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    tbh, i still dont actually have a clear idea of what youre trying to do though.. im having trouble imagining why you would need to write software that behaves like this.. ? what does your program actually do, and what specific problem are you trying to achieve by your surrent method?

    your use of static variables indicates to me, that there might be a problem with this design, in addition to the fact that both "base" and "gui", have a 'gui'
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  14. #14
    Join Date
    Nov 2003
    Posts
    21
    im going to create a small applet or program example, only a few lines of code, will post once complete

  15. #15
    Join Date
    Nov 2003
    Posts
    21
    Easier said than done, trying to make an example code turned out to be virtually the same size as my main code.. I doubt you would want to look at all that, so ive tried to create an example picture. By the way, thanks for being patient, i know what i want to do, but explaining it to others is not so easy. Image

    Hope someone can make sense of this more than i can. The base class has an int set to 5, public static int size = 5;
    from the gui class - combo box, i wish to change this value (base.size = 6 The base class works depending on that int size value, the idea is, from the gui class, change the int value, reload the base class so its layed out differently..
    As i say, thanks for putting up with me, and sorry for all these crappy explanations.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured