liboan1997
liboan1997

Reputation: 53

Adding a Swing button to a program that draws with AWT

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

}
}

Upvotes: 3

Views: 234

Answers (1)

Reimeus
Reimeus

Reputation: 159754

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.

Upvotes: 6

Related Questions