Results 1 to 3 of 3
  1. #1
    handro1104 is offline Member
    Join Date
    Feb 2011
    Rep Power

    Default Can't display my graphics in a JPanel

    I have a problem displaying in a JPanel figures that I create with java.awt.Graphics. I want to display a work flow tree that I create in a dialog (the display panel is in a different dialog). I have a button in the dialog that creates the tree to open the display dialog, which contains the display panel in a scroll pane.
    Java Code:
    private void jDisplayButtonActionPerformed(java.awt.event.ActionEvent evt) {
    		workFlowDialog = new WorkFlowDialog(this, steps);
    I am passing a list of the work flow steps. The constructor of the display dialog is
    Java Code:
    	public WorkFlowDialog(JDialog parent,
    			ArrayList<ProcessingStep> processingSteps) {
    		super(parent, true);
    The method constructGraph constructs a list of GraphNode, nodesList; this is what I set in the display pane with "javaworkFlowPanel.setNodesList(nodesList);".
    The display panel, workFlowPanel, extends JPanel and overrides the method paintComponent.
    Java Code:
    	public void paintComponent(Graphics graphics) {
    		graphics.setColor(new Color(153, 255, 255));
    		for (GraphNode nextGraphNode : nodesList) {
    			int x = nextGraphNode.i * WorkFlowDialog.SEPARATION + WorkFlowDialog.DIAMETER / 2;
    			int y = nextGraphNode.j * WorkFlowDialog.SEPARATION + WorkFlowDialog.DIAMETER / 2;
    			graphics.drawOval(x, y, WorkFlowDialog.DIAMETER, WorkFlowDialog.DIAMETER);
    			graphics.fillOval(x, y, WorkFlowDialog.DIAMETER, WorkFlowDialog.DIAMETER);
    			graphics.drawString("X",x, y);
    When the constructor of WorkFlowDialog completes the dialog is displayed and it is using my paintComponent method (I can single step through it). However nothing is displayed.
    I am perplexed; I have used code like this in the past and it worked. If the repaint method is executing "graphics.drawOval(x, y, WorkFlowDialog.DIAMETER, WorkFlowDialog.DIAMETER);" why the window doesn't show anything?

    Your help will be greatly appreciated,

    Alejandro barrero

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Rep Power


    Add some println's to the paint component (or use a debugger) to make sure the values used in your painting method are values which paint where you would expect.

  3. #3
    handro1104 is offline Member
    Join Date
    Feb 2011
    Rep Power


    Thank you doWhile. You are absolutely correct; I was painting at negative positions. How do I set a thread to SOLVED in this forum?

Similar Threads

  1. Replies: 3
    Last Post: 01-12-2011, 10:43 PM
  2. Having difficulty adding graphics to a JPanel
    By DrKilljoy in forum New To Java
    Replies: 20
    Last Post: 07-20-2010, 08:40 PM
  3. Jpanel and displaying graphics
    By jdsflash in forum New To Java
    Replies: 6
    Last Post: 11-21-2009, 01:14 AM
  4. Clear Graphics Objects from Jpanel
    By DavidG24 in forum AWT / Swing
    Replies: 2
    Last Post: 05-20-2009, 09:34 PM
  5. Newbie need help on JPanel graphics
    By junpogi in forum AWT / Swing
    Replies: 7
    Last Post: 10-21-2008, 07:44 AM

Posting Permissions

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