Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2007

Tip: Looking for answers? Try searching our database.

coordinate system and paint layered components

Thread view: 
Jeff Higgins - 19 Jan 2007 01:39 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.