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 7723639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:29:33+00:00 2026-06-01T04:29:33+00:00

Right now I’m working on a program that throws up a bunch of separate

  • 0

Right now I’m working on a program that throws up a bunch of separate (generated at runtime) images, each in their own window. To do this i’ve tried this approach:

public void display(){
    JFrame window = new JFrame("NetPart");
    JPanel canvas = new JPanel();
    window.getContentPane().add(canvas);
    Graphics g = canvas.getGraphics();
    Dimension d = getSize();
    System.out.println(d);
    draw(g,new Point(d.minX*50,d.maxY*50), 50);
    window.setSize(d.size(50));
    window.setResizable(false);
    window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    window.setVisible(true);
}

public void draw(Graphics g, Point startLoc, int scale){
    // generate and draw the image
}

public Dimension getSize(){
    //returns my own dimensions class
}

However, this throws a NullPointerException in draw, claiming that the graphics is null. is there any way to externally draw to a JPanel from outside it (not inherit from JPanel and override PaintComponent)? Any help would be appreciated.

  • 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-01T04:29:34+00:00Added an answer on June 1, 2026 at 4:29 am

    If you are drawing your images at runtime you should use BufferedImage.

    Create a BufferedImage, call getGraphics() on it to get its Graphics object, draw to it using the Graphics(2D) api, then call Graphics.dispose() (not strictly necessary but a good habit) and as the previous poster suggested either create an ImageIcon with it and put in a JLabel using setIcon() or subclass JPanel / JComponent and draw it in paintComponent

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

Sidebar

Related Questions

Right now I have an application that loads a bunch of thumbnail images into
Right now I've programmed a method for use in a swing program that is
Right now, I keep all of my projects on my laptop. I'm thinking that
Right now I have an SSIS package that runs every morning and gives me
Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
Right now, I only understand that Global indexes on a partitioned table are for
Right now I have a simple class that handles the parsing of XML files
Right now I have a script that creates symlinks to anything newer than 2
Right now I'm stuck between using PHP's native session management, or creating my own
Right now I am writing a little c++ console program using VS2010 Express. The

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.