所使用的 Expo 类基本上只是一个快捷方式,如果有人想运行我的代码,我可以链接它。问题是,只有用户可以单击的一个方块才应该让他们前进,并且随机地,让您前进的方块会发生变化。所以在第一页上是[失败][失败][通过],下一页也应该是[失败][失败][通过],是[通过][通过][通过]。
编辑:Expo 课程实际上太长,无法在此发布,这里有一个链接可以查看它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;
}
}
}
实际上当系统需要重新绘制窗口时,paint()就会被调用。您可以调整窗口大小、移动或隐藏窗口。每次都会调用paint()。将另一个窗口拖到您的窗口上,您将看到调用了 Paint() 次数。
所以,不要出于任何逻辑使用绘画。使用 MVC 模式,将绘画与逻辑分开。
特别是 - 将颜色保留在模型中并根据鼠标单击进行更改。然后 Paint() 应该使用基于颜色的当前值,但不决定它是什么颜色。
如果您想查看代码 - 这里是:
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);
}
}
这就是想法 - 将逻辑移动到控制器 - 在您的情况下是鼠标监听器。 这只是页码的示例。对于文本也应该这样做。所有文本都应该在模型中,并且一般来说,您应该只有一种从模型中绘制文本的绘制方法。所以,这些 pageN() 方法也应该消失。
我的学生也遇到了类似的问题,但是将 JDK/JRE 从 jdk1.8.0_11 更新到 jdk1.8.0_291 使问题消失。显然,有了这么多更新,我不知道它是什么时候修复的,但看起来现在好多了。