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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:08:57+00:00 2026-06-18T12:08:57+00:00

As the title says, I’m having a hard time trying to draw some rectangles

  • 0

As the title says, I’m having a hard time trying to draw some rectangles (filled) in JApplet.
The exact goal is to have a 50×50 table and when you click on a targeted cell, to make it filled (possibly done by drawing a filled rectangle). I have done the maths about the coordinates of the starting point, but for some reason I can’t draw the new rectangle in the MouseClicked method. Any suggestions?

public class Main extends JApplet {

public static final int DIMX = 800;
public static final int DIMY = 800;
public static final int ratio = 16;
Graphics g;
boolean drawing;
public int cX;
public int cY;

public Main() {
    JPanel MainFrame = new JPanel();
    MainFrame.setPreferredSize(new Dimension(400, 800));
    MainFrame.setBackground(Color.LIGHT_GRAY);
    JPanel Table = new JPanel();
    Table.setPreferredSize(new Dimension(800, 800));
    Table.setBackground(Color.LIGHT_GRAY);
    add(MainFrame, BorderLayout.EAST);
    add(Table, BorderLayout.WEST);
    addMouseListener(new clicked());
}



public void paint(Graphics g) {
    super.paintComponents(g);
    g.setColor(Color.black);
    for (int i = 0; i <= 800; i += 16) {
        g.drawLine(0, i, 800, i);
        g.drawLine(i, 0, i, 800);
//            g.fillRect(cX, cY, 16, 16);
    }
}

public static void main(String[] args) {
    JFrame win = new JFrame("Retarded Bullshit");
    win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    win.setPreferredSize(new Dimension(1216, 840));
    win.setContentPane(new Main());
    win.pack();
    win.setVisible(true);

}

public class clicked extends JApplet implements MouseListener {

public int cX;
public int cY;
Graphics g;

@Override
public void mouseClicked(MouseEvent e) {
//            Point a = e.getLocationOnScreen();
    int cellX = e.getX();
    int cellY = e.getY();
    if (cellX < 800 && cellX > 0 && cellY < 800 && cellY > 0) {
        cX = cellX / 16 + 1;
        cY = cellY / 16 + 1;

        JOptionPane.showMessageDialog(null, "" + cX + " " + cY);
    }
  • 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-18T12:08:58+00:00Added an answer on June 18, 2026 at 12:08 pm

    This is a relatively simple concept (no offense).

    To start with, don’t mix your code with JApplet and JFrame. If you want to use your application in these two mediums, separate the logic into a separate component (like JPanel) which you can easily add to either. You really shouldn’t add a top level container to another top level container (adding an applet to a frame) – it’s messy.

    Avoid overriding the paint methods of top level containers (like JApplet), instead, use a custom component (like JPanel) instead and override it’s paintComponent method.

    In your example, you should be calling super.paint rather then super.paintComponents. paint does important work, you don’t want to skip it – but you should be using JComponent#paintComponent

    MouseListeners should added to the components that you are interested in managing mouse events. Because clicked is never added to any containers, it will never recieve mouse events.

    Take a look at

    • How to write mouse listeners
    • Performing Custom Painting
    • 2D Graphics
    • Painting in AWT and Swing (because every Swing developer should have an understanding of this)

    enter image description here

    public class SimplePaint03 {
    
        public static void main(String[] args) {
            new SimplePaint03();
        }
    
        public SimplePaint03() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new PaintPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class PaintPane extends JPanel {
    
            private List<Shape> grid;
            private List<Shape> fill;
    
            public PaintPane() {
                grid = new ArrayList<>(5);
                fill = new ArrayList<>(5);
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        for (Shape shape : grid) {
                            if (shape.contains(e.getPoint())) {
                                if (fill.contains(shape)) {
                                    fill.remove(shape);
                                } else {
                                    fill.add(shape);
                                }
                            }
                        }
                        repaint();
                    }
                });
    
                int colWidth = 200 / 50;
                int rowHeight = 200 / 50;
    
                for (int row = 0; row < 50; row++) {
                    for (int col = 0; col < 50; col++) {
                        grid.add(new Rectangle(colWidth * col, rowHeight * row, colWidth, rowHeight));
                    }
                }
    
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); 
                Graphics2D g2d = (Graphics2D) g;
                g2d.setColor(Color.RED);
                for (Shape cell : fill) {
                    g2d.fill(cell);
                }
                g2d.setColor(Color.BLACK);
                for (Shape cell : grid) {
                    g2d.draw(cell);
                }
            }
    
        }
    
    }
    

    Additional

    Information from one paint cycle to another is not maintained. You are required to repaint the component exactly the way you want it to appear. This means you will need to maintain a list of click points that can be repainted at any time.

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

Sidebar

Related Questions

As title says. I am looking for some help with my .htaccess. I have
As title says i have some problems with IE8 and Javascript. It's known about
As title says, I have a list of words, Like stopWords = [the, and,
As title says for some purpose I need to get the size of request/response
As title says, I have access to the current listitem - and it's easy
The title says it mostly. If I have an object in Python and want
The title says enough I think. I have a full quality BufferedImage and I
As title says, im having trouble with my junit tests passing for checking if
The title says it pretty nicely. I have a huge project that uses Makefile.
The title says it: I have an excel Sheet with an column full of

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.