Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2005

Tip: Looking for answers? Try searching our database.

accessing a JFrame child...

Thread view: 
emaiale@tiscali.it - 13 Jun 2005 19:34 GMT
Hi friends,
I can't modify an instance variable of  JPanel_zoom, a subclass of
JPanel I've created from the parent JFrame.

Here is the "interesting" code:

public class JPanel_Zoom extends JPanel implements MouseListener{

   public ArrayList lines = new ArrayList();
   public ArrayList selected = new ArrayList();
   public Rectangle2D.Double selectionRectangle = new
Rectangle2D.Double();
   public double zoom = 1.0d;
[snip]
   public void setZoom(double z){
       this.zoom = z;
       repaint();
   }

   public double getZoom(){
       return this.zoom;
   }
[snip]

and this is the code I'd like to use from the parent JFrame to modify
the "zoom" instance variable:

public class MainFrame extends javax.swing.JFrame implements
MouseListener{

    public JPanel_Zoom jpanel2 = new JPanel_Zoom();
[snip]

private void resizeForm(double zoom){
       jpanel2.setZoom(zoom);

   }
[...]

even if it gives no error or exceptions, it performs no change on
"zoom".

Any help would be appreciated.
Bill Tschumy - 13 Jun 2005 20:27 GMT
> Hi friends,
> I can't modify an instance variable of  JPanel_zoom, a subclass of
[quoted text clipped - 39 lines]
>
> Any help would be appreciated.

What have you tried doing to debug it?  I don't see anything overtly wrong
with the code presented.  

I would try either following it through with a debugger or putting a println
statement in setZoom() to print the new zoom and make sure it is being
called.  If it is being called then then you need to look at paint() method
to see why the new zoom is not being used.

Signature

Bill Tschumy
Otherwise -- Austin, TX
http://www.otherwise.com

emaiale - 13 Jun 2005 21:00 GMT
> What have you tried doing to debug it?  I don't see anything overtly wrong
> with the code presented.  
[quoted text clipped - 3 lines]
> called.  If it is being called then then you need to look at paint() method
> to see why the new zoom is not being used.

Dear Bill,
I've tried to put a println in setZoom(), and I'm sure it's called!

 public void setZoom(double z){
        System.out.println("setZoom entered");
        this.zoom = z;
        System.out.println("New zoom:"+ getZoom());
        repaint();
    }

It tells me that
1)the method has been used
2)zoom has been changed... apparently!

The "new" zoom will be used by the method paintComponent(), called by
repaint(), in JPanel_Zoom, which redraws the component according to the
new magnifing ratio:

public void paintComponent(Graphics g) {
        int w = this.getWidth();
        int h = this.getHeight();

        Graphics2D g2d = (Graphics2D)g;

    g2d.clearRect(0, 0, w, h);
        g2d.setColor(Color.blue);

        g2d.draw(selectionRectangle);
        g2d.setColor(Color.black);

        if (lines.size()!=0){
            int j = 0;
            double z = getZoom();
         // debug: zoom is still the same!!!
     System.out.println("Zoom: "+ z);
            for (Iterator i = lines.iterator(); i.hasNext(); ) {
                LineInt line = (LineInt)i.next();
                if (selected.contains(new Integer(j)))
                    g2d.setColor(Color.red);
                int x1 = (int)Math.round(line.px1*z);
                int y1 = (int)Math.round(line.py1*z);
                int x2 = (int)Math.round(line.px2*z);
                int y2 = (int)Math.round(line.py2*z);
                g2d.drawLine(x1, y1, x2, y2);
                g2d.setColor(Color.black);
                j++;
            }
        }
    }

I usually put a debug instruction in my code and, as you can see from
the comment... zoom is still the same!

Anyway, thanks for your help!
Fred L. Kleinschmidt - 13 Jun 2005 23:01 GMT
> > What have you tried doing to debug it?  I don't see anything overtly wrong
> > with the code presented.
[quoted text clipped - 58 lines]
>
> Anyway, thanks for your help!

Maybe the problem is in getZoom().
And why are you using getZoom() in the first place? Why not just use
"zoom"?
Signature

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94  (206)544-5225

Joly Makunga - 30 Jun 2005 11:31 GMT
hi man,

i'm working in a project that i have to simulate a zoom on a document
like a text file. so i noticed that you've created a zoom, i'm
interested on the running of your zoom code...

see you online
jolyqr


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.