Results 1 to 9 of 9
Thread: bar chart painting problem
- 06-23-2011, 02:23 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
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.
http://imageshack.us/photo/my-images/543/screen2ypi.jpgJava 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); } } }
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
- 06-23-2011, 03:41 PM #2
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!
- 06-23-2011, 04:30 PM #3
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.
- 06-23-2011, 04:31 PM #4
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.
- 06-23-2011, 05:06 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
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.
- 06-23-2011, 05:11 PM #6
If there are no printlns then the code isn't being executed. Can you back track and find out why?In the second part aren't any println's coming out.
For example your posted code does not do a println to show if the paintComponent method is called.
- 06-23-2011, 05:50 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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.
- 06-23-2011, 07:18 PM #8
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
- 06-23-2011, 07:28 PM #9
Similar Threads
-
Problem painting on JPanel
By Boatski in forum AWT / SwingReplies: 0Last Post: 12-01-2010, 04:36 AM -
Painting Problem!
By Jcbconway in forum Advanced JavaReplies: 3Last Post: 11-17-2010, 04:14 AM -
Problem with JApplet painting
By Fuzzier in forum Java AppletsReplies: 5Last Post: 07-29-2010, 08:54 PM -
painting problem
By hannes in forum New To JavaReplies: 3Last Post: 01-17-2010, 11:44 AM -
Jpanel painting problem
By kcakir in forum AWT / SwingReplies: 3Last Post: 04-15-2009, 10:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks