Nicholas Barrera
Nicholas Barrera

Reputation: 1

How do you have a java applet repeat itself

I have a Graphics lab in java that runs a applet with 7 different "slides" with the click of the mouse. After the seventh slide I need to find out how to run my whole applet over again from the first slide. I think it has something to do with a While loop or the repaint method but am unsure< Help!

Here is my code for a better

// Nicholas Barrera
// May 15, 2017
//GraphicsLab09st.java
// This is the student, starting file for Graphics Lab 08.
// This file is identical to Java1520.java.
// This slide show is about Mr. Leon Schram.
// Each student needs to alter this presentation, 
// add pictures, add pages, add graphics and make it about him/herself.


import java.awt.*;


public class GraphicsLab09st extends java.applet.Applet
{

    int numClicks;
    Image picture1, picture2, picture3, picture4, picture5, picture6, picture7, picture8, picture9, picture10, picture11, picture12, picture13;

    public void init()
    {
        numClicks = 0;
        picture1 = getImage(getDocumentBase(),"RegularShowSunglasses.jpg"); 
        picture2 = getImage(getDocumentBase(),"film-production.jpg");   
        picture3 = getImage(getDocumentBase(),"los-angeles.jpg");               
        picture4 = getImage(getDocumentBase(),"SanAntonio.jpg");    
        picture5 = getImage(getDocumentBase(),"Brandeis.jpg");
        picture6 = getImage(getDocumentBase(),"sound-cloud.png");
        picture7 = getImage(getDocumentBase(),"studio.jpg");
        picture8 = getImage(getDocumentBase(),"StrangerThings.jpg");
        picture9 = getImage(getDocumentBase(),"MazeRunnerBooks.jpg");   
        picture10 = getImage(getDocumentBase(),"rubiks.jpg");       
        picture11 = getImage(getDocumentBase(),"CaliLongboarding.jpg"); 
        picture12 = getImage(getDocumentBase(),"Productive.jpg");   
        picture13 = getImage(getDocumentBase(),"Netflix_Logo.jpg");                                         
    }                                           


    public void paint(Graphics g)
    {
        switch (numClicks)
        {
            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;

        }   
    }

    public boolean mouseDown(Event e, int x, int y)
    {
        numClicks++;
        repaint();
        return true;
    }

    //SLIDE 1
    public void page1(Graphics g)
    {
        Expo.setColor(g,Expo.cyan);
        g.fillRect(0,0,1000,650);
        Expo.setColor(g,Expo.black);
        g.setFont(new Font("Arial",Font.BOLD,95));
        g.drawString("About Me",218,100);
        g.setFont(new Font("Arial",Font.BOLD,50));
        g.drawString("Nick Barrera",50,275);
        Expo.setColor(g,Expo.red);
        Expo.fillStar(g,510,350,175,8);
        Expo.setColor(g,Expo.yellow);
        Expo.drawThickStar(g,510,350,225,8,25);
        Expo.setColor(g,Expo.blue);
        Expo.drawThickStar(g,510,350,275,8,25);
        g.drawImage(picture1,400,250,this);
        g.setFont(new Font("Times Roman",Font.PLAIN,20)); 
        g.drawString("My name is",50,200);      
        g.drawString("Click once to continue.",750,580);    
    }



    //SLIDE 2
    public void page2(Graphics g)
    {
        Expo.setColor(g,Expo.gold);
        g.fillRect(0,0,1000,650);
        Expo.setColor(g,Expo.black);
        g.setFont(new Font("Algerian",Font.BOLD,100));
        g.drawString("PAGE 2",200,100);
        Expo.setColor(g,Expo.blue);
        Expo.fillRectangle(g,100,250,940,500);
        Expo.setColor(g,Expo.chartreuse);
    //  Expo.drawThickOval(g,260,375,100,150,20);
        Expo.drawThickRectangle(g,15,205,590,590,20);
        g.drawImage(picture3,25,215,this);
        g.drawImage(picture4,615,280,this);
        Expo.setColor(g,Expo.blue);
        g.setFont(new Font("Times Roman",Font.PLAIN,20)); 
        g.drawString("I was born in the Los Angeles, CA and moved to San Antonio, TX in 2005.",100,190); 
        g.drawString("Click once to continue.",750,580);    
    }



    //SLIDE 3
    public void page3(Graphics g)
    {
        Expo.setColor(g,Expo.magenta);
        g.fillRect(0,0,1000,650);
        Expo.setColor(g,Expo.black);
        g.setFont(new Font("Impact",Font.BOLD,100));
        g.drawString("PAGE 3",200,100);
        Expo.setColor(g,Expo.darkBlue);
        Expo.fillRoundedRectangle(g,460,220,960,580,50);
        Expo.drawRandomBurst(g,268,394,185,2500);
        g.drawImage(picture5,150,285,this);
        g.drawImage(picture2,520,240,this);
        g.setFont(new Font("Times Roman",Font.PLAIN,20)); 
        g.drawString("I am a juinior at Brandeis high school and intested in studying film production in college",100,150);
        g.drawString("and becoming a Hollywood Producer/Music Artist in Los Angles, California is my dream.",100,175);   
        g.drawString("Click once to continue.",750,600);
    }



    //SLIDE 4
    public void page4(Graphics g)
    {
        Expo.setColor(g,Expo.darkGreen);
        g.fillRect(0,0,1000,650);
        Expo.setColor(g,Expo.black);
        g.setFont(new Font("SansSeriff",Font.BOLD,100));
        g.drawString("PAGE 4",200,100);
        g.drawImage(picture6,40,200,this);
        g.drawImage(picture7, 500,200,this);
        Expo.setColor(g,Expo.black);
        g.setFont(new Font("Times Roman",Font.PLAIN,20)); 
        g.drawString("The studio I recorded at is called The Living Room Studios.",420,530);
        g.drawString("My group name is The Infinity Collective or ICE.",50,480);    
        g.drawString("I also hope to do something in music and I actually have recorded two songs and",75,150); 
        g.drawString("have them on soundcloud, a database that shares many artist's music.",75,175);
        Expo.setColor(g,Expo.blue);
        g.drawString("Click once to continue.",750,580);    
    }



    //SLIDE 5
    public void page5(Graphics g)
    {
        Expo.setColor(g,Expo.black);
        g.fillRect(0,0,1000,650);
        Expo.setColor(g,Expo.red);
        g.setFont(new Font("Garamond",Font.BOLD,100));
        g.drawString("PAGE 5",200,100);
        g.drawImage(picture8,40,200,this);
        g.drawImage(picture9, 500,200,this);
        Expo.setColor(g,Expo.red);
        g.setFont(new Font("Times Roman",Font.PLAIN,20));
        g.drawString("The Maze Runner series by James Dashner is by far my favorite.",320,530);
        g.drawString("My hobbies are reading books, playing basketball, and watching movies.",75,150);
         Expo.setColor(g,Expo.blue);
        g.drawString("Click once to continue.",750,580); 
    }   



    //SLIDE 6
    public void page6(Graphics g)
    {
        Expo.setColor(g,Expo.tan);
        g.fillRect(0,0,1000,650);
        Expo.setColor(g,Expo.black);
        g.setFont(new Font("Algerian",Font.BOLD,100));
        g.drawString("PAGE 6",200,100);
        g.setFont(new Font("Times Roman",Font.PLAIN,20));
        g.drawString("My other interest are playing video games, skating and challenging my mind to think with puzzles.",55,150);
        g.drawImage(picture10,75,200,this);
        g.drawString("I can solve a Rubik's cube in under 2 minutes.",45,500);
        g.drawString("I love longboarding more than doing actual skate tricks.",500,520);
        g.drawImage(picture11,470,200,this);
        Expo.setColor(g,Expo.blue);
        g.setFont(new Font("Times Roman",Font.PLAIN,20));
        g.drawString("Click once to continue.",750,580); 
    }   



    //SLIDE 7   
    public void page7(Graphics g)
    {
        Expo.setColor(g,Expo.lightBlue);
        g.fillRect(0,0,1000,650);
        Expo.setColor(g,Expo.black);
        g.setFont(new Font("Bold",Font.BOLD,100));
        g.drawString("PAGE 7",200,100);
        g.setFont(new Font("Times Roman",Font.PLAIN,20));
        g.drawString("Overall I'm a well organized, sporty, and very productive student, who loves fun hobbies and learning new skills",35,150);
        g.drawString("",35,165);
        g.drawImage(picture12,570,185,this);
        g.drawImage(picture13,50,185,this);
        Expo.setColor(g,Expo.blue);
        g.setFont(new Font("Times Roman",Font.PLAIN,20));
        g.drawString("Click once to continue.",750,600); 
    }   
}

Upvotes: 0

Views: 112

Answers (2)

GhostCat
GhostCat

Reputation: 140573

There are two simple solutions:

  • as already suggested: simply reset your click counter
  • instead of doing switch (numClicks) you could do use the modulo operation, in order to determine: is the counter a multiple of 1, 2, 3, 4, 5, or 6, and so on. For a multiple of 6, you call page7() ... and so on.

But: as so often, the real issue here is: UI examples are not a good way to learn Java. You see, you are lacking basic skills in Java!

Example: you are duplicating code all over the place. Your 7 page() methods are almost identical. You shouldn't worry how to enhance your code, but how to simplify it. Identify common patterns; and move them into specific methods. So that when you want to increase the Times font by 1, you only update 1 method, not 7.

Upvotes: 1

dumptruckman
dumptruckman

Reputation: 114

If you're just trying to go back to the first slide, all you need is something that will reset numClicks to 0 when it is greater than the number of slides.

For example:

public boolean mouseDown(Event e, int x, int y)
{
    numClicks++;
    repaint();
    if (numClicks == 6) {
        numClicks = 0;
    }
    return true;
}

This perhaps isn't the best way to do it, but it should get the job done.

Upvotes: 2

Related Questions