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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:18:04+00:00 2026-05-26T15:18:04+00:00

Trying to work with basic swing on Java keeps giving me issues. When the

  • 0

Trying to work with basic swing on Java keeps giving me issues.
When the JFrame is created by the runtime, none of the components are drawn initially, you have to resize the window to invoke paint() apparently? Is there a simple fix to this that I’m missing?

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class LabTen extends JFrame{

    int x, y;

    public LabTen(){
        this.setSize(200,200);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.getContentPane().add(new Board()); //do this in the constructor    
    }

    public static void main(String[] args){
        LabTen one = new LabTen();
        one.repaint();
    }       
}
//mouseListener has more things when we're going in and out so you should have it too
//write on the component or pannel, not the frame
class Board extends JComponent implements MouseListener, MouseMotionListener{

    int mouseX, mouseY;

    public Board(){
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void mouseMoved(MouseEvent e){           
        this.mouseX = e.getX();
        this.mouseY = e.getY();
        this.repaint(); 
    }

    public void mouseDragged(MouseEvent e){
        //do nothing...
    }

    public void mouseClicked(MouseEvent e){         
    }

    public void mouseEntered(MouseEvent e){         
    }

    public void mouseExited(MouseEvent e){
    }

    public void mousePressed(MouseEvent e){         
    }

    public void mouseReleased(MouseEvent e){
    }

    public void paintComponent(Graphics g){
        //g.drawString("(" + this.mouseX + ", " + this.mouseY +  ")", this.mouseX,this.mouseY);
        //this uses the default way
        // g.drawLine(this.getWidth()/2, this.getHeight()/2, this.mouseX, this.mouseY);

        double distance = Math.sqrt(Math.pow(this.mouseX - this.getWidth()/2, 2) + Math.pow(this.mouseY - this.getHeight()/2, 2));          
        int centerX = this.getWidth()/2;
        int centerY = this.getHeight()/2;

        for(int i = 0; i < 20; i++){
            double distanceX = 
            g.drawLine(centerX, centerY, (centerX))
        }           
    }
}
  • 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-26T15:18:04+00:00Added an answer on May 26, 2026 at 3:18 pm

    You are doing it completely the wrong order. Do it this way:

    1. Add the component

      this.add(new Board());
      
    2. Set the preferred size

      this.setPreferredSize(new Dimension(200, 200));
      
    3. Pack the frame

      this.pack();
      
    4. Set the frame visible.

      this.setVisible(true);
      

    Of course, this is your JFrame.

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

Sidebar

Related Questions

I'm trying to understand why this does not work. (Basic example with no validation
I am trying to work my way through basic iPhone programming and I have
I have two issues I'm trying to work though. The first is I'm trying
I'm trying to create a basic Pacman game in C++ (I'll use Java syntax
trying to do a really basic player for testing as we're having some issues
I am trying to make a simple email client in Java Swing. I want
I'm trying to work out why memory usage in a basic application I'm writing
I've got a very basic and common scenario I'm trying to work through in
I am trying work out with MERGE statment to Insert / Update Dimension Table
Trying to work with Eclipse for Android (ADT plugin) development at my iMac (2.4Ghz,

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.