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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:04:46+00:00 2026-05-25T23:04:46+00:00

This is a continuation from this post I have a set of random sized

  • 0

This is a continuation from this post

I have a set of random sized graphics adding and drawing onto a JPanel component. I have a button that is adding a new draw object to the same JPanel but is not displaying until i re-size the window. I have added the EDT information mentioned in this post and have also called the repaint() method on the component. I am not using an ArrayList yet, as suggested by Hovercraft, but I will. My brain needs to understand things slowly as i go.

Thank you.

The code is in two classes.

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ZombieDance extends JComponent {

JFrame canvas = new JFrame();
JPanel actionPanel = new JPanel();
JButton newZombie = new JButton("Add new Zombie");

ZombieDance(){
//create a couple default zombies
    buildGUI();
    Random rand = new Random();
    int i,x,y,w,h;

    //add zombies to my canvas
    for (i=1;i<8;i++) {
        float r = rand.nextFloat();
        float g = rand.nextFloat();
        float b = rand.nextFloat();
        x = rand.nextInt(50);
        y = rand.nextInt(50);
        w = rand.nextInt(50);
        h = rand.nextInt(50); 
        canvas.add(new Zombie(x,y,w,h,r,g,b));
    }   
}

//prep the canvas
void buildGUI(){
    actionPanel.add(newZombie);
    canvas.add(actionPanel);
    canvas.setLayout(new GridLayout(3,3));
    canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvas.setSize(400,400);
    canvas.setBackground(Color.WHITE);
    canvas.setVisible(true);

    newZombie.addActionListener(new NewZombieClickHandler());
}

public class NewZombieClickHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        Random rand = new Random();
        int x,y,w,h;
        float r = rand.nextFloat();
        float g = rand.nextFloat();
        float b = rand.nextFloat();
        x = rand.nextInt(50);
        y = rand.nextInt(50);
        w = rand.nextInt(50);
        h = rand.nextInt(50); 
        canvas.add(new Zombie(x,y,w,h,r,g,b));
        canvas.repaint();
    }
}   

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          new ZombieDance();
        }
    });
  }

}

Second class

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Zombie extends JPanel{

private int x,y,w,h;
private float r,g,b;

Zombie(int argx, int argy, int argw, int argh, float argr, float argg, float argb){
    x = argx;
    y = argy;
    w = argw;
    h = argh;
    r = argr;
    g = argg;
    b = argb;
}

public Dimension getPreferredSize() {
    return new Dimension(20,20);
}

protected void paintComponent(Graphics gr) {
    super.paintComponent(gr);       
    //g.drawString("Drawing canvas...",10,20);
    gr.setColor(new Color(r,g,b));
    gr.fillRect(x,y,h,w);
} 
}
  • 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-25T23:04:47+00:00Added an answer on May 25, 2026 at 11:04 pm

    I have a button that is adding a new draw object to the same JPanel but is not displaying until i re-size the window

    When you add a component to a visible GUI the code should be:

    canvas.add(...);
    canvas.validate();
    //canvas.repaint(); // sometimes needed
    

    (editor: changed to validate)

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

Sidebar

Related Questions

This is a continuation of the post How does one access a method from
This is a continuation question from a previous question I have asked I now
This question is in continuation of this post , I have tried installing Xerces-C
This is a continuation question I had from a post yesterday: Initializing NSMutableDictionary (It's
This question is continuation from my earlier post titled selecting digits from regular expression.
This is just the continuation of this post . I'm coming from ASP.NET world.
Ok - this is in continuation from my earlier question about sending an email
This is a continuation of the question here: JBoss - does app have to
This is a continuation from a previous stackoverflow question. I've renamed some variables so
This question is in continuation to my previous post located here . Since there

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.