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 / General / May 2006

Tip: Looking for answers? Try searching our database.

Paint Method

Thread view: 
JessyCute - 04 May 2006 11:45 GMT
I try to draw the line like this on the screen, but when some menu show
up my line is not draw correctly. Anyone know how to solve it thankyou.

Follow this code. I tried paint() and paintComponent() but it doesn't
work both.

Code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class TestPaint extends JFrame{

  public TestPaint(){
     JMenuBar bar = new JMenuBar();
     JMenu testM = new JMenu("TEST");
     testM.add(new JMenuItem("test1-child"));
     testM.add(new JMenuItem("test1-child"));

     JMenu test1M = new JMenu("TEST1");
     test1M.add(new JMenuItem("test1-child"));
     test1M.add(new JMenuItem("test1-child"));
     test1M.add(new JMenuItem("test1-child"));
     test1M.add(new JMenuItem("test1-child"));
     test1M.add(new JMenuItem("test1-child"));

     JMenu test2M = new JMenu("TEST2");
     test2M.add(new JMenuItem("test1-child"));
     test2M.add(new JMenuItem("test1-child"));
     test2M.add(new JMenuItem("test1-child"));
     test2M.add(new JMenuItem("test1-child"));
     test2M.add(new JMenuItem("test1-child"));

     JMenu test3M = new JMenu("TEST2");
     test3M.add(new JMenuItem("test1-child"));
     test3M.add(new JMenuItem("test1-child"));
     test3M.add(new JMenuItem("test1-child"));
     test3M.add(new JMenuItem("test1-child"));
     test3M.add(new JMenuItem("test1-child"));
     test3M.add(new JMenuItem("test1-child"));
     test3M.add(new JMenuItem("test1-child"));
     test3M.add(new JMenuItem("test1-child"));
     test3M.add(new JMenuItem("test1-child"));
     test3M.add(new JMenuItem("test1-child"));

     bar.add(testM);
     bar.add(test1M);
     bar.add(test2M);
     bar.add(test3M);

     setJMenuBar(bar);

     getContentPane().add(new TestPanel(), BorderLayout.CENTER);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setSize(400,400);
     show();
  }

  class TestPanel extends JPanel{

     /*public void paint(Graphics g) {
        super.paint(g);
        System.out.println("paint");
        int x2 = ( int ) g.getClipBounds().getWidth();
        int y = 100;
        g.setColor( Color.RED );
        g.drawLine( 0, y, x2, y );
     }*/

     protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println("paintComponent");
        int x2 = ( int ) g.getClipBounds().getWidth();
        int y = 100;
        g.setColor( Color.RED );
        g.drawLine( 0, y, x2, y );
     }
  }
 
  public static void main(String[] args) { new TestPaint(); }
}
Bart Cremers - 04 May 2006 11:59 GMT
When using the clipbounds to paint you'll have to make sure you take
the x position of the clip rectangle also into account:

   int x1 = g.getClipBounds().x;
   int x2 = x1 + g.getClipBounds().width;
   int y = 100;
   g.setColor(Color.RED);
   g.drawLine(x1, y, x2, y);

Regards,

Bart
Bart Rider - 04 May 2006 14:35 GMT
> I try to draw the line like this on the screen, but when some menu show
> up my line is not draw correctly. Anyone know how to solve it thankyou.
[quoted text clipped - 85 lines]
>    public static void main(String[] args) { new TestPaint(); }
> }

Use x2=getWidth(), so you draw from zero x coordinate to the
maximum (you may want to give credit to any border zone). If
you use the clip bounderies, the section to redraw might not
nessecarily start with x coordinate zero, but that was pointed
out by Bart Cremers before. :)

Just a hint to add:
dont use show any more, instead use  setVisible(true) and before
the call to setVisible insert a pack() statement (together with
a new method in TestPanel called getMinimumSize)


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



©2009 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.