I have a JFrame containing a JScrollPane containing a JPanel (with only graphics) and a JTabbedPanel.
On the JScrollPane, here is the paint method :
public void paint(Graphics g) {
g2d = (Graphics2D) g;
super.paint(g2d);
if (geneLines.size() != 0 ) {
g2d.drawString("5'",linePadding, topMargin+4);
for(int i=0; i<geneLines.size();i++) {
drawDNALine(i);
drawLineLabels(i);
for(int j=0; j<geneLines.get(i).size();j++) {
Gene gene = geneLines.get(i).get(j);
drawGene(i, gene.getMid(), initColor(gene.getFunctionnality()), gene.getFunctionnality() >= 2 ? (float)0.5:(float)1, gene);
}
}
g2d.drawString("3'", 510, (geneLines.size()-1)*linePadding+topMargin+4);
}
setPreferredSize(new Dimension(getWidth()-20,(geneLines.size()-1)*linePadding+topMargin-GlobalVars.squareY/2+topMargin));
revalidate();
}
And here is the drawGene method :
private void drawGene(int l, int mid, Color c, float w, Gene g) {
int posX = Math.round(((((float)mid/(float)length)-l)*400+100-GlobalVars.squareX/2));
int posY = l*linePadding+topMargin-GlobalVars.squareY/2;
g2d.setColor(c);
g2d.fillRect(posX,posY,(int) (GlobalVars.squareX*w),GlobalVars.squareY);
g2d.setColor(Color.black);
g2d.drawRect(posX,posY,(int) (GlobalVars.squareX*w),GlobalVars.squareY);
AffineTransform atBefore = g2d.getTransform();
Rectangle2D r = g2d.getFontMetrics().getStringBounds(g.getName(), g2d);
AffineTransform trans = AffineTransform.getTranslateInstance(posX+5, posY-40);
trans.concatenate(AffineTransform.getRotateInstance(-Math.PI/2));
trans.concatenate(AffineTransform.getTranslateInstance(-r.getCenterX(), -r.getCenterY()));
g2d.setTransform(trans);
g2d.drawString(g.getName(), 0, 0);
g2d.setTransform(atBefore);
}
So, here is my goal and my problem. I have to make a picture showing the gene distribution on lines. Each gene is a square (or rectangle), and I have to write the name of the gene vertically above the square/rectangle. I also write some information like the length of the line, etc.
By this way, when I resize the frame, all is ok (well repainted), same if I reduce the frame. But, if the number of line is too large, a scrollbar appear (that’s why I use JScrollPane). And when I move this scrollbar, all vertical text disappear. I don’t understand why, because the text drawing is in the same method than the rectangle drawing (and they are well repainted while scrolling).
I have no idea about where this is coming from… I’ve tried some manual repaints, but I have no result.
I’m using Eclipse on Windows, and I’m available if you need more information, of course. Thank’s very much for any help.
You have to use a simple JScrollPane with a JPanel doing the drawing and resizing. If you give the JPanel a border and other background colour, you will see whether everything behaves as desired.
An other answer
Painting should be fast, and not call
setPreferredSizeandrevalidate.For the moment you could do:
Then this?
There is no difference, but maybe the painting takes long and then this just might help.
Last resort
Make a JPanel with a BoxLayout in the vertical direction. Add for every line a JPanel that does the drawing for just that line. (Or a JList)