Results 1 to 6 of 6
- 10-21-2010, 02:14 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 6
- Rep Power
- 0
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>
- 10-21-2010, 02:29 PM #2
- 10-21-2010, 02:36 PM #3
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.
- 10-21-2010, 03:01 PM #4
Member
- Join Date
- Nov 2008
- Posts
- 6
- Rep Power
- 0
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.
- 10-21-2010, 03:35 PM #5
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
- 10-22-2010, 01:55 PM #6
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.
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.
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
-
paint in vector graphics
By koddy in forum New To JavaReplies: 7Last Post: 06-16-2010, 08:30 AM -
Why the paint() method is called two times ?
By supremo in forum New To JavaReplies: 4Last Post: 06-03-2010, 06:21 PM -
Two JPanels and paint(Graphics)
By ingfy in forum New To JavaReplies: 3Last Post: 04-27-2010, 08:51 PM -
how to add more than one paint method
By gautham in forum Java 2DReplies: 2Last Post: 04-06-2010, 07:07 AM -
print variable with paint(Graphics g) ??
By tghn2b in forum New To JavaReplies: 10Last Post: 12-29-2008, 12:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks