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

The Archive Base Latest Questions

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

I’ve been searching for a way to draw a black-and-white array on screen. It’s

  • 0

I’ve been searching for a way to draw a black-and-white array on screen. It’s a simple array, just 20×20. What I plan to do is to draw on an array with the mouse so that each pixel “toggles” from black to white and back when clicked, then pass the array as a set of booleans (or integers) to another function. Currently I’m using Swing. I do remember to have used Swing for drawing on a canvas, but I still can’t find the actual usage. Should I use a canvas, or instead rely on JToggleButtons?

  • 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-26T01:04:19+00:00Added an answer on May 26, 2026 at 1:04 am

    You can simply use a JFrame (or other Swing component) and override the paint(Graphics) method to draw a representation of the boolean matrix (note that in the case of a lightweight component such as JPanel you should override paintComponent(Graphics). This will give you the click-and-drag capability you require (which is very difficult to achieve using a grid of individual Swing components).

    As other people have commented, AWT Canvas doesn’t give you anything not provided by Swing components and you’ll see in the example below that I’ve used the createBufferStrategy method also present on JFrame to ensure a non-flicker display.

    smiley face

    Note that my example is fairly simple in that it toggles every pixel you drag across rather than the click operation establishing whether you’re in “paint” mode or “erase” mode and then exclusively applying black or white pixels for the duration of the drag.

    public class Grid extends JFrame {
        private static final int SCALE = 10; // 1 boolean value == 10 x 10 pixels.
        private static final int SIZE = 20;
    
        private boolean[][] matrix = new boolean[SIZE][SIZE];
        private boolean painting;
        private int lastX = -1;
        private int lastY = -1;
    
        public Grid() throws HeadlessException {
            setPreferredSize(new Dimension(SIZE * SCALE, SIZE * SCALE));
            setResizable(false);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setBackground(Color.WHITE);
    
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    painting = true;
                    tryAdjustValue(e.getPoint());
                }
    
                public void mouseReleased(MouseEvent e) {
                    painting = false;
                    lastX = -1;
                    lastY = -1;
                }
            });
    
            addMouseMotionListener(new MouseMotionListener() {
                public void mouseDragged(MouseEvent e) {
                    tryAdjustValue(e.getPoint());
                }
    
                public void mouseMoved(MouseEvent e) {
                    tryAdjustValue(e.getPoint());
                }
            });
        }
    
        private void tryAdjustValue(Point pt) {
            int newX = pt.x / SCALE;
            int newY = pt.y / SCALE;
    
            if (painting && isInRange(newX) && isInRange(newY) && (newX != lastX || newY != lastY)) {
                // Only invert "pixel" if we're currently in painting mode, both array indices are valid
                // and we're not attempting to adjust the same "pixel" as before (important for drag operations).
                matrix[newX][newY] = !matrix[newX][newY];
                lastX = newX;
                lastY = newY;
                repaint();
            }
        }
    
        private boolean isInRange(int val) {
            return val >= 0 && val < SIZE;
        }
    
        public void paint(Graphics g) {
            super.paint(g);
    
            for (int x=0; x<SIZE; ++x) {
                for (int y=0; y<SIZE; ++y) {
                    if (matrix[x][y]) {
                        g.fillRect(x * SCALE, y * SCALE, SCALE, SCALE);
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            Grid grid = new Grid();
            grid.pack();
            grid.setLocationRelativeTo(null);
            grid.createBufferStrategy(2);
            grid.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

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.