Results 1 to 9 of 9
  1. #1
    mitttig is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default bar chart painting problem

    I am doing a project but i encountered a problem i need some help with it.

    The code i made is supposed to draw a bar chart. But somehow it doesn't paint something.
    I have been looking on it for a while now but i don't get it.

    Java Code:
        public void graphicSD(String reportName)
        {
            try
            {
                graphicFrame = new JFrame();
    
                graphicFrame.setTitle(reportName);
    
                graphicFrame.setSize(800, 600);
                graphicFrame.setLayout(new FlowLayout());
    
                Object[][] data = CM.getReportQueryData(reportName);
                System.out.println(data);
    
                BarGraph barGraph = new BarGraph(data);
                JPanel barGraphContainer = new JPanel();
                barGraphContainer.setSize(300,300);
                barGraphContainer.add(barGraph);
    
                graphicCancel = new JButton("Annuleren");
                graphicFrame.add(graphicCancel);
                graphicCancel.addActionListener(this);
    
                JPanel buttonPanel = new JPanel();
                buttonPanel.add(graphicCancel);
    
                graphicFrame.add(barGraphContainer);
                graphicFrame.add(buttonPanel);
                graphicFrame.setVisible(true);
            }
            catch (ConnectException ex)
            {
                JOptionPane.showMessageDialog(this,
                "Server is not available at this time.",
                "Inane error",
                JOptionPane.ERROR_MESSAGE);
            }
        }
    
    
       
        }
    }

    this part makes a nice begin screen with a button(ShowStaafDiagram) and some stuff more that is a bit hard to describe here.
    When you click the button(ShowStaafDiagram) it goes through the ActionListener and after that to the method GraphicSD. When in graphicSD it makes another screen with a cancel button and there goes the bar chart code.

    Java Code:
    public class BarGraph extends JPanel
    {
      private Object[][] data;
    
        public BarGraph(Object[][] data)
      {
        this.data = data;
    
      }
    
      @Override
      public void paintComponent(Graphics g)
      {
        super.paintComponent(g);
    
    //from here on it's mostly getting the scaling for bar chart
    
        if (data == null || data.length == 0)
          return;
        double minValue = 0;
        double maxValue = 0;
        System.out.println(minValue);
        for (int i = 0; i < data.length; i++)
        {
          if (minValue > ((BigDecimal)data[i][1]).doubleValue())
              System.out.println(data[i][1]);
           minValue = ((BigDecimal)data[i][1]).doubleValue();
          if (maxValue < ((BigDecimal)data[i][1]).doubleValue())
              System.out.println(data[i][1]);
            maxValue = ((BigDecimal)data[i][1]).doubleValue();
        }
    
        Dimension d = getSize();
        int clientWidth = d.width;
        int clientHeight = d.height;
        int barWidth = clientWidth / data.length;
    
        Font titleFont = new Font("SansSerif", Font.BOLD, 20);
        FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
        Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
        FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);
    
        int titleWidth = titleFontMetrics.stringWidth(title);
        int y = titleFontMetrics.getAscent();
        int x = (clientWidth - titleWidth) / 2;
        g.setFont(titleFont);
    
    
        int top = titleFontMetrics.getHeight();
        int bottom = labelFontMetrics.getHeight();
        if (maxValue == minValue)
          return;
        double scale = (clientHeight - top - bottom) / (maxValue - minValue);
        y = clientHeight - labelFontMetrics.getDescent();
        g.setFont(labelFont);
    
       //It's actually getting painted here. So i guess here is the main problem
        for (int i = 0; i < data.length; i++)
        {
          int valueX = i * barWidth + 1;
          int valueY = y;
          int height = (int) (((BigDecimal)data[i][1]).doubleValue() * scale);
          if (((BigDecimal)data[i][1]).doubleValue() >= 0)
            valueY += (int) ((maxValue - ((BigDecimal)data[i][1]).doubleValue()) * scale);
          else
          {
            valueY += (int) (maxValue * scale);
            height = -height;
          }
    
          g.setColor(Color.green);
          g.drawRect(valueX, valueY, barWidth - 2, height);
          g.setColor(Color.black);
          g.fillRect(valueX, valueY, barWidth - 2, height);
          int labelWidth = labelFontMetrics.stringWidth((String)data[i][0]);
          x = i * barWidth + (barWidth - labelWidth) / 2;
          g.drawString((String)data[i][1], x, y);
        }
      }
    
    }
    http://imageshack.us/photo/my-images/543/screen2ypi.jpg

    now i don't know where it goes wrong? any ideas here from the people who know how to program?
    Last edited by mitttig; 06-23-2011 at 04:39 PM. Reason: to much code

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

    Default

    That's almost definitely way too much code for anybody to wade through. We have hundreds of posts here, so we don't have time to do a ton of debugging for you. If you boil your problem down to an SSCCE that demonstrates the problem in as few lines as possible, we can go from there.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Are the methods being called as you expect? Are the values correct when the methods are called?

    Add some printlns to show that the methods are being called and that the values are correct.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Are the methods being called as you expect? Are the values correct when the methods are called?

    Add some printlns to show that the methods are being called and that the values are correct.

  5. #5
    mitttig is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    i'm trying to debug as much as possible. In the second part aren't any println's coming out. If i'm thinking out loudly my guess is something with the container(BarGraphContainer first part). I do not completely understand how it works.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    In the second part aren't any println's coming out.
    If there are no printlns then the code isn't being executed. Can you back track and find out why?

    For example your posted code does not do a println to show if the paintComponent method is called.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    When you do custom painting you are responsible for overriding the getPreferredSize() method to return the size of your panel so the layout manager can layout your panel properly. Add a LineBorder to your panel and I'm sure you will just see a small square indicating that even if the painting is done there will be nothing to display.

  8. #8
    mitttig is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    When you do custom painting you are responsible for overriding the getPreferredSize() method to return the size of your panel so the layout manager can layout your panel properly. Add a LineBorder to your panel and I'm sure you will just see a small square indicating that even if the painting is done there will be nothing to display.
    You are indeed right about the small square. but what i don't get is how to fix it?
    I'm remembering that i saw some chinese looking letters in that spot.

    i have an override just before the paintComponent
    Last edited by mitttig; 06-23-2011 at 07:29 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    how to fix it?
    Add the following method to the panel class
    Java Code:
        
        @Override
        public Dimension getPreferredSize() {    //<<<<<<<<<<<<<< NEED THIS
          return ourSize;
        }
    Define a Dimension ourSize to desired size.
    Last edited by Norm; 06-23-2011 at 07:37 PM.

Similar Threads

  1. Problem painting on JPanel
    By Boatski in forum AWT / Swing
    Replies: 0
    Last Post: 12-01-2010, 04:36 AM
  2. Painting Problem!
    By Jcbconway in forum Advanced Java
    Replies: 3
    Last Post: 11-17-2010, 04:14 AM
  3. Problem with JApplet painting
    By Fuzzier in forum Java Applets
    Replies: 5
    Last Post: 07-29-2010, 08:54 PM
  4. painting problem
    By hannes in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 11:44 AM
  5. Jpanel painting problem
    By kcakir in forum AWT / Swing
    Replies: 3
    Last Post: 04-15-2009, 10:21 PM

Tags for this Thread

Posting Permissions

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