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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:19:39+00:00 2026-06-10T23:19:39+00:00

How do you initialize a Graphics variable? I tried Graphics g = new Graphics()

  • 0

How do you initialize a Graphics variable?

I tried Graphics g = new Graphics() and Graphics g; but it says it cannot be instantiated. I need to pass Graphics g from one method to another so I can’t just call it from the argument. Any help is appreciated.

This is my code:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.applet.Applet;
import java.lang.Math;
import java.util.Random;

public class testApp extends JApplet 
{
    public void init() 
    {
    this.add(new RandomCardsPanel());
    }

public RandomCardsPanel extends JPanel 
{
    public Image card1, card2, card3, card4, card5, card6, card7, card8, card9, card10, card11, card12, card13;
    public Image card14, card15, card16, card17, card18, card19, card20, card21, card22, card23, card24, card25, card26;
    public Image card27, card28, card29, card30, card31, card32, card33, card34, card35, card36, card37, card38, card39;
    public Image card40, card41, card42, card43, card44, card45, card46, card47, card48, card49, card50, card51, card52;

    public RandomCardsPanel() 
    {
        Image[] card = new Image[52];

        card[0] = getImage( getDocumentBase(), "c1.gif" );
        card[1] = getImage( getDocumentBase(), "c2.gif" );
        card[2] = getImage( getDocumentBase(), "c3.gif" );
        card[3] = getImage( getDocumentBase(), "c4.gif" );
        card[4] = getImage( getDocumentBase(), "c5.gif" );
        card[5] = getImage( getDocumentBase(), "c6.gif" );
        card[6] = getImage( getDocumentBase(), "c7.gif" );
        card[7] = getImage( getDocumentBase(), "c8.gif" );
        card[8] = getImage( getDocumentBase(), "c9.gif" );
        card[9] = getImage( getDocumentBase(), "c10.gif" );
        card[10] = getImage( getDocumentBase(), "cj.gif" );
        card[11] = getImage( getDocumentBase(), "cq.gif" );
        card[12] = getImage( getDocumentBase(), "ck.gif" );
        card[13] = getImage( getDocumentBase(), "d1.gif" );
        card[14] = getImage( getDocumentBase(), "d2.gif" );
        card[15] = getImage( getDocumentBase(), "d3.gif" );
        card[16] = getImage( getDocumentBase(), "d4.gif" );
        card[17] = getImage( getDocumentBase(), "d5.gif" );
        card[18] = getImage( getDocumentBase(), "d6.gif" );
        card[19] = getImage( getDocumentBase(), "d7.gif" );
        card[20] = getImage( getDocumentBase(), "d8.gif" );
        card[21] = getImage( getDocumentBase(), "d9.gif" );
        card[22] = getImage( getDocumentBase(), "d10.gif" );
        card[23] = getImage( getDocumentBase(), "dj.gif" );
        card[24] = getImage( getDocumentBase(), "dq.gif" );
        card[25] = getImage( getDocumentBase(), "dk.gif" );
        card[26] = getImage( getDocumentBase(), "h1.gif" );
        card[27] = getImage( getDocumentBase(), "h2.gif" );
        card[28] = getImage( getDocumentBase(), "h3.gif" );
        card[29] = getImage( getDocumentBase(), "h4.gif" );
        card[30] = getImage( getDocumentBase(), "h5.gif" );
        card[31] = getImage( getDocumentBase(), "h6.gif" );
        card[32] = getImage( getDocumentBase(), "h7.gif" );
        card[33] = getImage( getDocumentBase(), "h8.gif" );
        card[34] = getImage( getDocumentBase(), "h9.gif" );
        card[35] = getImage( getDocumentBase(), "h10.gif" );
        card[36] = getImage( getDocumentBase(), "hj.gif" );
        card[37] = getImage( getDocumentBase(), "hq.gif" );
        card[38] = getImage( getDocumentBase(), "hk.gif" );
        card[39] = getImage( getDocumentBase(), "s1.gif" );
        card[40] = getImage( getDocumentBase(), "s2.gif" );
        card[41] = getImage( getDocumentBase(), "s3.gif" );
        card[42] = getImage( getDocumentBase(), "s4.gif" );
        card[43] = getImage( getDocumentBase(), "s5.gif" );
        card[44] = getImage( getDocumentBase(), "s6.gif" );
        card[45] = getImage( getDocumentBase(), "s7.gif" );
        card[46] = getImage( getDocumentBase(), "s8.gif" );
        card[47] = getImage( getDocumentBase(), "s9.gif" );
        card[48] = getImage( getDocumentBase(), "s10.gif" );
        card[49] = getImage( getDocumentBase(), "sj.gif" );
        card[50] = getImage( getDocumentBase(), "sq.gif" );
        card[51] = getImage( getDocumentBase(), "sk.gif" );

        String cardNumber; 
        double cardRandom;
        int cardRandomNumber;
        public int[] ranNum = new int[10];
        Random ran = new Random();


        for (int number = 0; number <=  9; )
        {
            cardRandom = ran.nextInt(52) + 1;
            cardRandomNumber = (int)Math.round( cardRandom );

            if ( cardRandomNumber > 0 && cardRandomNumber <= 52 )
            { 
                ranNum[number] = cardRandomNumber;
                number++;
            }
        }   
    }

   public void paintComponent(Graphics g) 
    {
        setBackground( Color.green );
        g.drawImage( cards[ranNum[0]], 10, 10, this);
    }
}

}

  • 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-10T23:19:40+00:00Added an answer on June 10, 2026 at 11:19 pm

    Typically you don’t need to initialize a Graphics variable since most painting should be done in a components paintComponent() method. (And you can’t do Graphics g = new Graphics(); because Graphics is an abstract class.) Usually I will create a class which extends JPanel and override the paintComponent() method to do my custom painting. From there, I can pass the Graphics reference to any other methods that need it.

    Try this out. Of course, I am talking at a pretty abstract level. Let us know what you figure out from these tips and we’ll work from there.

    Edit:

    JComponent declares an abstract method paintComponent(Graphics g). If you want to provide your own implementation, you must match this method signature exactly. That means you are only allowed one parameter, namely a Graphics object.

    You are free to create a paintComponent() method with more arguments, but since the method signature doesn’t match, it will not be called automatically on the Event Dispatch Thread when the operating system decides your Applet needs to be repainted.

    With that said, you don’t have to extend JPanel in order to get this to work. You can extend any class which in turn has JComponent as an ancestor. In your particular case, JPanel seems like the best option. I suggest that you refactor your code as follows:

    In RandomCards.java

    public RandomCards extends JApplet {
        public void init() {
            this.add(new RandomCardsPanel();
        }
    }
    

    In RandomCardsPanel.java

    public RandomCardsPanel extends JPanel {
        // Put private member variables here
    
        public RandomCardsPanel() {
          // Put code from your current init() method here
        }
    
        public void paintComponent(Graphics g) {
            // Put your custom painting code here, including setting the background color and drawing the card images
        }
    }
    

    I have a few further suggestions, if you don’t mind:

    1. Notice that I am extending JApplet rather than Applet. This is the preferred class. Applet is still around for backwards-compatibility with AWT. Modern Java applets use JApplet and Swing instead.

    2. When you indent code, use spaces rather than tabs. Tabs are displayed in a system-dependent manner. This means that if you view your code in another editor or on another computer, tab-formatted code may not appear the way you intended. In particular, you can see that this website does not format your code properly. Most text editors have a setting to replace tabs with spaces. I strongly suggest that you find out how to do this with yours.

    3. When you find yourself using variable names that only differ with a number suffix (e.g. Image card1, card2, card3, card4, card5, etc.), you should use an array instead.

    4. You are using an array inside the init() method. However, you have declared it as a local variable. This means that none of your other methods can see the card array you declared here. You should declare this as a member variable instead of the card1, card2, card3, etc. variables. The same goes for any other variables you need to use in your paintComponent() method since it can only have a single Graphics g parameter.

    5. You do not need to call paintComponent() explicitly. In fact, you should not call it expicitly. The system will call this method whenever it is appropriate.

    I hope this helps to clarify a few points. Good luck with your Java experience. Let us know if you have any more questions.

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

Sidebar

Related Questions

I don't know why, but I can't call the method paint(Graphics g) from code.
When initialize an entity framework context. One is to initialize at class level, such
To initialize an int array with all zeros, do I need to use: int
Objective Initialize a new addition problem for users to solve when they visit /task
Can I initialize an IOLoop.instance and than fork new processes, which uses IOLoop.instance()? Something
I need to get a regular snapshot from a webcam in Delphi. Speed is
I've made a couple of apps for android, but none used graphics that much.
I'm trying to get into generating 3D graphics in F# (as was apparent from
//initialize the people data list - (void)initializePeopleListFromJson:(CLLocationCoordinate2D)coordinate auraId:(NSString *)auraId { //initialize the NSMutableArray list
def initialize(apps, catch=404) @apps = []; @has_app = {} apps.each { |app| add app

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.