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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:53:05+00:00 2026-06-03T03:53:05+00:00

I make a game in java and I can’t seem to call the paintComponent()

  • 0

I make a game in java and I can’t seem to call the paintComponent() method in my game. here’s the code :

public class TwinMoonTowers extends JPanel implements ActionListener{

    private class TMTMenu extends MouseAdapter{
        private Icon start, shop, quit;
        private Icon main_bg;

        public TMTMenu (){
            System.out.println("starting menu...");

            start = new Icon(TMTConstant.START_SOURCE,
                        TMTConstant.START_COORD_X, TMTConstant.START_COORD_Y);

            shop = new Icon (TMTConstant.SHOP_SOURCE,
                        TMTConstant.SHOP_COORD_X, TMTConstant.SHOP_COORD_Y);

            quit = new Icon (TMTConstant.EXIT_SOURCE,
                        TMTConstant.EXIT_COORD_X, TMTConstant.EXIT_COORD_Y);

            main_bg = new Icon (TMTConstant.MAIN_MENU_BG, 0, 0);
        }

        @Override
        public void mousePressed(MouseEvent me){

            int x = me.getX();
            int y = me.getY();

            System.out.println("coord: "+x+","+y);

            if (me.getButton() == MouseEvent.BUTTON1){
                if (x > start.x && x < start.x + start.getIcon().getWidth(null)
                        && y > start.y && y < start.y + start.getIcon().getHeight(null)){
                    curState = TMTConstant.MODE_STATE;
                }
                else if (x > quit.x && x < quit.x + quit.getIcon().getWidth(null)
                        && y > quit.y && y < quit.y + quit.getIcon().getHeight(null)){
                    curState = TMTConstant.EXIT_STATE;
                }
                else if (x > shop.x && x < shop.x + shop.getIcon().getWidth(null)
                        && y > shop.y && y < shop.y + shop.getIcon().getHeight(null)){
                    curState = TMTConstant.SHOP_STATE;
                }
            }

        }
    }

    private class TMTModeSelector extends MouseAdapter{
        private Icon TwinTower, TripleTower, back;

        public TMTModeSelector(){

        }

        @Override
        public void mousePressed(MouseEvent e){

        }
    }

    private String testDebug;
    private int curState;
    private TMTMenu menu;

    public TwinMoonTowers (){
        menu = new TMTMenu();
        curState = TMTConstant.MAIN_MENU_STATE;

        System.out.println("creating panel..");

        this.setVisible(true);
        this.setDoubleBuffered(true);
        this.setSize(TMTConstant.WINDOW_WIDTH, TMTConstant.WINDOW_HEIGHT);
        this.addMouseListener(menu);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        this.repaint();

    }
    @Override
    public void paintComponent (Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        System.out.println("preparing canvas..");

        switch(curState){
            case TMTConstant.MAIN_MENU_STATE :
                g2d.drawImage(menu.main_bg.getIcon(), menu.main_bg.x, menu.main_bg.y, this);
                g2d.drawImage(menu.start.getIcon(), menu.start.x, menu.start.y, null);
                g2d.drawImage(menu.quit.getIcon(), menu.quit.x, menu.quit.y, null);
                g2d.drawImage(menu.shop.getIcon(), menu.shop.x, menu.shop.y, null);
                break;
            case TMTConstant.GAME_STATE :
                break;
            case TMTConstant.SHOP_STATE :
                break;
            case TMTConstant.MODE_STATE :
                break;
        }

        System.out.print("render image...");

        Toolkit.getDefaultToolkit().sync();
        g.dispose();

    }
}

while class Icon is this:

public class Icon{

private Image pic;
public int x, y;

public Icon (String path, int x, int y){

    ImageIcon ii = new ImageIcon (path);
    pic = ii.getImage();

    this.x = x;
    this.y = y;
}

public Image getIcon() {return pic;}
}

the basics here is to draw a menu containing 3 buttons (start, shop, exit)

but, the paintComponent method is not called (the system.out.println not showing)

  • 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-03T03:53:05+00:00Added an answer on June 3, 2026 at 3:53 am
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
    

    These lines are unnecessary (might even be wrong), remove them.

    The problem is most likely not in the code you posted. Where do you add the panel to a top level window (frame/applet)? What layout do you use for its content pane? My guess is the size is (0,0) so the panel won’t be painted.

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

Sidebar

Related Questions

I have to make a game in Java for my computer science class. Since
I need to make a game in Java for a project. What I'm trying
I'm trying to make a game (using irrlicht engine with c++) where you can
I want to make a java swing application from where i can generated xml
I am attempting to make a game using java, and I need a plugin
I was trying to make a board game in java and i am stuck
I wanted to make a memories or find pairs game for Java, just to
I'm making a chess game in Java, and testing to make sure there are
I try hard to find the problem in this Java code, but I can't
I'm attempting to make a simple game of Pairs for Android. Program Structure Menu.java

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.