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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:54:58+00:00 2026-06-13T17:54:58+00:00

I have a program that draws a random triangle. I wanted to add a

  • 0

I have a program that draws a random triangle. I wanted to add a button that, upon clicking, would erase and regenerate another random triangle. I tested to see if I could make a button show up in another program, SwingButtonTest.java, (This is how I roll, I’m learning) and it succeeded.

Then, I essentially copied all the code I thought necessary to make a button show up from that program to my triangle program. Unfortunately the program does not display the button…

Once again, any help is appreciated, thanks a ton!

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


@SuppressWarnings({ "serial" })
public class TriangleApplet extends JApplet implements ActionListener {

   int width, height;
   int[] coords = new int[6];
   JButton refresh;

   public void init() {
      //This stuff was added from SwingButtonTest.java
      Container container = getContentPane();
      container.setLayout(null);

        container.setLayout(null);

        refresh= new JButton("I like trains");
        refresh.setBackground(Color.yellow);
        refresh.setSize(140, 20);
        refresh.addActionListener(this);

        container.add(refresh);
      //End of SwingButtonTest.java stuff

      coords = randomTri();
   }


    public static int[] randomTri() {
        int[] pointArray = new int[6];
        int x;
        for (int i = 0; i < 6; i++) {
            x = ((int)(Math.random()*40)*10);
            pointArray[i] = x;
        }
        return pointArray;
    }


   public void paint( Graphics g ) {
        g.setColor(Color.BLUE);

        g.drawLine(coords[0], coords[1], coords[2], coords[3]);
        g.drawString("A: " + coords[0]/10 + ", " + coords[1]/10, coords[0], coords[1]);
        g.drawLine(coords[2], coords[3], coords[4], coords[5]);
        g.drawString("B: " + coords[2]/10 + ", " + coords[3]/10, coords[2], coords[3]);
        g.drawLine(coords[4], coords[5], coords[0], coords[1]);
        g.drawString("C: " + coords[4]/10 + ", " + coords[5]/10, coords[4], coords[5]);
        //Math for AB
        int abXDif = Math.abs(coords[0]/10 - coords[2]/10);
        int abYDif = Math.abs(coords[1]/10 - coords[3]/10);
        int abLength = (abXDif*abXDif) + (abYDif*abYDif);
        g.drawString("AB: Sqrt of "+ abLength, 0, 10);
        //Math for BC
        int bcXDif = Math.abs(coords[2]/10 - coords[4]/10);
        int bcYDif = Math.abs(coords[3]/10 - coords[5]/10);
        int bcLength = (bcXDif*bcXDif) + (bcYDif*bcYDif);
        g.drawString("BC: Sqrt of "+ bcLength, 0, 20);
        //Math for AC
        int acXDif = Math.abs(coords[4]/10 - coords[0]/10);
        int acYDif = Math.abs(coords[5]/10 - coords[1]/10);
        int acLength = (acXDif*acXDif) + (acYDif*acYDif);
        g.drawString("AC: Sqrt of "+ acLength, 0, 30);


   }

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}
}
  • 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-13T17:54:59+00:00Added an answer on June 13, 2026 at 5:54 pm

    The reason the button is not appearing is that you don’t call:

    super.paint(g);
    

    in the paint() method. This call will cause the button and other added components to be painted.

    A better approach to custom painting in Swing is to place your custom painting in components that extend JComponent and override paintComponent to take advantage of Swing’s optimized painting model.

    Consider also using a layout manager to size & place your components. Avoid using absolute positioning (null layout). From Doing Without a Layout Manager:

    Although it is possible to do without a layout manager, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container’s changing size, and to different locales.

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

Sidebar

Related Questions

I have a program that draws some vector graphics using System.Drawing and the Graphics
i have a program that create the circle that move to random position with
I have a program that draws shapes on an image. I have a separate
I have a program (a fractal) that draws lines in an interlaced order. Originally,
I have a program that reads and draws an SVG file in a java
I have a shader that draws particles with solid colors. I would like to
I have a program that draws the contents of a QGraphicsScene to a PNG
I have created a simple program that draws shapes within command prompt. I am
I have my program that can draw rectangles. I have two problems I can't
I have program that requires Python 3, but I develop Django and it uses

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.