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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:08:17+00:00 2026-06-13T18:08:17+00:00

When I run my code, I expect to see a JPanel in my JFrame

  • 0

When I run my code, I expect to see a JPanel in my JFrame, but nothing shows up. I had a button in the frame, and it shows up. But the JPanel doesn’t show up, I even colored it in red.
Here is the code for my JPanel:

import java.awt.*;
import javax.swing.JPanel;
public class graphic extends JPanel {
    private static final long serialVersionUID = -3458717449092499931L;
    public Game game;
    public graphic(Game game){
    this.game = game;
    this.setPreferredSize(new Dimension(400,400));
    this.setBackground(Color.RED);
}
public void paintComponent(Graphics g){
    for (Line l:game.mirrors){
        g.setColor(Color.BLACK);
        g.drawLine(l.start.x, l.start.y, l.end.x, l.end.y);
    }
}
}

And my JFrame code:

import java.awt.Container;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.*;
public class Viewer implements ActionListener {
public JFrame frame;
public JButton drawShoot;
public boolean draw;
public Game game;
public graphic graphic;
public TimerTask timert;
public Timer timer;
public Viewer(){
    draw = true;
    game = new Game();
}
public static void main(String args[]){
    Viewer v = new Viewer();
    v.setup();
}
public void setup(){
    frame = new JFrame("Laser Stimulator");
    drawShoot = new JButton("Edit Mode");
    graphic = new graphic(game);
    graphic.repaint();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(300, 300, 600, 600);
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);
    drawShoot.addActionListener(this);
    timert = new TimerTask() {
        @Override
        public void run() {

        }
    };
    timer =new Timer();
    timer.scheduleAtFixedRate(timert, 0, 1000/30);
    contentPane.add(graphic);
    layout.putConstraint(SpringLayout.NORTH, graphic, 0, SpringLayout.NORTH, contentPane);
    layout.putConstraint(SpringLayout.WEST, graphic, 0, SpringLayout.WEST, contentPane);
    frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource()==drawShoot){
        draw = !draw;
        drawShoot.setText((draw)?"Edit Mode":"Shoot Mode");
    }
}
}
  • 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-13T18:08:19+00:00Added an answer on June 13, 2026 at 6:08 pm

    So the basic problem is you failed to honor the paint chain. You must call super.paintXxx when ever you override on the paint methods…

    This method must call super.paintComponent(g), because it’s responsible for clearing the background 😉

    public void paintComponent(Graphics g) {
        for (Line l : game.mirrors) {
            g.setColor(Color.BLACK);
            g.drawLine(l.start.x, l.start.y, l.end.x, l.end.y);
        }
    }
    

    enter image description here

    public class QuickTestPaintPane {
    
        public static void main(String[] args) {
            new QuickTestPaintPane();
        }
    
        public QuickTestPaintPane() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    Viewer v = new Viewer();
                    v.setup();
    
                }
            });
        }
    
        public class Viewer implements ActionListener {
    
            public JFrame frame;
            public JButton drawShoot;
            public boolean draw;
    //        public Game game;
            public graphic graphic;
            public TimerTask timert;
            public Timer timer;
    
            public Viewer() {
                draw = true;
    //            game = new Game();
            }
    
            public void setup() {
                frame = new JFrame("Laser Stimulator");
                drawShoot = new JButton("Edit Mode");
                graphic = new graphic();
    //            graphic.repaint();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(300, 300, 600, 600);
    //            Container contentPane = frame.getContentPane();
                SpringLayout layout = new SpringLayout();
                frame.setLayout(layout);
                drawShoot.addActionListener(this);
                timert = new TimerTask() {
                    @Override
                    public void run() {
                    }
                };
                timer = new Timer();
                timer.scheduleAtFixedRate(timert, 0, 1000 / 30);
                frame.add(graphic);
                layout.putConstraint(SpringLayout.NORTH, graphic, 0, SpringLayout.NORTH, frame.getContentPane());
                layout.putConstraint(SpringLayout.WEST, graphic, 0, SpringLayout.WEST, frame.getContentPane());
                frame.setVisible(true);
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == drawShoot) {
                    draw = !draw;
                    drawShoot.setText((draw) ? "Edit Mode" : "Shoot Mode");
                }
            }
        }
    
        public class graphic extends JPanel {
    
            private static final long serialVersionUID = -3458717449092499931L;
    //        public Game game;
    
    //        public graphic(Game game) {
            public graphic() {
    //            this.game = game;
                this.setPreferredSize(new Dimension(400, 400));
                this.setBackground(Color.RED);
            }
    
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                Graphics2D g2d = (Graphics2D) g;
                g2d.setColor(Color.BLACK);
                g2d.drawLine(0, 0, getWidth(), getHeight());
    //            for (Line l : game.mirrors) {
    //                g.setColor(Color.BLACK);
    //                g.drawLine(l.start.x, l.start.y, l.end.x, l.end.y);
    //            }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what behaviour can I expect when I run this code: do while(testA) { //
If I run this code as it is, I see a line from the
When I run my code I'm facing the next 2 errors: mysql_num_rows() expects parameter
We run code from adress 8002000 to 80020028 and the question is what is
I want to run code which needs boost libraries. I built it using CMake.
how can i run code when an application ends with asp.net mvc ?
i need run code that will create a database and populate tables. i am
I am trying to run code on several cores (I tried both the snow
I am using to compile and run code from Features2D + Homography to find
How can I check and conditionally only compile / run code if iOS5 is

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.