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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:31:01+00:00 2026-05-30T19:31:01+00:00

I think I need to put some code where the comment is (or maybe

  • 0

I think I need to put some code where the comment is (or maybe use non static method but I am not sure). The main method creates the window and then starts the graphics method. I would like the blue square to flash.

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


public class paintTest extends JPanel{
private static JFrame theWindow = new JFrame("Window");
static boolean blueSqr = false;

public void paint(Graphics g) {
    g.setColor(Color.RED);
    g.fillRect(10, 10, 10, 10);

    if(blueSqr){
        g.setColor(Color.BLUE);
        g.fillRect(10, 10, 10, 10);
    }
}

public static void main(String[] args){
    createWindow();
    theWindow.getContentPane().add(new paintTest());
    while(true){
        blueSqr = false;

        System.out.println("off");

        try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}

        blueSqr = true;
        // Needs something here
        System.out.println("on");

        try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        }
}

public static void createWindow(){
    theWindow.setSize(500, 500);
    theWindow.setLocationRelativeTo(null);
    theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    theWindow.setVisible(true);
}
}

Any help would be really good.

  • 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-30T19:31:03+00:00Added an answer on May 30, 2026 at 7:31 pm

    Use a Swing Timer to call repaint(). Also, override paintComponent() in a JPanel, rather than paint().

    Something like this:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class PaintTest extends JPanel{
    
        boolean blueSqr = false;
    
        PaintTest() {
            setPreferredSize(new Dimension(100,25));
            ActionListener al = new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    blueSqr = !blueSqr;
                    repaint();
                }
            };
            Timer timer = new Timer(1000,al);
            timer.start();
        }
    
        public void paintComponent(Graphics g) {
            Color c = (blueSqr ? Color.BLUE : Color.RED);
            g.setColor(c);
            g.fillRect(10, 10, 10, 10);
        }
    
        public static void main(String[] args){
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame theWindow = new JFrame("Window");
                    theWindow.getContentPane().add(new PaintTest());
                    createWindow(theWindow);
                }
            });
        }
    
        public static void createWindow(JFrame theWindow){
            theWindow.pack();
            theWindow.setLocationByPlatform(true);
            theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            theWindow.setVisible(true);
        }
    }
    

    There were other improvements I could not be bothered documenting (code speaks louder than words). If you have any questions (check the docs first, then) ask.

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

Sidebar

Related Questions

I'm confused regarding each() and .each()... I think I need to use both, but
What I think I need to do is create some code that lets me
Let's say, hypothetically (read: I don't think I actually need this, but I am
I think even if we will not need interoperability between applications, and even we
its hard for me to explain this one, but i hope some code will
I'm trying to put some C hooks into some code that someone else wrote
a newbie django question I want to use 3rd party app, but I need
I am using PHP to generate some XML. I need to put quotes in
I've recently had to put some code into a different server and my .htaccess
I think I need a way to perform a pivot or crosstab using C#

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.