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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:51:42+00:00 2026-06-14T04:51:42+00:00

I’ve been trying to figure out what is wrong with my code here. The

  • 0

I’ve been trying to figure out what is wrong with my code here. The idea is to create a small Paint program and to have red, green, blue, and clear buttons. I have everything that I can think of for it to work, but can’t figure out what is wrong with the code. The Program opens, and immediately closes.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Paint{

    public static void main(String[] args){
            gui g = new gui();
            g.setVisible(true);

    }

}

 public class gui extends JComponent implements ActionListener{
    JButton red, green, blue, clear;
    Image image;
    Graphics2D draw;
    int x, y, prevX, prevY;

    gui(){
            JFrame frame = new JFrame("Paint");
            Container content = frame.getContentPane();
            content.setLayout(new BorderLayout());
            setDoubleBuffered(false);

            JPanel panel = new JPanel();
            content.add(panel, BorderLayout.SOUTH);
            panel.setPreferredSize(new Dimension(32, 68));
            panel.setMinimumSize(new Dimension(32, 68));
            panel.setMaximumSize(new Dimension(32, 68));


            red = new JButton("Red");
            green = new JButton("Green");
            blue = new JButton("Blue");
            clear = new JButton("Clear");

            red.setPreferredSize(new Dimension(50, 16));
            green.setPreferredSize(new Dimension(50,16));
            blue.setPreferredSize(new Dimension(50, 16));

            panel.add(red);
            panel.add(green);
            panel.add(blue);
            panel.add(clear);

            red.addActionListener(this);
            green.addActionListener(this);
            blue.addActionListener(this);
            clear.addActionListener(this);

            frame.setSize(500, 500);


            addMouseListener(new MouseAdapter(){
                    public void mousePressed(MouseEvent e){
                            prevX = e.getX();
                            prevY = e.getY();
                    }

            });

            addMouseMotionListener(new MouseMotionAdapter(){
                    public void mouseDragged(MouseEvent e){
                            x = e.getX();
                            y = e.getY();
                            draw.drawLine(prevX, prevY, x, y);
                            repaint();
                            prevX = x;
                            prevY = y;

                    }


            });

    }


    public void paintComponent(Graphics g){

           if(image==null){
                    image = createImage(getSize().width, getSize().height);

                    draw = (Graphics2D)image.getGraphics();

                    draw.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                    draw.setPaint(Color.white);
                    draw.fillRect(0, 0, getSize().width, getSize().height);
                    draw.setPaint(Color.black);
                    repaint();
           }

            g.drawImage(image, 0, 0, null);

    }


    public void actionPerformed(ActionEvent e) {
        if( e.getSource()==red){
            draw.setPaint(Color.red);
            repaint();
        }
        if( e.getSource()==green){
            draw.setPaint(Color.green);
            repaint();
        }
        if( e.getSource()==blue){
            draw.setPaint(Color.blue);
            repaint();
        }
        if( e.getSource()==clear){
            draw.setPaint(Color.white);
            draw.fillRect(0, 0, getSize().width, getSize().height);
            draw.setPaint(Color.black);
            repaint();
        }


    }


 }
  • 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-14T04:51:43+00:00Added an answer on June 14, 2026 at 4:51 am

    your have to use the visibility properties with frame and panel as well like

    frame.setVisible(true);
    

    line g.setVisible(true); is not working for you as you have extended your class jcomponent and your are using frame and not setting its property to set it visible.

    Same problem will occure with your panel so your have to set its property as well i-e

    panel.setVisible(true);
    

    Here is your full code that is working after adding these properties

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Paint{
    
        public static void main(String[] args){
                gui g = new gui();
                g.setVisible(true);
    
        }
    
    }
    
     class gui extends JComponent implements ActionListener{
        JButton red, green, blue, clear;
        Image image;
        Graphics2D draw;
        int x, y, prevX, prevY;
    
        gui(){
                JFrame frame = new JFrame("Paint");
                Container content = frame.getContentPane();
                content.setLayout(new BorderLayout());
                setDoubleBuffered(false);
    
                JPanel panel = new JPanel();
                content.add(panel, BorderLayout.SOUTH);
                panel.setPreferredSize(new Dimension(32, 68));
                panel.setMinimumSize(new Dimension(32, 68));
                panel.setMaximumSize(new Dimension(32, 68));
    
    
                red = new JButton("Red");
                green = new JButton("Green");
                blue = new JButton("Blue");
                clear = new JButton("Clear");
    
                red.setPreferredSize(new Dimension(50, 16));
                green.setPreferredSize(new Dimension(50,16));
                blue.setPreferredSize(new Dimension(50, 16));
    
                panel.add(red);
                panel.add(green);
                panel.add(blue);
                panel.add(clear);
    
                panel.setVisible(true);
    
                red.addActionListener(this);
                green.addActionListener(this);
                blue.addActionListener(this);
                clear.addActionListener(this);
    
                frame.setSize(500, 500);
    
                frame.setVisible(true)            ;
    
    
                addMouseListener(new MouseAdapter(){
                        public void mousePressed(MouseEvent e){
                                prevX = e.getX();
                                prevY = e.getY();
                        }
    
                });
    
                addMouseMotionListener(new MouseMotionAdapter(){
                        public void mouseDragged(MouseEvent e){
                                x = e.getX();
                                y = e.getY();
                                draw.drawLine(prevX, prevY, x, y);
                                repaint();
                                prevX = x;
                                prevY = y;
    
                        }
    
    
                });
    
        }
    
    
        public void paintComponent(Graphics g){
    
               if(image==null){
                        image = createImage(getSize().width, getSize().height);
    
                        draw = (Graphics2D)image.getGraphics();
    
                        draw.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
                        draw.setPaint(Color.white);
                        draw.fillRect(0, 0, getSize().width, getSize().height);
                        draw.setPaint(Color.black);
                        repaint();
               }
    
                g.drawImage(image, 0, 0, null);
    
        }
    
    
        public void actionPerformed(ActionEvent e) {
            if( e.getSource()==red){
                draw.setPaint(Color.red);
                repaint();
            }
            if( e.getSource()==green){
                draw.setPaint(Color.green);
                repaint();
            }
            if( e.getSource()==blue){
                draw.setPaint(Color.blue);
                repaint();
            }
            if( e.getSource()==clear){
                draw.setPaint(Color.white);
                draw.fillRect(0, 0, getSize().width, getSize().height);
                draw.setPaint(Color.black);
                repaint();
            }
    
    
        }
    
    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an array which has BIG numbers and small numbers in it. I
I'm trying to create an if statement in PHP that prevents a single post

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.