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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:09:42+00:00 2026-06-03T03:09:42+00:00

i need your help badly because i cannot solve this problem on my own.

  • 0

i need your help badly because i cannot solve this problem on my own.
I am trying to create a GUI and want to draw something in it after pressing a button, but i seem to have some kind of refresh/revalidate or threading issue. The drawing is painted, but when I resize the window, the painting disappears. Also, when moving the window very quickly, parts of the drawing disappear. I have tried many things, but I can’t get this problem to work, maybe you can help me. I was instructed not to write my own code to begin with, but use the NetBeans Design functions to generate Buttons and Panels etc. Maybe this impairs the drawing process/functions, but I don’t know. I’ll post you the relevant code and would be very thankful for suggestions (the outcommented stuff are just artefacts from what I’ve tried before, so don’t mind it):

public class NewJFrame extends JFrame  {
  public NewJFrame() { initComponents(); }

  @SuppressWarnings("unchecked")
  private void initComponents() {
    jButton1 = new javax.swing.JButton();
    jPanel1 = new javax.swing.JPanel();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setMinimumSize(new java.awt.Dimension(1200, 1000));
    jButton1.setText("Draw");
    jButton1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });
    GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGap(0, 1000, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );
    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, 1000, Short.MAX_VALUE)
            .addGap(18, 18, 18)
            .addComponent(jButton1)
            .addGap(33, 33, 33))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 745, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(237, 237, 237))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
    );

    pack();
  }// </editor-fold>

  private void jButton1ActionPerformed(ActionEvent evt) {
    Graphics g = jPanel1.getGraphics();
    draw(jPanel1, g);
  }

  protected void paintComponent(Graphics g){
    jPanel1.setSize(1000, 1000);
    Dimension d = jPanel1.getSize();
    g.setColor(Color.BLACK);
    for (int i=0; i<=1000;i++){
      if (i%100==0){
        g.setColor(Color.RED);
        g.drawLine(i, d.height/2, i, (d.height/2)+100);
      }
      else if(i%50==0 && i%100!=0){
        g.setColor(Color.BLUE);
        g.drawLine(i, d.height/2, i, (d.height/2)+100);
      }
      else {
        g.setColor(Color.BLACK);
        g.drawLine(i, d.height/2, i, (d.height/2)+100);
      }
    }
    g.setColor(Color.green);
    g.drawLine(0, d.height / 2, d.width, d.height / 2);
  }

  public void draw(JPanel Jpanel1, Graphics g) {
    System.out.println("wuffkowski");
    Jpanel1.setSize(1000,1000);
    Dimension d = Jpanel1.getSize();
    g.setColor(Color.BLACK);
    for (int i=0; i<=1000;i++){
      if (i%100==0){
        g.setColor(Color.RED);
        g.drawLine(i, d.height/2, i, (d.height/2)+100);
      }
      else if(i%50==0 && i%100!=0){
        g.setColor(Color.BLUE);
        g.drawLine(i, d.height/2, i, (d.height/2)+100);
      }
      else {
        g.setColor(Color.BLACK);
        g.drawLine(i, d.height/2, i, (d.height/2)+100);
      }
    }
    g.setColor(Color.green);
    g.drawLine(0, d.height / 2, d.width, d.height / 2);
    Jpanel1.paintComponents(g);
  }

  public static void lala () {
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException ex) {
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        NewJFrame JF =new NewJFrame();
        JF.setVisible(true);
      }
    });
  }
  private javax.swing.JButton jButton1;
  private javax.swing.JPanel jPanel1;
}

Again thanks a lot for your precious time.

  • 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-03T03:09:43+00:00Added an answer on June 3, 2026 at 3:09 am

    Java doesn’t remember drawing commands for you; your graphic is rendered once when you click the button because that’s the only time when draw() is called.

    If you want to refresh the rendering after a resize, override paint(Graphics) and call draw() in there.

    If that is related to the button click, you must add fields to your class in which you remember everything that you need in draw() including the fact whether it should draw anything:

    private boolean drawAtAll = false;
    
    private void jButton1ActionPerformed(ActionEvent evt) {
      drawAtAll = true; // ok to draw now
      draw();
    }
    
    @Override
    public void paint(Graphics g) {
       super.paint(g);
       draw();
    }
    
    public void draw() {
      if( !drawAtAll ) return;
    
      Graphics g = jPanel1.getGraphics();
      ...
    }
    

    Further reading:

    • Lesson: Performing Custom Painting
    • Painting in AWT and Swing
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need your help about this issue badly. i am trying to model a
Need your help, trying to change this script: https://github.com/MrHus/jquery-monthly-ical To load external JSON data
I need your help in this particular issue I want to pass the value
I need your help! I want to create a desktop app and a mobile
Need your help figuring this thing out. Scenario: On my JSP, I am trying
Need your help. I'm trying to create a page that has two UI states.
I need your help again :) I'm trying to do a plugin with jQuery
I need your help. I'm trying to install php_apc.dll into my php directory. So
I need your help to translate a query into pyes. This query is working
Hi Java programmers. I badly need your help. I have a JavaFX/Java ME application.

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.