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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:07:24+00:00 2026-05-29T08:07:24+00:00

I have an application where I have a JPanel with some rectangles painted on

  • 0

I have an application where I have a JPanel with some rectangles painted on it, based on informations retrived from a list (ref ()), later I change the list and need to repaint the JPanel.

I don’t think I’m doing it right, it’s getting slow. I which I could draw each rectangle in an autonomous way, I mean, if the rectangle ‘wants’ to change it’s color, it changes and that rectangle be automatically changed in the panel.

Can anybody help me? Here are some important snippets of codes I’m using.

(defn fill [g x y c]
  (doto g
    (.setColor c)
    (.fillRect 
      (+ *margin* (* y 15)) 
      (+ *margin* *margin-top* (* x 15)) 
              15 15)))

(defn draw [g x y c]
  (doto g
    (.setColor c)
    (.drawRect 
      (+ *margin* (* y 15)) 
      (+ *margin* *margin-top* (* x 15)) 
    15 15)))

(defn make-panel 
      ([]
        (proxy [JPanel] []
          (paintComponent 
            [g]
            (doseq [i (range *size*)
                    j (range *size*)]
              (let [ v (:color @(get-obj i j))]
                (cond 
                  (= v :blue) (fill g i j Color/BLUE)
                  (= v :red)  (fill g i j Color/RED)
                  :else       (draw g i j Color/LIGHT_GRAY))))))))

;; This is how I repaint the frame, after changing the list
(doto *frame*
      (.setContentPane (make-panel))
      .repaint
      (.setVisible true))
  • 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-29T08:07:25+00:00Added an answer on May 29, 2026 at 8:07 am

    Firstly, you don’t need to replace your panel (the (.setContentPane (make-panel)) part), just repaint the old panel.

    To do the minimal amount of work only when something actually changes you could use the watch facility. If information on all your rectangles is held in a single Ref, you might do something like this:

    (add-watch the-ref :update-rects
      (fn [_ _ old new]
        (update-rects-as-appropriate old new)))
    

    Here update-rects-as-appropriate stands for code which calculates rectangle colours based on the old and new data and performs the updates when — and only when — results differ. Depending on the precise form of the data held in the Ref, it might become obvious early in the process that there will be no difference, so the whole calculation doesn’t need to be carried out.

    If each rectangle has a Ref to itself, you can just update based on the new state:

    (add-watch the-ref :update-rect
      (fn [_ _ _ new]
        (update-rect the-rect new)))
    

    Here the-rect is the rectangle corresponding to the-ref and update-rect a function which knows how to update it based on the state of the-ref.

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

Sidebar

Related Questions

I am building a swing application. At some point, I have to start an
I have a custom component subclassed from JPanel, with a keyboard handler. My main
I have Java application which adds JTextFields @ runtime to JPanel. Basically user clicks
I have a swing application which stores a list of objects. When the users
I have an application that bounces Shapes around inside a JPanel. Whenever shapes hit
I'm having some trouble designing an MDI Application with Swing. I have no trouble
I have a JPanel on which some drawing is performed using paintComponent method and
I have application which needs to use a dll (also written by me) which
I have application that makes different queries with different results so the caching in
I have Application settings stored under HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany branch. Settings must be same for different

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.