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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:25:36+00:00 2026-05-14T07:25:36+00:00

When i run this code the paintComponent method is not being called It may

  • 0

When i run this code the paintComponent method is not being called
It may be very simple error but i dont know why this, plz.

package test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

 class   Userboard extends JPanel 
{
    static BufferedImage image;
    String shape;
    Point start;
    Point end;
    Point mp;
    String selected;
    int ex,ey;//eraser
    int w,h;
    public Userboard() 
    {        

        setOpaque(false);
        System.out.println("paper");
        setBackground(Color.white);

        setBorder(BorderFactory.createLineBorder(Color.black));         
    }
    public  void paintComponent(Graphics g) 
    {
           System.out.println("userboard-paint");
        try
        {

            //g.drawImage(image, 0, 0, this);
            Graphics2D g2 = (Graphics2D)g;
            g2.setPaint(Color.black);
            if(start!=null && end!=null)
            {
                if(selected==("elipse"))
                {
                    System.out.println("userboard-elipse");
                    g2.drawOval(start.x, start.y,(end.x-start.x),(end.y-start.y));
                    System.out.println("userboard-elipse drawn");
                }
                else if(selected==("rect"))
                    g2.drawRect(start.x, start.y, (end.x-start.x),(end.y-start.y));
                else if(selected==("line"))
                    g2.drawLine(start.x,start.y,end.x,end.y);
            }           
        }
            catch(Exception e)
            {}
    }
    //Function to draw the shape on image
    public  void draw()
    {
        System.out.println("Userboard-draw");
        System.out.println(selected);
        System.out.println(start);
        System.out.println(end);
        Graphics2D g2 = image.createGraphics();
        g2.setPaint(Color.black);
        if(start!=null && end!=null)
        {
            if(selected=="line")
                    g2.drawLine(start.x, start.y, end.x, end.y);
            else if(selected=="elipse")
            {
                System.out.println("userboard-elipse");
                g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y));
                System.out.println("userboard-elipse drawn");
            }
            else if(selected=="rect")
                    g2.drawRect(start.x, start.y, (end.x-start.x),(end.y-start.y));
        }
        start=null;
        repaint();
        g2.dispose();
    } 
    //To add the point to the board which is broadcasted by the server  
    public   void addPoint(Point ps,String varname,String shape,String event) 
    {       
        try
        {
            if(end==null)
                end = new Point();
            if(start==null)
                start = new Point();

            if(shape.equals("elipse"))
                this.selected="elipse";
            else if(shape.equals("line"))
                this.selected="line";
            else if(shape.equals("rect"))
                this.selected="rect";
            else if(shape.equals("erase"))
                erase();

            if(end!=null && start!=null)
            {           
                if(varname.equals("end"))
                        end=ps;
                else if(varname.equals("mp"))
                        mp=ps;          
                else if(varname.equals("start"))
                        start=ps;

                if(event.equals("drag"))
                       repaint();
                else if(event.equals("release"))
                        draw();     
            }
              repaint();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
} 
    //Function which provides the erase functionality
    public  void erase() 
    {
        Graphics2D pic=(Graphics2D) image.getGraphics();
        pic.setPaint(Color.white);
        if(start!=null)
        pic.fillRect(start.x, start.y, 10, 10);
    }

    //To set the size of the image

    public void setWidth(int x,int y)
    {
        System.out.println("("+x+","+y+")");
        w=x;
        h=y;
        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    }
    //Function to add buttons into the panel, calling this function returns a panel  

}

code that instantiate the userboard class

public class Client extends Thread 
{
    Point point;
    Paper paper;  
    Userboard userboard;

    DatagramSocket datasocket=null;
    public Client(DatagramSocket datasocket)
    {
            this.datasocket=datasocket;
    }

    //This function is to create the JFrame 
    public void createFrame()
    {
        JLayeredPane layerpane=new JLayeredPane();
         JFrame frame=new JFrame("Whiteboard");
         paper= new Paper(datasocket); 
         userboard=new Userboard();
        frame.setLayout(new BorderLayout());
        layerpane.setLayout(new BorderLayout());


        layerpane.add(paper,BorderLayout.CENTER);
        layerpane.add(userboard,BorderLayout.CENTER);//Panel where remote user draws 

        frame.add(paper.addButtons(),BorderLayout.WEST);
        frame.add(layerpane,BorderLayout.CENTER);


         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         frame.setSize(640, 480);
         frame.addWindowListener(new WindowAdapter() 
         {
             public void windowOpened(WindowEvent e) {}
             public void windowClosing(WindowEvent e) 
             {
                 Draw draw=new Draw();
                draw. close();
             }          
         });
         paper.setWidth(frame.getWidth(),frame.getHeight());
         userboard.setWidth(frame.getWidth(),frame.getHeight());
         frame.setVisible(true);
    }

    /*
     * This function is overridden from the thread class
     * This function listens for incoming packets from the server 
     * which contains the points drawn  by the other client
    */
    public void run () 
    {       
            while (true) 
            {
                try 
                {
                    byte[] buffer = new byte[512];
                    DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
                    datasocket.receive(packet);
                    InputStream in=new ByteArrayInputStream(packet.getData(), packet.getOffset(),packet.getLength());
                    DataInputStream din=new DataInputStream(in);              
                    int x=din.readInt();
                    int y=din.readInt();
                    String varname=din.readLine();
                    String var[]=varname.split("-",3);
                    point=new Point(x,y);
                    userboard.addPoint(point, var[0], var[1],var[2]);                   
              }
                catch (IOException ex) 
                {
                    ex.printStackTrace();
                }
        }       
    }

    //This function is to broadcast the newly drawn point to the server 
    public   void  broadcast (Point p,String varname,String shape,String event) 
    {
        try
        {
            ByteArrayOutputStream baos=new ByteArrayOutputStream();
            DataOutputStream dos=new DataOutputStream(baos);
            dos.writeInt(p.x);
            dos.writeInt(p.y);
            dos.writeBytes(varname);
            dos.writeBytes("-");
            dos.writeBytes(shape);
            dos.writeBytes("-");
            dos.writeBytes(event);
            dos.close();
            byte[]data=baos.toByteArray();
            InetAddress ip=InetAddress.getByName("10.123.97.125");
            DatagramPacket packet=new DatagramPacket(data, data.length,ip , 8002);
            datasocket.send(packet);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

}
  • 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-14T07:25:36+00:00Added an answer on May 14, 2026 at 7:25 am

    I think you are adding your two panels to your layered pane the wrong way.

    I recommend you read the tutorial for JLayeredPane.

    When adding to a JLayeredPane, you shouldn’t give the layout hint (irrelevant in this case), but the id of the layer you want to add your component on.

    From the tutorial:

    for (int i = 0; i < ...number of labels...; i++) {
        JLabel label = createColoredLabel(...);
        layeredPane.add(label, new Integer(i));
        ...
    }
    

    In your case, you probably want something like that:

    layerpane.add( paper,     new Integer(0));
    layerpane.add( userboard, new Integer(1));//Panel where remote user draws 
    

    I don’t guarantee that this is the only problem, but if there is indeed something wrong with the layer, it could explain why your UserBoard.paintComponent() method is never called.

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

Sidebar

Ask A Question

Stats

  • Questions 386k
  • Answers 386k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer So the answer to this question was: the WSDL does… May 14, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer Yes, you can. Simply call the OpenGL ES 1.0 APIs.… May 14, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer This is how we organize our solution. Following would be… May 14, 2026 at 11:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.