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

  • Home
  • SEARCH
  • 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 1044041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:45:49+00:00 2026-05-16T15:45:49+00:00

import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Frame; import java.awt.Graphics; import java.awt.Color; import java.util.Random; public class

  • 0
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Color;
import java.util.Random;

public class dots {
    public dots() {
        init();
    }
    public void init() {
        JFrame frame = new JFrame("Dots");
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        int scrWidth = (int) frame.getSize().getWidth();
        int scrHeight = (int) frame.getSize().getHeight();
        JFrame.getContentPane().add(panel);
        Random rand = new Random();
        Graphics g = panel.getGraphics();
        for (int i = 0; i < 18; i++) {
            g.setColor(i < 12 ? Color.YELLOW : Color.BLUE);
            g.drawOval(Random.nextInt(scrWidth),Random.nextInt(scrHeight),40,40);
        }
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        dots d = new dots();
    }
}

Alright, this makes no sense. When I try to compile this, I get

dots.java:19: non-static method getContentPane() cannot be referenced from a sta
tic context
                JFrame.getContentPane().add(panel);
                      ^
dots.java:24: non-static method nextInt(int) cannot be referenced from a static
context
                        g.drawOval(Random.nextInt(scrWidth),Random.nextInt(scrHe
ight),40,40);
                                         ^
dots.java:24: non-static method nextInt(int) cannot be referenced from a static
context
                        g.drawOval(Random.nextInt(scrWidth),Random.nextInt(scrHe
ight),40,40);
                                                                  ^
3 errors

I am obviously not operating in a static context, so I have no idea what is going on. Any help sincerely 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-05-16T15:45:49+00:00Added an answer on May 16, 2026 at 3:45 pm

    Those are instance methods, so you need to call them on objects, not the class.

    frame.getContentPane().add(panel);
    // ...
    g.drawOval(rand.nextInt(scrWidth), rand.nextInt(scrHeight),40,40);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.awt.Color; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MyDrawPanel extends
import java.awt.Graphics; import javax.swing.*; public class Demo { JFrame jf; JLabel[] labels; JPanel panel;
import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class Sheet extends JFrame{ private String[] line
import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.BorderFactory; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics;
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
Code : import javax.swing.*; import java.awt.*; public class firstGUI extends JPanel { public static
Here is the code: import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.event.*;
Here is my code... import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
Here is the code: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TestGrid {

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.