Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:06:53+00:00 2026-06-13T08:06:53+00:00

I am writing a java application using swing in which I need to draw

  • 0

I am writing a java application using swing in which I need to draw a grid above a square. In order to do so, I am using the drawLine(...) method provided by the Graphics class.

Everything works fine except that it takes a lot of time to draw each line (more than 20 sec for 50 lines…). I can even see the lines being drawn in real time. One weird thing is that the horizontal lines are drawn way faster than the vertical lines (almost instantly).

I might be doing something wrong. Here is the code for the grid.

public void drawGrid(Graphics g){
    g.setColor(new Color(255, 255, 255, 20));
    int width = getWidth();
    int height = (int) (width * Utils.PLATE_RATIO);
    int step = pixelSize*gridSpacing;
    Color bright = new Color(255, 255, 255, 100);
    Color transparent = new Color(255, 255, 255, 20);
    for(int ix = insets.left + step;                        
            ix < width; ix += step){
        if(((ix - insets.left) / step) % 10 == 0){
            g.setColor(bright);
        }
        else{
            g.setColor(transparent);
        }
        g.drawLine(ix, insets.top, ix, height+insets.top);
    }
    for(int iy = insets.top+step;
            iy < (insets.top + height); iy += step){
        if(((iy - insets.top) / step) % 10 == 0){
            g.setColor(bright);
        }
        else{
            g.setColor(transparent);
        }
        g.drawLine(insets.left, iy, width + insets.left, iy);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:06:58+00:00Added an answer on June 13, 2026 at 8:06 am

    The code you have posted is fine, there is no problems in it.
    Here is a working example of a component using your method (a bit simplified):

    public static class MyGrid extends JComponent
    {
        private int step = 10;
    
        public MyGrid ()
        {
            super ();
        }
    
        public Dimension getPreferredSize ()
        {
            return new Dimension ( 500, 500 );
        }
    
        protected void paintComponent ( Graphics g )
        {
            super.paintComponent ( g );
            drawGrid ( g );
        }
    
        public void drawGrid ( Graphics g )
        {
            int width = getWidth ();
            int height = getHeight ();
            Color bright = new Color ( 255, 255, 255, 200 );
            Color transparent = new Color ( 255, 255, 255, 100 );
    
            for ( int ix = step; ix < width; ix += step )
            {
                if ( ( ix / step ) % 10 == 0 )
                {
                    g.setColor ( bright );
                }
                else
                {
                    g.setColor ( transparent );
                }
                g.drawLine ( ix, 0, ix, height );
            }
    
            for ( int iy = step; iy < height; iy += step )
            {
                if ( ( iy / step ) % 10 == 0 )
                {
                    g.setColor ( bright );
                }
                else
                {
                    g.setColor ( transparent );
                }
                g.drawLine ( 0, iy, width, iy );
            }
        }
    }
    

    I guess there is some problem outside that piece of code.

    P.S. A bit an offtopic but…

    I suggest you to calculate the visible part of the painting area (using either JComponent’s getVisibleRect () method or Graphics g.getClip ().getBounds () method) and limit your paintings with only that area.

    That small optimization could speedup component’s painting in times if it is really large (for example with 10000×10000 pixels component’s area).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Java application with GUI using Swing. One of the GUI components
I am writing an application in java (1.6) using swing. I currently have a
I am writing a mapping application in Java, using Swing for the UI (I've
I'm writing a Java Swing application for the Mac using Java 1.6. I've read
I'm writing an app in Java and swing using Netbeans, and I need property
I am writing a Desktop GUI application in Clojure using Java Swing. Normally when
writing a Java EE 6 application i need some help using the DatabaseServerLoginModule with
I'm writing an application with Java Swing. What i need is a procedure where
I am writing standalone java application which is using Hibernate. Maven brought jboss-logging library
I'm writing a Java Swing application using the Metal look-and-feel. Every time there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.