Hi,
newbie questions.
When I draw lines in the following
Ruler and RulerCursor paint methods
I draw them both at y=20 but they
do not line up.
When I change Ruler from
extends JPanel to extends Container
RulerCursor does not draw.
Will someone explain what I'm doing wrong?
Thanks,
Jeff Higgins
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class TestLinePosition extends JFrame {
TestLinePosition() {
Ruler vRule = new Ruler();
RulerCursor vCursor = new RulerCursor();
vRule.add(vCursor);
MousePad mp = new MousePad();
JScrollPane sPane = new JScrollPane();
sPane.setViewportView(mp);
sPane.setRowHeaderView(vRule);
sPane.setViewportBorder(new BevelBorder(
BevelBorder.LOWERED));
getContentPane().add(sPane);
}
class MousePad extends Component {}
class Ruler extends JPanel {
public Ruler() {
setPreferredSize(new Dimension(20,1000));
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.drawLine(0, 20, 5, 20);
}
}
public class RulerCursor extends Component {
public RulerCursor() {
setPreferredSize(new Dimension(20,1000));
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.drawLine(5, 20, 10, 20);
}
}
public static void main(String[] args) {
JFrame frame = new TestLinePosition();
frame.pack();
frame.setBounds(100, 100, 200, 200);
frame.setVisible(true);
}
}
Andrew Thompson - 19 Jan 2007 02:05 GMT
> Will someone explain what I'm doing wrong?
I suspect you are doing a lot of things wrong,
but since I had no idea what you are attempting
to achieve, it is hard to say exactly what..
OTOH, the reason the lines are not 'aligned' is
that the JPanel has a default layout of FlowLayout.
Add a call to..
setLayout(new BorderLayout());
..in the the constructor of Ruler - and the red/black
lines will then 'align'.
Andrew T.
Jeff Higgins - 19 Jan 2007 02:27 GMT
>> Will someone explain what I'm doing wrong?
>
[quoted text clipped - 4 lines]
>
> Andrew T.
Thanks for the help
much appreciated.
JH
Knute Johnson - 19 Jan 2007 02:30 GMT
>>> Will someone explain what I'm doing wrong?
>> setLayout(new BorderLayout());
[quoted text clipped - 7 lines]
> much appreciated.
> JH
You might also move your drawing code from paint() to paintComponent()
for Swing components.

Signature
Knute Johnson
email s/nospam/knute/
Jeff Higgins - 19 Jan 2007 02:55 GMT
>>>> Will someone explain what I'm doing wrong?
>>> setLayout(new BorderLayout());
[quoted text clipped - 10 lines]
> You might also move your drawing code from paint() to paintComponent() for
> Swing components.
When I started this, I had originally drawn the ruler's ticks
and cursor in one JPanel contained in the JScrollPanel' HeaderViews,
but I got a lot of flicker when I moved the mouse in the MousePad
Component so I thought to overlay the Ruler JPanel with another
RulerCursor JPanel which I did but had other problems, so I switched
to RulerCursor Component - Andrew's advice fixed the alignment problem.
Seeing your response makes me think that paintComponent() is what I was
missing in the first place, so I think I'll go back and try MousePad
extends JPanel, Ruler extends JPanel and paints both ticks and cursor.
Thanks,
Jeff Higgins