0

I am working on a Main Menu for a game and whenever the applet starts running it draws the applet 4 times before starting the program itself it keeps repainting it i added textures to the program so i don't know whats the problem

public class MainMenu extends Applet implements MouseListener, Runnable {

    int xpos;
    int ypos;

    private Image menu = null;
    private Image play = null;
    private Image exit = null;
    int rect1xco, rect1yco, rect1width, rect1height;
    int rect2xco, rect2yco, rect2width, rect2height;

    boolean mouseEntered;    
    boolean rect1Clicked;
    boolean rect2Clicked;

    public void init() {    
        rect1xco = 590;
        rect1yco = 300;
        rect1width = 150;
        rect1height = 70;    
        rect2xco = 590;
        rect2yco = 400;
        rect2width = 150;
        rect2height = 70;    
        addMouseListener(this);
    }

public void paint(Graphics g) {
        this.setSize(1350, 650);    
        if (menu == null)
            menu = getImage("menu.JPG");
        if (play == null)
            play = getImage("Button.png");
        if (exit == null)
            exit = getImage("Button.png");    
        g.drawImage(menu, 0, 0, getWidth(), getHeight(), this);
        g.drawImage(play, 590, 300, 150, 70, this);
        g.drawImage(exit, 590, 400, 150, 70, this);    
        Font Blockt = new Font("INFECTED", Font.ITALIC, 200);
        g.setFont(Blockt);
        g.setColor(Color.yellow);
        g.drawString("DX BALL", 360, 200);    
        g.getFontMetrics();
        Font DIGITAL = new Font("Blockt", Font.ITALIC, 30);
        g.setFont(DIGITAL);
        g.setColor(Color.black);
        g.drawString("Play", 635, 340);
        g.drawString("Exit", 635, 440);

        if (rect1Clicked) {

        }

        if (rect2Clicked) {
            System.exit(1);
        }    
    }
    public void mousePressed(MouseEvent me) {
        if (rect1Clicked == true) {
            repaint();
            play = getImage("ButtonClicked.png");    
        }
        if (rect2Clicked == true) {
            repaint();
            exit = getImage("ButtonClicked.png");
        }
    }

    public void mouseClicked(MouseEvent me) {    
        xpos = me.getX();
        ypos = me.getY();
        rect1Clicked = false;
        if (xpos > rect1xco && xpos < rect1xco + rect1width && ypos > rect1yco
                && ypos < rect1yco + rect1height) {
            rect1Clicked = true;    
            System.out.println("Start button pressed"); // start the game    
        } else
            rect1Clicked = false;
        if (xpos > rect2xco && xpos < rect2xco + rect1width && ypos > rect2yco
                && ypos < rect2yco + rect2height)
            rect2Clicked = true;    
        else
            rect2Clicked = false;    
    }

    public void mouseReleased(MouseEvent me) {    
        play = getImage("Button.png");
        exit = getImage("Button.png");
        repaint();    
    }

    public void mouseEntered(MouseEvent me) {    
        mouseEntered = true;    
    }

    public void mouseExited(MouseEvent me) {    
        mouseEntered = false;    
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
james
  • 23
  • 4
  • Never set sizes, load images or create fonts within a `paint()` method! These should be done when the class is constructed and thereafter cached. (Actually, the call to set the applet size should not be done **at all* given this is an applet that gets a size from the HTML that loads it. – Andrew Thompson Dec 27 '14 at 02:12
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Dec 27 '14 at 02:12
  • Oh,,, and no applet can do this.. `if (rect2Clicked) { System.exit(1);`. But then, an app. that exited due to a mouse event(?) is hardly ending unexpectedly, so the value provided to the exit method seems wrong. – Andrew Thompson Dec 27 '14 at 02:15

0 Answers0