Results 1 to 6 of 6
  1. #1
    vsanandan is offline Member
    Join Date
    Nov 2008
    Posts
    6
    Rep Power
    0

    Default Paint(Graphics g) method is not called on its run.

    There is some statement missing in this program or some other error exists in the below program that I am not able to find. This program works fine first taking some other window to the front then bringing this window to front (basically refreshing). Is there any way to correct this problem?
    <Program>
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Polygon;
    import java.util.Vector;
    import javax.swing.JFrame;

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author root
    */
    public class DrawPolygon extends JFrame{

    Vector<Point> pointSeq = new Vector<Point>();
    public DrawPolygon()
    {
    super("Plotting polygon");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setSize(850,650);
    }

    public void paint(Graphics g)
    {
    Graphics2D g2d = (Graphics2D)g;
    addPointSequence();
    Polygon polygon = addToPolygon();
    g2d.draw(polygon);
    }

    public void addPointSequence()
    {
    Point p1 = new Point(0, 0, 800, 600);
    Point p2 = new Point(800, 0, 800, 600);
    Point p3 = new Point(800, 600, 800, 600);
    Point p4 = new Point(0, 600, 800, 600);
    pointSeq.add(p1);pointSeq.add(p2);
    pointSeq.add(p3);pointSeq.add(p4);
    }

    public Polygon addToPolygon()
    {
    Polygon polygon = new Polygon();
    for(int i = 0; i < pointSeq.size(); i++){
    Point p = pointSeq.elementAt(i);
    System.out.println("Points: x = " + p.normalize().getX() + ", y = " + p.normalize().getY());
    polygon.addPoint(p.normalize().getX(), p.normalize().getY());
    }
    return polygon;
    }

    public static void main(String[] args)
    {
    DrawPolygon poly = new DrawPolygon();
    poly.setVisible(true);
    }

    }

    class Point{
    int x, y;
    int max_x, max_y;

    public Point()
    {
    }

    public Point(int x, int y)
    {
    this.x = x;
    this.y = y;
    }

    public Point(int x, int y, int max_x, int max_y)
    {
    this.x = x;
    this.y = y;
    this.max_x = max_x;
    this.max_y = max_y;
    }

    public int getX() {
    return x;
    }

    public void setX(int x) {
    this.x = x;
    }

    public int getY() {
    return y;
    }

    public void setY(int y) {
    this.y = y;
    }

    public int getMax_x() {
    return max_x;
    }

    public void setMax_x(int max_x) {
    this.max_x = max_x;
    }

    public int getMax_y() {
    return max_y;
    }

    public void setMax_y(int max_y) {
    this.max_y = max_y;
    }

    public Point normalize()
    {
    Point norm = new Point();
    norm.x = max_x - x + 25;
    norm.y = y + 25;
    return norm;
    }
    }

    </Program>

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    How do you know it's not being called? How do you know the problem isn't elsewhere? Put some print statements in there. Is there a reason you aren't calling the super class's paint method also? You should probably extend a JPanel (or a JComponent) and override paintComponent instead.

    When post code, make sure you use the code tags to preserve formatting.

  4. #4
    vsanandan is offline Member
    Join Date
    Nov 2008
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the replies.
    I'm saying the paint(Graphics g) method is not called, because it is not rendering the polygon on the JFrame window output. I have added println statements to print the (x,y) co-ordinates. When I call the super.paint(g); or super.paing(g2d); even my second attempt fails to render the polygon.

    I hope to resolve the problem using another JPanel or JApplet and adding to the JFrame contentPane using getContentPane() method. I have seen other 2d programs not using such methods and I wanted to know what is going wrong in this program, I have posted this.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Kevin gave you a link to a tutorial. Go through it.

    Custom painting should not be attempted in a top-level window like JFrame / JDialog. Also, painting methods are for painting and painting alone. It looks like you have some business logic in there.

    Repeat after me, slowly and clearly:
    "I have no control over

    how many times

    and how often

    a painting method executes."

    db

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Quote Originally Posted by vsanandan View Post
    Thanks for the replies.
    I'm saying the paint(Graphics g) method is not called, because it is not rendering the polygon on the JFrame window output.
    And I'm saying how do you know that that's why the Polygon isn't rendering? How do you know the problem isn't in one of your methods that computes the Polygon? That's why you should put some print statements in the method itself, instead of wrapped up in your logic that might be the problem in the first place. But, like you've been told, and like the tutorials show, you shouldn't be using that method anyway.

    Quote Originally Posted by vsanandan View Post
    I have added println statements to print the (x,y) co-ordinates. When I call the super.paint(g); or super.paing(g2d); even my second attempt fails to render the polygon.
    And do those print statements print anything? What do they print? Why do you think calling super's paint method do that? Where exactly are you calling it? But, again, don't use that method.

    Quote Originally Posted by vsanandan View Post
    I hope to resolve the problem using another JPanel or JApplet and adding to the JFrame contentPane using getContentPane() method. I have seen other 2d programs not using such methods and I wanted to know what is going wrong in this program, I have posted this.
    One of the things that's wrong is you're using JFrame's paint method instead of JPanel's paintComponent method. Fix that first, then we'll get to the other problems.

Similar Threads

  1. paint in vector graphics
    By koddy in forum New To Java
    Replies: 7
    Last Post: 06-16-2010, 08:30 AM
  2. Why the paint() method is called two times ?
    By supremo in forum New To Java
    Replies: 4
    Last Post: 06-03-2010, 06:21 PM
  3. Two JPanels and paint(Graphics)
    By ingfy in forum New To Java
    Replies: 3
    Last Post: 04-27-2010, 08:51 PM
  4. how to add more than one paint method
    By gautham in forum Java 2D
    Replies: 2
    Last Post: 04-06-2010, 07:07 AM
  5. print variable with paint(Graphics g) ??
    By tghn2b in forum New To Java
    Replies: 10
    Last Post: 12-29-2008, 12:11 PM

Posting Permissions

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