This button will only appear if you move your mouse over it. See here:
http://mitch9654.zymichost.com/Java/index.html

You hava to move the mouse around the top left

my applet is this

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Main;

import java.applet.Applet;
import Buttons.AddButton;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Mitch
 */
public class Main extends Applet {
boolean firstrun = true;
AddButton add;
buttonhandler addlistener;

@Override
    public void init() {
    setLayout(null);
    addlistener = new buttonhandler();
    add = new AddButton("AddSongs", 0, 0, 40, 40);
    add.setBounds(50, 90, 40, 40);
    repaint(50, 90, 40, 40);
    add(add);
    add.addActionListener(addlistener);
}



    @Override
public void paint(Graphics g) {
resize(200, 200);
}
private static class buttonhandler implements ActionListener {
     public void actionPerformed(ActionEvent e) {
         System.exit(0);
      }
}
}

My button is this:

package Buttons;

import java.awt.*;
import javax.swing.JButton;
/**
 *
 * @author Mitch
 */
public class AddButton extends JButton {
Color background;
Color cross;
int inx;
int iny;
int width;
int height;


    public AddButton(String label, int inxtemp, int inytemp, int widthtemp, int heighttemp) {
        super(label);
        inx = inxtemp;
        iny = inytemp;
        width = widthtemp;
        height = heighttemp;
        System.out.println("Hi");
        repaint(inx, iny, width, height);
        System.out.println("bye");
    }

@Override
    protected void paintComponent(Graphics g) {
    System.out.println("Beginning");
    System.out.println(inx);
    System.out.println(iny);
    System.out.println(width);
    System.out.println(height);
    g.setColor(Color.BLACK);
    g.drawRect(inx, iny, width, height);
        resize(width, height);
        cross = new Color(200, 255, 200);
        background = new Color(0, 255, 0);
        g.setColor(background);
        g.fillRect(inx, iny, width, height);
        DrawCross(g, inx, iny, width, height, cross);
        g.setColor(Color.BLACK);

    }
    protected void DrawCross(Graphics g, int inx, int iny, int width, int height, Color cross) {
        int dimensionswidth[] = new int[3];
        int dimensionsheight[] = new int[3];
        int xc[] = new int[3];
        int yc[] = new int[3];
        dimensionswidth[0] = width / 3;
        dimensionsheight[0] = height / 3;
        dimensionswidth[1] = width / 3;
        dimensionsheight[1] = height / 3;
        dimensionswidth[2] = width / 3;
        dimensionsheight[2] = height / 3;
        xc[0] = inx;
        yc[0] = iny;
        xc[1] = width / 3 + xc[0];
        yc[1] = height / 3 + yc[0];
        xc[2] = width / 3 * 2 + xc[0];
        yc[2] = height / 3 * 2 + yc[0];
        if (width % 3 == 1) {
           dimensionswidth[2] += 1;
        }
        else if (width % 3 == 2) {
            dimensionswidth[1] += 1;
            dimensionswidth[2] += 1;
            xc[2] += 1;
        }
        if (height % 3 == 1) {
           dimensionsheight[2] += 1;
        }
        else if (height % 3 == 2) {
            dimensionsheight[1] += 1;
            dimensionsheight[2] += 1;
            yc[2] += 1;
        }
        g.setColor(cross);
        drawing:
        for(int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
              if ((x + y) % 2 == 1 || (x == 1 && y == 1)) {
                  g.fillRect(xc[x], yc[y], dimensionswidth[x], dimensionsheight[y]);
              }

            }
        }
    return;
    }
}

Recommended Answers

All 8 Replies

In class Main implement MouseMotionListener interface
Check, where mouse is inside the Rectangle(50, 90, 40, 40)
Make button visible depending on this value

In class Main implement MouseMotionListener interface
Check, where mouse is inside the Rectangle(50, 90, 40, 40)
Make button visible depending on this value

No, I don't want it to appear on mousemotion. I want it to appear automatically...

I added this:

g.drawRect(50, 90, 40, 40);

to the main method to show you where the button is. I also updated the site. You have probably noticed that it only appears when you mouse over it... I want it to appear on startup! (as soon as the button object is constructed) but it WON'T go! I don't want it to be delayed... That is what I have been trying to do without delay (haha, what a lame play on words by me:) )


Thanks for helping,
Mitch

@Override
    public void paint(Graphics g) {
        super.paint(g); ///  <-- lame laughs here
        resize(200, 200);
    }
commented: Sweet! thanks for the super.paint(g); solution!!! +1
@Override
    public void paint(Graphics g) {
        super.paint(g); ///  <-- lame laughs here
        resize(200, 200);
    }

Sweet! Thanks for the intense solution! How does it work? I rooted around the applets family tree and the actual paint() method dated back to the Container class. I looked at the code for paint() and wasn't able to figure out what it does!:$ Could you please tell me?

Awesome, Thanks,
Mitch

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.