我在java中有一个图形实验室,它运行一个带有7个不同"幻灯片的小程序"点击鼠标。在第七张幻灯片后,我需要找到如何从第一张幻灯片重新运行我的整个applet。我认为它与While循环或重绘方法有关但不确定<救命啊!
这是我更好的代码
// 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);
}
}
答案 0 :(得分:2)
如果您只是想回到第一张幻灯片,那么只要它大于幻灯片数量,您所需要的就会重置numClicks
到0
。
例如:
public boolean mouseDown(Event e, int x, int y)
{
numClicks++;
repaint();
if (numClicks == 6) {
numClicks = 0;
}
return true;
}
这可能不是最好的方法,但它应该完成工作。
答案 1 :(得分:1)
有两个简单的解决方案:
switch (numClicks)
你可以使用 modulo 操作,以确定:计数器是1,2,3,4,5或6的倍数,等等。对于6的倍数,您可以调用page7()
...依此类推。但是:通常,真正的问题是:UI示例不是学习Java的好方法。你知道,你缺乏Java的基本技能!
示例:您正在复制代码到处。你的7页()方法几乎完全相同。您不应该担心如何增强您的代码,但如何简化它。识别常见的模式;并将它们转换为特定的方法。因此,当您想要将Times字体增加1时,您只更新1个方法,而不是7。