Swing painting problems

Discussion in 'Java' started by Evan, Mar 8, 2008.

  1. Evan

    Evan Guest

    So, I'm writing a swing program that displays a graph on a JPanel (I
    have created a custom "Canvas" class that extends JPanel) inside a
    larger gui.

    My Canvas class creates a VertexImage object, adds the VertexImage
    object to itself. Then, since it knows about each of the vertices, it
    draws the lines between them. The Vertices (class VertexImage extends
    JComponent) paint themselves on the Canvas.

    So, while I am having no problems painting the connections between the
    vertices, I am having PLENTY of problems painting the VertexImages. It
    shouldn't be too hard - but I'm obviously messing up somewhere. Here
    are are my painting methods.

    in my Canvas Class:

    public void paintComponent(Graphics g) {

    Component[] components = getComponents();

    // Cycle through each component
    for (int i=0; i < components.length; i++){
    VertexImage vertex = (VertexImage) components;
    Point coordinates = vertex.getCoordinates();

    HashSet <Vertex> edges = vertex.getEdges();

    Iterator <Vertex> it = edges.iterator();

    // Draws all the edges to neighbor vertices
    while (it.hasNext()) {
    Point toCoordinates =
    it.next().getInitCoordinates();
    g.drawLine(coordinates.x, coordinates.y,
    toCoordinates.x, toCoordinates.y);
    }
    }
    }


    In my VertexImage class:

    public void paintComponent(Graphics g) {
    g.setColor(Color.blue);

    g.fillOval(coordinates.x,
    coordinates.y,
    imageWidth, imageHeight);

    g.drawOval(coordinates.x,
    coordinates.y,
    imageWidth, imageHeight);
    }
     
    Evan, Mar 8, 2008
    #1
    1. Advertising

  2. Evan

    Peter Duniho Guest

    On Sat, 08 Mar 2008 13:23:51 -0800, Evan <> wrote:

    > [...]
    > So, while I am having no problems painting the connections between the
    > vertices, I am having PLENTY of problems painting the VertexImages. It
    > shouldn't be too hard - but I'm obviously messing up somewhere.


    Well, without a complete sample it's pretty much impossible to know for
    sure what might be wrong.

    However, are you sure that your VertexImage instances have non-zero width
    and height? If they don't, the paintComponent() method for that class
    might never even be called.

    I'm assuming that at least the locations of the instances are correct,
    since you say the lines get drawn correctly. But I'm curious what layout
    manager you're using to allow that.

    You can use a debugger or the console output to trace what methods are
    actually getting called, which should help you at least understand whether
    it's a problem getting called or a problem in how you're actually
    drawing. Beyond that, if you really expect your question to be answered,
    you should post a concise-but-complete sample of code that reliably
    demonstrates the problem.

    Another thing to try is separate your custom JPanel from your custom
    JComponents. Try adding built-in JComponents to your JPanel sub-class and
    see if those work. Likewise, try adding your custom JComponent sub-class
    to a plain JPanel and see if that works. Hopefully at least one or the
    other will, and then you can focus on getting the other to work. Of
    course, if neither works then you have more than one problem to solve. :)

    Finally, there are a number of things I question in your choice of
    design. The most significant is your decision to make the vertices
    individual JComponents. Unless there's really something about them being
    JComponents that you need in your implementation (and being able to draw
    wouldn't qualify for that...you can draw data with or without it being an
    actual JComponent), it seems to me you're better off just drawing the
    vertices in your "Canvas" class. Otherwise you're unnecessarily limiting
    the ability of your code to scale well as the number of vertices goes up
    and creating additional overhead in any case.

    I also wonder why you're apparently drawing every connecting line twice.
    That seems inefficient. Or are all of the links between vertices one-way?

    You also have made the assumption that all of the Components inside your
    Canvas class are VertexImage instances, which seems unnecessarily
    dangerous (you could check the type before casting). It also seems odd to
    make the paintComponent() method public. Shouldn't you stick with
    protected, as in the base implementation?

    Anyway, the basic issue is that there's nothing fundamentally impossible
    about what you're describing. And generally speaking, you can add child
    components to a custom JPanel sub-class without any trouble, including
    custom JComponent sub-classes. So, showing the paint methods isn't really
    going to help. That doesn't tell us anything about the things that might
    cause problems with getting your custom JComponent sub-class instances to
    show up.

    Pete
     
    Peter Duniho, Mar 8, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael C. Gilson
    Replies:
    4
    Views:
    987
    Ken DeLong
    Aug 31, 2003
  2. Jesse Pavel

    Swing Menu Painting, 2D Demo

    Jesse Pavel, Feb 15, 2004, in forum: Java
    Replies:
    2
    Views:
    829
    Jesse Pavel
    Feb 18, 2004
  3. matt

    Swing Painting Problems

    matt, Mar 28, 2007, in forum: Java
    Replies:
    2
    Views:
    372
  4. Kgbol
    Replies:
    4
    Views:
    1,176
  5. Replies:
    10
    Views:
    8,600
    Roedy Green
    Apr 11, 2009
Loading...

Share This Page