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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:49:01+00:00 2026-06-10T15:49:01+00:00

This is the JFrame package client.connection; import java.awt.Dimension; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import javax.swing.JFrame;

  • 0

This is the JFrame

package client.connection;

import java.awt.Dimension;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JFrame;


class DrawFrameRemoteControl extends JFrame
{
    private DrawPanelRemoteControl imagePanel;
    private ClientRemoteControlConnection clientRemoteControlConnection;
    private ObjectInputStream clientInputStream;
    private ObjectOutputStream clientOutputStream;
    private Dimension imageDimension;
    private Dimension serverDimension;

    public DrawFrameRemoteControl(Dimension imageDimension,ClientRemoteControlConnection clientRemoteControlConnection,ObjectInputStream clientInputStream,ObjectOutputStream clientOutputStream,Dimension serverDimension)
    {
        super("Remote Desktop Control");

        this.clientRemoteControlConnection=clientRemoteControlConnection;
        this.clientInputStream=clientInputStream;
        this.clientOutputStream=clientOutputStream;
        this.imageDimension=imageDimension;
        this.serverDimension=serverDimension;

        imagePanel=new DrawPanelRemoteControl(imageDimension);
        add(imagePanel);


        setSize(imageDimension.width,imageDimension.height);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);
    }

    void drawNewImageGrayscale(byte[] array)
    {
        imagePanel.setNewImageGrayscale(array);
        imagePanel.repaint();
    }
}

And this is the extended JPanel class-

package client.connection;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.MemoryImageSource;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

class DrawPanelRemoteControl extends JPanel
{
    private byte[] byteArray=null;
    private Image image;
    private JLabel imageLabel=new JLabel();
    private Dimension imageDimension;

    public DrawPanelRemoteControl(Dimension imageDimension)
    {
        this.imageDimension=imageDimension;
        add(imageLabel);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        System.out.println(".");
        if(byteArray!=null)
        {
            image=getGrayscaleImageFromArray(byteArray,imageDimension.width,imageDimension.height);
            imageLabel.setIcon(new ImageIcon(image));
        }
    }

    private Image getGrayscaleImageFromArray(byte[] buffer, int width, int height)
    {
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        int[] nBits = { 8 };
        ColorModel cm = new ComponentColorModel(cs, nBits, false, true,Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        SampleModel sm = cm.createCompatibleSampleModel(width, height);
        DataBufferByte db = new DataBufferByte(buffer, width * height);
        WritableRaster raster = Raster.createWritableRaster(sm, db, null);
        BufferedImage result = new BufferedImage(cm, raster, false, null);
        return result;
    }

    void setNewImageGrayscale(byte[] array)
    {
        this.byteArray=array;
        this.intArray=null;
    }
}

I have tried debugging the code, even though imagePanel.repaint() is being executed many times, the program never reaches the paintComponent() method of DrawPanelRemoteControl class.

Can anybody give me any idea why this might be happening? Has it got anything to do with the imageDimension object?

Additional Information : In main() method, a DrawFrameRemoteControl object is created and it’s drawNewImageGrayscale(byte[] arr) method is being updated from main() every second.

  • 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-06-10T15:49:03+00:00Added an answer on June 10, 2026 at 3:49 pm

    It’s not clear why you’re passing around byte[], but it looks like you want to update a component’s Icon with a gray thumbnail. The example below creates grayscale icons from existing sample icons and uses setIcon() to do the update. A similar approach works with any Image. See also this example that suggests ColorConvertOp, and this example that updates whole components rather than icons.

    Gray Icons Aqua

    Gray Icons Nimbus

    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Graphics2D;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.awt.image.ColorConvertOp;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JToggleButton;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    
    /**
     * @see https://stackoverflow.com/a/12228640/230513
     * @see https://stackoverflow.com/a/7935040/230513
     */
    public class GrayIcons extends JPanel {
    
        private List<Icon> list = new ArrayList<Icon>();
        private List<JToggleButton> buttons = new ArrayList<JToggleButton>();
        private Timer timer = new Timer(1000, new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                update();
            }
        });
    
        public GrayIcons() {
            this.setLayout(new GridLayout(1, 0));
            list.add(getGray(UIManager.getIcon("OptionPane.errorIcon")));
            list.add(getGray(UIManager.getIcon("OptionPane.informationIcon")));
            list.add(getGray(UIManager.getIcon("OptionPane.warningIcon")));
            list.add(getGray(UIManager.getIcon("OptionPane.questionIcon")));
            for (Icon icon : list) {
                JToggleButton jtb = new JToggleButton(icon);
                buttons.add(jtb);
                this.add(jtb);
            }
            timer.start();
        }
    
        private void update() {
            Collections.shuffle(list);
            int index = 0;
            for (JToggleButton b : buttons) {
                b.setIcon(list.get(index++));
            }
        }
    
        /**
         * @see https://stackoverflow.com/q/5830533/230513
         * @see https://stackoverflow.com/a/3106550/230513
         */
        private Icon getGray(Icon icon) {
            final int w = icon.getIconWidth();
            final int h = icon.getIconHeight();
            GraphicsEnvironment ge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            BufferedImage image = gc.createCompatibleImage(w, h);
            Graphics2D g2d = image.createGraphics();
            g2d.setPaint(new Color(0x00f0f0f0));
            g2d.fillRect(0, 0, w, h);
            icon.paintIcon(null, g2d, 0, 0);
            BufferedImage gray = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
            ColorConvertOp op = new ColorConvertOp(
                image.getColorModel().getColorSpace(),
                gray.getColorModel().getColorSpace(), null);
            op.filter(image, gray);
            return new ImageIcon(gray);
        }
    
        private void display() {
            JFrame f = new JFrame("GrayIcons");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new GrayIcons().display();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
I have the following code: package example; import java.awt.BorderLayout; import java.awt.FlowLayout; import javax.swing.JFrame; import
I'm trying to print a JFrame using the PrintUtilities Class: package util; import java.awt.*;
package jtextareatest; import java.io.FileInputStream; import java.io.IOException; import javax.swing.*; public class Jtextareatest { public static
Here's my canvas class extending JPanel : package start; import java.awt.Color; import java.awt.Graphics; import
import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; @SuppressWarnings(serial) public class Picture extends JFrame{
package pkg411project; import javax.swing.ButtonGroup; public class CrudMain extends javax.swing.JInternalFrame { public CrudMain() { initComponents();
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have the following Programm package utests; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.ArrayList; import
So i have the following code: package animation; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage;

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.