javapaint

My Paint method is running twice and I have no idea why. How can I fix this, and does anyone know why this is happening?


The Expo class being used is just a shortcut basically, I can link it if anyone wants to run my code. The issue is that only one square the user can click is supposed to progress them and, randomly, the square that progresses you changes. So on page one it's [Fail][Fail][Pass], and the next page, where it is also supposed to be [Fail][Fail][Pass], is [Pass][Pass][Pass].

EDIT: The Expo class is actually too long to post in this, here is a link to view it though Link

import java.awt.*;
import java.applet.Applet;


public class BarryViper extends java.applet.Applet
   {
   Rectangle top, mid, bot;
   int numColor;
   int pageNum;

   public void init()
      {
      top = new Rectangle(100,75,150,150);
      mid = new Rectangle(100,275,150,150);
      bot = new Rectangle(100,475,150,150);
      numColor = 0;
      pageNum = 0;
      }

   public void paint(Graphics g)
      {

      switch (pageNum)
         {
         case 0 : page1(g); break;
         case 1 : page2(g); break;
         case 2 : page3(g); break;
         case 3 : page4(g); break;
         // case 4 : page5(g); break;
      //          case 5 : page6(g); break;
      //          case 6 : page7(g); break;
      //          case 7 : page8(g); break;
      //          case 8 : page9(g); break;
      //          case 9 : page10(g); break;
         }
      }


   public boolean mouseDown(Event e, int x, int y)
      {
      if(top.inside(x,y)) 
         numColor = 1;
      else if(mid.inside(x,y)) 
         numColor = 2;
      else if(bot.inside(x,y)) 
         numColor = 3;        
      else
         numColor = 0;
      repaint();
      return true;

      }


   public void page1(Graphics g)
      {

      Expo.setBackground(g,0);
      Expo.setColor(g,Expo.white);
      Expo.fillRectangle(g,100,75,250,225);
      Expo.fillRectangle(g,100,275,250,425);
      Expo.fillRectangle(g,100,475,250,625);
      Expo.setFont(g,"Arial",Font.BOLD,20);
      Expo.drawString(g,"-You Have been Tasked with slaying the dragon that has forsaken these lands.",260,75);
      Expo.drawString(g,"-This quest will be long, difficult, and full of Monty Python refrences.",260,95);
      Expo.drawString(g,"-Do you accept this task?",260,115);
      Expo.setColor(g,Expo.red);
      Expo.setFont(g,"Arial",Font.BOLD,15);
      Expo.drawString(g,"Nah",155,155);
      Expo.drawString(g,"I Prefer",145,350);
      Expo.drawString(g,"Monty Java",130,370);
      Expo.drawString(g,"Sure I could",130,540);
      Expo.drawString(g,"waste some time",115,560);
      switch (numColor)
         {
         case 0 : pageNum=0; break;
         case 1 : pageNum=0; break;
         case 2 : pageNum=0; break;
         case 3 : pageNum=1; break;

         }   

      }

   public void page2(Graphics g)
      {

      Expo.setBackground(g,0);
      Expo.setColor(g,Expo.white);
      Expo.fillRectangle(g,100,75,250,225);
      Expo.fillRectangle(g,100,275,250,425);
      Expo.fillRectangle(g,100,475,250,625);
      Expo.setFont(g,"Arial",Font.BOLD,20);
      Expo.drawString(g,"-Good Choice! You now decide to roam down an old dirt road, when suddenly",260,75);
      Expo.drawString(g,"-You encounter a group of three Goblins blocking your path",260,95);
      Expo.drawString(g,"-What is your reaction?",260,115);
      Expo.setColor(g,Expo.red);
      Expo.setFont(g,"Arial",Font.BOLD,15);
      Expo.drawString(g,"Hide from them ",115,155);
      Expo.drawString(g,"Try talking",130,350);
      Expo.drawString(g,"To them",130,370);
      Expo.drawString(g,"Attack them",130,540);
      Expo.drawString(g,"immediately",130,560);
      switch (numColor)
         {
         case 0 : pageNum=0; break;
         case 1 : pageNum=0; break;
         case 2 : pageNum=0; break;
         case 3 : pageNum=2; break;

         }     


      }

   public void page3(Graphics g)
      {

      Expo.setBackground(g,0);
      Expo.setColor(g,Expo.white);
      Expo.fillRectangle(g,100,75,250,225);
      Expo.fillRectangle(g,100,275,250,425);
      Expo.fillRectangle(g,100,475,250,625);
      Expo.setFont(g,"Arial",Font.BOLD,75);
      Expo.drawString(g,"PAGE 3",200,75);
      switch (numColor)
         {
         case 0 : pageNum=0; break;
         case 1 : pageNum=0; break;
         case 2 : pageNum=0; break;
         case 3 : pageNum=3; break;

         }    

      }

   public void page4(Graphics g)
      {

      Expo.setBackground(g,0);
      Expo.setColor(g,Expo.white);
      Expo.fillRectangle(g,100,75,250,225);
      Expo.fillRectangle(g,100,275,250,425);
      Expo.fillRectangle(g,100,475,250,625);
      Expo.setFont(g,"Arial",Font.BOLD,75);
      Expo.drawString(g,"PAGE 4",200,75);
      switch (numColor)
         {
         case 0 : pageNum=0; break;
         case 1 : pageNum=0; break;
         case 2 : pageNum=0; break;
         case 3 : pageNum=4; break;

         }    


      }







   }

Solution

  • Actually paint() is invoked any time when system needs to repaint the window. You can resize, move or hide window. paint() is called every time. Drag another window over yours and you will see how many times paint() is called.

    So, don't use paint fr any logic. Use MVC pattern and separate your painting from logic.

    In particular - keep your color in your model and change it based on mouse click. Then paint() should use color based current value but not decide what color it is.

    If you want to see code - here it is:

    import java.awt.*;
    import java.applet.Applet;
    
    public class BarryViper extends java.applet.Applet {
        Rectangle top, mid, bot;
        int numColor;
        int pageNum;
    
        public void init() {
            top = new Rectangle(100, 75, 150, 150);
            mid = new Rectangle(100, 275, 150, 150);
            bot = new Rectangle(100, 475, 150, 150);
            numColor = 0;
            pageNum = 0;
        }
    
        public void paint(Graphics g) {
    
            switch (pageNum) {
            case 0:
                page1(g);
                break;
            case 1:
                page2(g);
                break;
            case 2:
                page3(g);
                break;
            case 3:
                page4(g);
                break;
            }
        }
    
        public boolean mouseDown(Event e, int x, int y) {
            if (top.inside(x, y))
                numColor = 1;
            else if (mid.inside(x, y))
                numColor = 2;
            else if (bot.inside(x, y))
                numColor = 3;
            else
                numColor = 0;
    
            switch (numColor) {
            case 0:
                pageNum = 0;
                break;
            case 1:
                pageNum = 0;
                break;
            case 2:
                pageNum = 0;
                break;
            case 3:
                pageNum = pageNum;
                break;
            }
    
            repaint();
            return true;
    
        }
    
        public void page1(Graphics g) {
    
            Expo.setBackground(g, 0);
            Expo.setColor(g, Expo.white);
            Expo.fillRectangle(g, 100, 75, 250, 225);
            Expo.fillRectangle(g, 100, 275, 250, 425);
            Expo.fillRectangle(g, 100, 475, 250, 625);
            Expo.setFont(g, "Arial", Font.BOLD, 20);
            Expo.drawString(
                    g,
                    "-You Have been Tasked with slaying the dragon that has forsaken these lands.",
                    260, 75);
            Expo.drawString(
                    g,
                    "-This quest will be long, difficult, and full of Monty Python refrences.",
                    260, 95);
            Expo.drawString(g, "-Do you accept this task?", 260, 115);
            Expo.setColor(g, Expo.red);
            Expo.setFont(g, "Arial", Font.BOLD, 15);
            Expo.drawString(g, "Nah", 155, 155);
            Expo.drawString(g, "I Prefer", 145, 350);
            Expo.drawString(g, "Monty Java", 130, 370);
            Expo.drawString(g, "Sure I could", 130, 540);
            Expo.drawString(g, "waste some time", 115, 560);
        }
    
        public void page2(Graphics g) {
    
            Expo.setBackground(g, 0);
            Expo.setColor(g, Expo.white);
            Expo.fillRectangle(g, 100, 75, 250, 225);
            Expo.fillRectangle(g, 100, 275, 250, 425);
            Expo.fillRectangle(g, 100, 475, 250, 625);
            Expo.setFont(g, "Arial", Font.BOLD, 20);
            Expo.drawString(
                    g,
                    "-Good Choice! You now decide to roam down an old dirt road, when suddenly",
                    260, 75);
            Expo.drawString(g,
                    "-You encounter a group of three Goblins blocking your path",
                    260, 95);
            Expo.drawString(g, "-What is your reaction?", 260, 115);
            Expo.setColor(g, Expo.red);
            Expo.setFont(g, "Arial", Font.BOLD, 15);
            Expo.drawString(g, "Hide from them ", 115, 155);
            Expo.drawString(g, "Try talking", 130, 350);
            Expo.drawString(g, "To them", 130, 370);
            Expo.drawString(g, "Attack them", 130, 540);
            Expo.drawString(g, "immediately", 130, 560);
        }
    
        public void page3(Graphics g) {
    
            Expo.setBackground(g, 0);
            Expo.setColor(g, Expo.white);
            Expo.fillRectangle(g, 100, 75, 250, 225);
            Expo.fillRectangle(g, 100, 275, 250, 425);
            Expo.fillRectangle(g, 100, 475, 250, 625);
            Expo.setFont(g, "Arial", Font.BOLD, 75);
            Expo.drawString(g, "PAGE 3", 200, 75);
        }
    
        public void page4(Graphics g) {
    
            Expo.setBackground(g, 0);
            Expo.setColor(g, Expo.white);
            Expo.fillRectangle(g, 100, 75, 250, 225);
            Expo.fillRectangle(g, 100, 275, 250, 425);
            Expo.fillRectangle(g, 100, 475, 250, 625);
            Expo.setFont(g, "Arial", Font.BOLD, 75);
            Expo.drawString(g, "PAGE 4", 200, 75);
        }
    
    }
    

    This is the idea - move logic to the controller - mouselistener in your case. This is example for pagenumber only. The same should be done for texts. All texts should be in model and, in general, you should have only one paint method which draws texts from your model. So, these pageN() methods should be gone too.