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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:35:35+00:00 2026-05-25T12:35:35+00:00

I have some Javascript code that acts on an pixel array defined like so:

  • 0

I have some Javascript code that acts on an pixel array defined like so:

screen = {'width':160, 'height':144, 'data':new Array(160*144*4)};
...
canvas.putImageData(GPU._scrn, 0,0);

Where screen is 1D array of width * height * 4 values representing the colors as detailed here: https://developer.mozilla.org/En/HTML/Canvas/Pixel_manipulation_with_canvas

Is there a convenience method to paint this array to the screen as is? If not, what’s the easiest way to paint this array using Swing?

  • 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-25T12:35:36+00:00Added an answer on May 25, 2026 at 12:35 pm

    BufferedImage is probably the most flexible choice. You can use it as an Icon or override paintComponent() for the full generality of Java2D.

    PiRaster

    package overflow;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.image.BufferedImage;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    /** @see http://stackoverflow.com/questions/7298492 */
    public class PiRaster extends JPanel {
    
        private static final int W = 30;
        private static final int H = 30;
        private static List<Integer> pi = new ArrayList<Integer>();
        private final List<Integer> clut = new ArrayList<Integer>();
        private BufferedImage image;
    
        public PiRaster() {
            this.setPreferredSize(new Dimension(W * 16, H * 10));
            String s = ""
                + "31415926535897932384626433832795028841971693993751"
                + "05820974944592307816406286208998628034825342117067"
                + "98214808651328230664709384460955058223172535940812"
                + "84811174502841027019385211055596446229489549303819"
                + "64428810975665933446128475648233786783165271201909"
                + "14564856692346034861045432664821339360726024914127";
            for (int i = 0; i < s.length(); i++) {
                pi.add(s.charAt(i) - '0');
            }
            for (int i = 0; i < 10; i++) {
                clut.add(Color.getHSBColor(0.6f, i / 10f, 1).getRGB());
            }
            image = new BufferedImage(W, H, BufferedImage.TYPE_INT_ARGB);
            int i = 0;
            for (int row = 0; row < H; row++) {
                for (int col = 0; col < W; col++) {
                    image.setRGB(col, row, clut.get(pi.get(i)));
                    if (++i == pi.size()) {
                        i = 0;
                    }
                }
            }
        }
    
        private static class ClutPanel extends JPanel {
    
            private int i;
    
            public ClutPanel(List<Integer> rgbList) {
                this.setLayout(new GridLayout(1, 0));
                for (Integer rgb : rgbList) {
                    JLabel label = new JLabel(String.valueOf(i++), JLabel.CENTER);
                    label.setOpaque(true);
                    label.setBackground(new Color(rgb));
                    this.add(label);
                }
            }
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    PiRaster pr = new PiRaster();
                    Icon icon = new ImageIcon(pr.image);
                    frame.add(new JLabel(icon), BorderLayout.WEST);
                    frame.add(pr, BorderLayout.CENTER);
                    frame.add(new ClutPanel(pr.clut), BorderLayout.SOUTH);
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some JavaScript code that resizes <div/> elements (adjusts height/width/padding/margin etc.) based on
I have some JavaScript code that looks like: function statechangedPostQuestion() { //alert("statechangedPostQuestion"); if (xmlhttp.readyState==4)
I have some jQuery/JavaScript code that I want to run only when there is
I have some code in a javascript file that needs to send queries back
I have some JavaScript code that does the following link.NavigateUrl = string.Format(javascript:MyFunction({0}, {1});, ID1,
I have some Javascript code that will programmatically register an COM interop assembly by
I have some JavaScript code that works in IE containing the following: myElement.innerText =
I have some javascript code that creates an img tag with a mouseover callback,
I have some Javascript code that creates 2 arrays: One for Product Category and
I have some javascript code that is used in a few different pages in

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.