It works all fine. Except, as the title mentioned, when i start my program the frame is blank for about 1 second. Then it shows the graph.
Okay, my paintComponent has 2 for-loops in it, so it should take a bit longer than the average paintComponent.
But even if it's 2x a for-loop of 10 times, it's still 1 second.
Besides, when I resize my frame (to trigger the repaint) it all goes at a normal speed. So I doubt that is the issue.
It's just at the startup of the frame that the frame is lagging for a second.
Any ideas? :confused:
All the code:
Simple Gui class, just create a BarGraph object, add it to frame, set frame's size and set visible.
package test;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
/**
* User: Wim
* Date: 11/01/11
* Time: 18:46
*/
public class Gui extends JFrame {
public Gui(){
super();
BarGraph barGraph = getFirstGraph();
//BarGraph barGraph = getSecondGraph();
add(barGraph);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,800);
setVisible(true);
}
private BarGraph getFirstGraph() {
List<Integer> xValues = new ArrayList<Integer>();
List<String> yValues = new ArrayList<String>();
xValues.add(40);
xValues.add(100);
xValues.add(10);
xValues.add(20);
xValues.add(75);
xValues.add(10);
xValues.add(20);
xValues.add(30);
xValues.add(40);
xValues.add(50);
xValues.add(60);
xValues.add(70);
xValues.add(80);
xValues.add(90);
xValues.add(100);
yValues.add("40");
yValues.add("100");
yValues.add("10");
yValues.add("20");
yValues.add("75");
yValues.add("10");
yValues.add("VeryVeryLongWord");
yValues.add("30");
yValues.add("40");
yValues.add("50");
yValues.add("OmgAnotherLongWord");
yValues.add("70");
yValues.add("80");
yValues.add("90");
yValues.add("100");
return new BarGraph(xValues, yValues, 5, 100);
}
public BarGraph getSecondGraph() {
List<Integer> xValues = new ArrayList<Integer>();
for(int i=0 ; i<51 ; i++){
xValues.add(i);
}
return new BarGraph(xValues);
}
public static void main(String[] args) {
Gui gui = new Gui();
}
}
You can choose between a graph with 10 and 50 bars, by commenting / uncommenting getFirstGraph() or getSecondGraph() in the constructor.
BarGraph class:
package test;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
/**
* User: Wim
* Date: 11/01/11
* Time: 18:46
*/
public class BarGraph extends JPanel {
private List<Integer> xValues;
private List<String> yValues;
private int hGap;
private int xScaleMax;
private int xScaleStep;
private static final int MARGIN = 60;
public BarGraph() {
xValues = new ArrayList<Integer>();
yValues = new ArrayList<String>();
hGap = 5;
xScaleMax = 100;
xScaleStep = 10;
}
public BarGraph(List<Integer> xValues) {
this();
this.xValues = xValues;
for (Integer xValue : xValues) {
yValues.add(xValue.toString());
}
}
public BarGraph(List<Integer> xValues, List<String> yValues, int hGap, int xScaleMax) {
this.xValues = xValues;
this.yValues = yValues;
this.hGap = hGap;
this.xScaleMax = xScaleMax;
xScaleStep = 10;
}
@Override
public void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
int startingY = MARGIN - 1;
int drawingWidth = width - MARGIN * 2;
double drawingHeight = height - MARGIN * 2;
int fontHeight;
Graphics2D g2 = initGraphics(g);
fontHeight = g2.getFontMetrics().getHeight();
drawGraphBorder(g2);
int barWidth = drawBars(g2, drawingWidth, drawingHeight, startingY);
drawXValues(g2, barWidth, fontHeight);
drawYValues(g2, drawingHeight);
}
private Graphics2D initGraphics(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(2));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
return g2;
}
private void drawGraphBorder(Graphics2D g2) {
g2.drawLine(MARGIN, MARGIN, MARGIN, getHeight() - (MARGIN - 5));
g2.drawLine(MARGIN - 5, getHeight() - MARGIN, getWidth() - MARGIN, getHeight() - MARGIN);
}
private int drawBars(Graphics2D g2, int drawingWidth, double drawingHeight, int startingY) {
g2.setColor(Color.ORANGE);
int barWidth = drawingWidth / xValues.size() - hGap;
for (int i = 0; i < xValues.size(); i++) {
int value = xValues.get(i);
int barHeight = (int) (value * drawingHeight / xScaleMax);
g2.fillRect(MARGIN + (i + 1) * hGap + i * barWidth, (int) (startingY + drawingHeight - barHeight), barWidth, barHeight);
}
return barWidth;
}
private void drawXValues(Graphics2D g2, int barWidth, int fontHeight) {
g2.setColor(Color.BLACK);
int previousEnd = 0;
boolean up = true;
for (int i = 0; i < yValues.size(); i++) {
String value = yValues.get(i);
int center = MARGIN + (i + 1) * hGap + i * barWidth + barWidth / 2;
int wordWidth = getWordWidth(value, g2);
int startX = center - wordWidth / 2;
if (startX > previousEnd) {
g2.drawString(value, startX, getHeight() - MARGIN + fontHeight);
up = true;
} else {
if (up) {
g2.drawString(value, startX, getHeight() - MARGIN + 2 * fontHeight - 2);
g2.drawLine(center, getHeight() - MARGIN, center, getHeight() - MARGIN + fontHeight);
} else {
g2.drawString(value, startX, getHeight() - MARGIN + fontHeight);
}
up = !up;
}
previousEnd = startX + wordWidth;
}
}
private void drawYValues(Graphics2D g2, double drawingHeight) {
int startY = getHeight() - MARGIN;
double step = drawingHeight * xScaleStep / xScaleMax;
g2.setFont(new Font("Calibri", Font.PLAIN, 24));
int fontHeight = g2.getFontMetrics().getHeight();
for (int i = 0; i <= xScaleMax; i += xScaleStep) {
String value = i + "";
int wordWidth = getWordWidth(value, g2);
g2.drawString(value, MARGIN - 7 - wordWidth, (int) (startY - step * i / xScaleStep) + fontHeight / 4);
g2.drawLine(MARGIN - 5, (int) (startY - step * i / xScaleStep), MARGIN, (int) (startY - step * i / xScaleStep));
}
}
private int getWordWidth(String word, Graphics2D g2) {
return g2.getFontMetrics().charsWidth(word.toCharArray(), 0, word.length());
}
}


Sign In
Create Account


Back to top









