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 / December 2005

Tip: Looking for answers? Try searching our database.

paintComponent question

Thread view: 
Huub - 01 Dec 2005 07:48 GMT
Hi,

I've been trying to pass coordinates to a class with paintComponent, but
so far I can't get it working. Unfortunately, I can't get the class
selfrunning. This is the code I have now:

/*
 * Grafisch.java
 *
 * Created on 18 October 2005, 19:36
 */

/**
 *
 * @author  huub
 */
import javax.swing.*;
import java.awt.*;

public class Grafisch extends JFrame
{

    /** Creates a new instance of Grafisch */
    public Grafisch(int x1, int y1, int x2, int y2)
    {
        System.out.println("grxx1= "+x1+" grxy1= "+y1+
                " grxx2= "+x2+" grxy2= "+y2);
        getContentPane().add(new Grafics(x1, y1, x2, y2));
    }

    public void grafisch_start(int x1, int y1, int x2, int y2)
    {
        Grafisch grafisch = new Grafisch(x1, y1, x2, y2);
        //System.out.println("grx1= "+x1+" gry1= "+y1+
        //" grx2= "+x2+" gry2= "+y2);
        this.pack();
        this.setVisible(true);
    }

    class Grafics extends JPanel
    {
        int x1, y1, x2, y2;
       
        public Grafics(int x1, int y1, int x2, int y2)
        {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            System.out.println("grgx1= "+x1+" grgy1= "+y1+
                    " grgx2= "+x2+" grgy2= "+y2);
        }
       
// how can I pass the coordinates from Grafics on to         paintComponent?

        public void paintComponent(Graphics g, int x1, int y1,
                int x2, int y2)
        {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.drawRect(x1, y1, 50, 50);
            System.out.println("x1 = " + x1 + " y1 = " + y1);
            g.setColor(Color.GREEN);
            g.drawRect(x2, y2, 50, 50);
            System.out.println("x2 = " + x2 + " y2 = " + y2);
            g.setColor(Color.BLUE);
            g.drawLine(x1, y1, x2, y2);
        }
    }
}

Thank you.

Huub
Lothar Leidner - 01 Dec 2005 09:43 GMT
>        
> // how can I pass the coordinates from Grafics on to        
> paintComponent?
>
>         public void paintComponent(Graphics g, int x1, int y1,
>                 int x2, int y2)

I would suggest to use private class variables:

private int x1, x2, y1, y2;

and then define

     public void paintComponent(Graphics g)

Regards,

Lothar Leidner
Huub - 01 Dec 2005 10:07 GMT
>>        // how can I pass the coordinates from Grafics on to        
>> paintComponent?
[quoted text clipped - 13 lines]
>
> Lothar Leidner

Can you explain some more? Like I (hopefully) said: my idea is to call
this class from other classes and pass the read-in variables to
paintComponent. If I only use "Graphics g" as an argument, I fail to see
 how I can pass the variables.
Michael Rauscher - 01 Dec 2005 10:59 GMT
Huub schrieb:

> Can you explain some more? Like I (hopefully) said: my idea is to call
> this class from other classes and pass the read-in variables to
> paintComponent. If I only use "Graphics g" as an argument, I fail to see
>  how I can pass the variables.

To me it's unclear what you'd like to do.

If you want to use Grafics like a stamp in order to render it on an
arbitrary graphics context (represented by an instance of class Graphics
), your code is ok. In this case, Grafics needs - in principle - not to
be a subclass of JComponent.

But if I look at your code, you'd rather like to use Grafics like an
ordinary swing component. In this case, Grafics must be a subclass of
JComponent.

Sloppy said: The toolkit calls Component#paint (the paint method of
class Component) whenever the component needs to be painted.

JComponent extends Component, introduces a method paintComponent and
overrides paint so that it calls paintComponent with the graphics
context as parameter.

If one simply adds some parameters to a method like you did, he
overloads the method. Overloading a method results in a method that is
absolutely different from the original method.

In your case: paintComponent(Graphics) and paintComponent(Graphics, int,
int, int, int) coexist. The toolkit only calls the first. You could have
named your method likeToDrawSomething and you'd get the same effect: no
output.

You see, there's no possibility to pass parameters to paintComponent in
a useful way.

But: your object stores the coordinates already. Use the stored
coordinates in paintComponent(Graphics) to paint the component. If you'd
like other objects be able to change the coordinates, the Grafics class
must provide a method for this.

So, change paintComponent the way, Lothat Leidner suggested and add a method

public void set( int x1, int y1, int x2, int y2 ) {
    this.x1=x1;
    this.x2=x2;
    this.y1=y1;
    this.y2=y2;
    repaint();
}

Of course, you can replace the this.x1... lines of the constructor by

set(x1,y1,x2,y2);

Bye
Michael
Huub - 01 Dec 2005 11:41 GMT
<snip>

> So, change paintComponent the way, Lothat Leidner suggested and add a
> method
[quoted text clipped - 13 lines]
> Bye
> Michael

Thank you for your explanation. Have some of it working now. The
remaining problem is that none of the passed coordinates make it to
paintComponent. The graphics all start at (0,0) instead of the given
coordinates. Can this be solved anyway?
Michael Rauscher - 01 Dec 2005 12:54 GMT
Huub schrieb:
> remaining problem is that none of the passed coordinates make it to
> paintComponent. The graphics all start at (0,0) instead of the given
> coordinates. Can this be solved anyway?

It would be better if you show us some code and tell us what you'd like
the code to do.

Bye
Michael
Huub - 01 Dec 2005 13:12 GMT
> It would be better if you show us some code and tell us what you'd like
> the code to do.
>
> Bye
> Michael

In my o.p. I posted the entire code of the class I'm talking about. The
class Graphics gets some variables passed on by/from another class which
reads them from keyboard. I want these variables to be passed on to
class Grafics method paintComponent. So far, I've been unable to achieve
that last step.
Lothar Leidner - 01 Dec 2005 13:47 GMT
> In my o.p. I posted the entire code of the class I'm talking about. The
> class Graphics gets some variables passed on by/from another class which
> reads them from keyboard. I want these variables to be passed on to
> class Grafics method paintComponent. So far, I've been unable to achieve
> that last step.

I assume you create an object of your Grafisch class in your parent
class, e.g.

Grafisch grafisch = new Grafisch();

Did you really call the public method set() mentioned by Michael
Rauscher in the parent class? e.g.

grafisch.set(100, 200, 150, 250);

Then the paintComponent method of the Grafisch class should work
correctly. As Michael said the code of your new implementation would be
helpful.

Regards,

Lothar Leidner
Michael Rauscher - 01 Dec 2005 13:58 GMT
Lothar Leidner schrieb:

>> In my o.p. I posted the entire code of the class I'm talking about.
>> The class Graphics gets some variables passed on by/from another class
[quoted text clipped - 11 lines]
>
> grafisch.set(100, 200, 150, 250);

You mean: grafics.set(100, 200, 150, 250);

Bye
Michael
Huub - 01 Dec 2005 14:37 GMT
>> I assume you create an object of your Grafisch class in your parent
>> class, e.g.
[quoted text clipped - 10 lines]
> Bye
> Michael

This is the code I have now. I have tried to add set() but that ends in
error.

/*
 * Grafisch.java
 *
 * Created on 18 October 2005, 19:36
 */

/**
 *
 * @author  huub
 */
import javax.swing.*;
import java.awt.*;

public class Grafisch extends JFrame
{

    /** Creates a new instance of Grafisch */
    public Grafisch(int x1, int y1, int x2, int y2)
    {
        System.out.println("grxx1= "+x1+" grxy1= "+y1+
                " grxx2= "+x2+" grxy2= "+y2);
        getContentPane().add(new Grafics());
    }

    public void grafisch_start(int x1, int y1, int x2, int y2)
    {
        Grafisch grafisch = new Grafisch(x1, y1, x2, y2);
/*        Grafics grafics = new Grafics();
        grafics.x1 = x1;
        grafics.y1 = y1;
        grafics.x2 = x2;
        grafics.y2 = y2;*/
        //System.out.println("grx1= "+x1+" gry1= "+y1+
        //" grx2= "+x2+" gry2= "+y2);
        this.pack();
        this.setVisible(true);
    }

    class Grafics extends JPanel
    {
        int x1, y1, x2, y2;
       
/*        public Grafics(int x1, int y1, int x2, int y2)
        {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            System.out.println("grgx1= "+x1+" grgy1= "+y1+
                    " grgx2= "+x2+" grgy2= "+y2);
            repaint();
        }*/
       
        public void set(int x1, int y1, int x2, int y2)
        {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            System.out.println("grgx1= "+x1+" grgy1= "+y1+
                    " grgx2= "+x2+" grgy2= "+y2);
            repaint();
        }
       
        public void paintComponent(Graphics g)
        {
            g.setColor(Color.RED);
            g.drawRect(x1, y1, 50, 50);
            System.out.println("x1 = " + x1 + " y1 = " + y1);
            g.setColor(Color.GREEN);
            g.drawRect(x2, y2, 50, 50);
            System.out.println("x2 = " + x2 + " y2 = " + y2);
            g.setColor(Color.BLUE);
            g.drawLine(x1, y1, x2, y2);
        }
    }
}
Michael Rauscher - 01 Dec 2005 15:10 GMT
Huub schrieb:

 >
> This is the code I have now. I have tried to add set() but that ends in
> error.

You have to learn to be a bit more precise. So *where* did you try to
add set? *When* do you want to set the coordinates?

For now, see my other posting and the changes I made below. To set the
coordinates use

Grafisch g = new Grafisch(...);
g.getGrafics().set( 100, 100, 200, 200 );

But better get rid of the Grafisch class. It's useless.

> import javax.swing.*;
> import java.awt.*;
>
> public class Grafisch extends JFrame
> {

      private Grafics gfx;

>     /** Creates a new instance of Grafisch */
>     public Grafisch(int x1, int y1, int x2, int y2)
>     {
>         System.out.println("grxx1= "+x1+" grxy1= "+y1+
>                 " grxx2= "+x2+" grxy2= "+y2);
          gfx = new Grafics();
          getContentPane().add(gfx);
>     }

      public Grafics getGrafics() {
          return gfx;
      }

>     class Grafics extends JPanel
>     {
[quoted text clipped - 25 lines]
>     }
> }

Bye
Michael
Huub - 01 Dec 2005 16:07 GMT
Thank you for your comments. After some heavy changes in code it ok now.
Have yet to make some slight modifications but it's working now like I
intended. Thank you both.
Vova Reznik - 01 Dec 2005 15:16 GMT
I don't understand what you try to do, but if you want to see to
rectangles and one line:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Grafisch extends JFrame {
    public Grafisch(int x1, int y1, int x2, int y2) {
        Grafics g = new Grafics();
        g.set(x1, y1, x2, y2);
        getContentPane().add(g);
        pack();
    }

    public static void main(String[] args) {
        final Grafisch f = new Grafisch(1, 1, 58, 58);
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                f.setVisible(true);
            }
        });
    }

    class Grafics extends JPanel {
        private int    x1, y1, x2, y2;
        public void set(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.drawRect(x1, y1, 50, 50);
            g.setColor(Color.GREEN);
            g.drawRect(x2, y2, 50, 50);
            g.setColor(Color.BLUE);
            g.drawLine(x1, y1, x2, y2);
        }
    }
}

>>> I assume you create an object of your Grafisch class in your parent
>>> class, e.g.
[quoted text clipped - 91 lines]
>     }
> }
Michael Rauscher - 01 Dec 2005 14:13 GMT
Huub schrieb:

>> It would be better if you show us some code and tell us what you'd
>> like the code to do.
[quoted text clipped - 7 lines]
> class Grafics method paintComponent. So far, I've been unable to achieve
> that last step.

It's clear to me that you want to pass the coordinates on to you Grafics
object. But it's unclear what you expect the result to be.

E.g. if you want the whole component to be at some arbitrary coordinates
in the parent container, the whole approach is totally wrong. If you
want the drawing to live at some arbitrary coordinates within the
component, the approach ist correct.

Ok, let me show you an example. In this example we want to have a
component that is able to draw a circle at some given location.
Additionally we want to have this component shown in a JFrame and we
want to let the user to set the location of the circle by clicking.

// File: Test.java
//
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;

public class Test {
    private static class CircleView extends JComponent {
        private Point coordinates = new Point(0,0);
        private Dimension preferredSize =
                new Dimension(600,400);

        public Dimension getPreferredSize() {
            return preferredSize;
        }

        public void set( Point p ) {
            coordinates.setLocation(p);
            repaint();
        }

        public void paintComponent( Graphics g ) {
            g.setColor( getForeground() );
            g.drawOval( coordinates.x-5, coordinates.y-5,
                        10, 10 );
        }
    }

    private CircleView view;

    public void createAndShowGUI() {
        view = new CircleView();
        view.addMouseListener( new MouseAdapter() {
            public void mouseClicked( MouseEvent e ) {
                view.set( e.getPoint() );
            }
        });

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        frame.getContentPane().add( view );
        frame.pack();
        frame.setVisible(true);
    }

    public static final void main( String args[] ) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                new Test().createAndShowGUI();
            }
        });
    }
}
//
// EOF: Test.java

Bye
Michael
   


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.