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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:07:56+00:00 2026-06-03T04:07:56+00:00

I got a JPanel where a triangle is painted. When someone clicks on a

  • 0

I got a JPanel where a triangle is painted.
When someone clicks on a button the triangle should be repainted with new parameters. The problem is the old triangle is still there and the new one is messed up with part of the textfield underneath.

public class Vermessung {   

private static void eingabe(){
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(screen.height/2, screen.height/4*3);
    JPanel jp = new JPanel();
    jp.setLayout(new BoxLayout(jp, BoxLayout.PAGE_AXIS));

    //Eingabebereich
    JPanel eingabebereich = new JPanel(new GridLayout(3, 1));
    JPanel abc = new JPanel(new GridLayout(4, 2));
    abc.add(new JLabel("Strecke"));
    abc.add(new JLabel("Gemessener Wert in [m]"));
    abc.add(new JLabel("a:"));
    abc.add(tfa);
    abc.add(new JLabel("b:"));
    abc.add(tfb);
    abc.add(new JLabel("c:"));
    abc.add(tfc);

    //AusgabeBereich
    JPanel ausgabe = new JPanel(new GridLayout(2, 3));
    ausgabe.add(new JLabel("p [m]"));
    ausgabe.add(new JLabel("q [m]"));
    ausgabe.add(new JLabel("h [m]"));
    ausgabe.add(P);
    ausgabe.add(Q);
    ausgabe.add(H);
    P.setEditable(false);
    Q.setEditable(false);
    H.setEditable(false);

    //Buttons mit Listenern
    JPanel buttons = new JPanel(new FlowLayout());
    JButton ok = new JButton("OK"); 
    JButton cancel = new JButton("beenden");

    ActionListener al = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            //Textfelder auslesen
            TextfelderAuslesen();

            //bei gueltiger Eingabe Höhe Berechnen
            if(berechenbar){
                berechnungPQ();
                berechnungH();
                P.setText(String.valueOf(p));
                Q.setText(String.valueOf(q));
                H.setText(String.valueOf(h));
                sketch.update(vec);
sketch.repaint();
            }else{

            }

        }
    };
    ok.addActionListener(al);

    ActionListener beenden = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);

        }
    };
    cancel.addActionListener(beenden);

    buttons.add(ok);
    buttons.add(cancel);

    //Fensteraufbau
    sketch.setPreferredSize(new Dimension(screen.height/2, screen.height/2));
    jp.add(sketch);
    eingabebereich.add(abc);
    eingabebereich.add(ausgabe);
    eingabebereich.add(buttons);
    eingabebereich.setPreferredSize(new Dimension(screen.height/4, screen.height/4));
    jp.add(eingabebereich);
    f.add(jp);
    f.setVisible(true);
}


}

public class Zeichnung extends JPanel{

public void paint(Graphics g){
    zeichneDreieck(g);
}

private void zeichneDreieck(Graphics g){
    berechneLaengen();
    g.setColor(new Color(255,0,0));
    g.drawLine(30, 30, ca, 30);
    g.drawString("c", ca/2, 20);
    g.drawLine(ca, 30, qa, ha);
    g.drawString("a", (ca-pa/2), ha/2);
    g.drawLine(qa, ha, 30, 30);
    g.drawString("b", (qa/2), ha/2);
    g.setColor(new Color(0,0,0));
    g.drawLine(qa, ha, qa, 30);
    g.drawString("h", qa+5, ha/2);
}
}
  • 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-03T04:07:57+00:00Added an answer on June 3, 2026 at 4:07 am
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        zeichneDreieck(g);
    }
    

    For JPanel, override paintComponent() instead. As the first line, call the super method.

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

Sidebar

Related Questions

I've got the following button... public void actionPerformed(ActionEvent arg0) { Contacts contact = new
I've got GridLayout-JPanel. In every cell there is a JLabel with some String. How
I've got a problem when trying to drag a JPanel. If I implement it
I have one problem with my jPanel. I have a button which PNG image
I've got a JPanel class called Board with a static subclass, MouseHanlder, which tracks
Got a requirement to rebuild mssql full-text index. Problem is - I need to
Got a strange problem created a little stored proc which need to execute a
Got a math calculation problem. $a = 34.56 $b = 34.55 $a do some
Got a window.history.go(1) to stop the user hitting the back button, but was wondering
I'm coding a prototype, but got problems with the GUI. I want the JPanel

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.