我的 Paint 方法是 运行 两次,我不知道为什么。我该如何解决这个问题,有人知道为什么会这样吗?
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?
正在使用的 Expo class 基本上只是一个快捷方式,如果有人想要 运行 我的代码,我可以 link 它。问题是只有用户可以点击的一个方块应该让他们前进,并且随机地,让你前进的方块会改变。所以在第一页上它是[失败][失败][通过],而下一页,也应该是[失败][失败][通过],是[通过][通过][通过]。
编辑:世博会 class 实际上对 post 来说太长了,这里有一个 link 可以查看 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;
}
}
}
实际上,当系统需要重新绘制 window 时,paint() 会在任何时候被调用。您可以调整大小、移动或隐藏 window。 paint() 每次都会被调用。将另一个 window 拖到你的上面,你会看到调用了多少次 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);
}
}
这就是想法 - 将逻辑移至控制器 - 在您的情况下为 mouselistener。
这仅是页码的示例。文本也应该这样做。所有文本都应该在模型中,一般来说,您应该只有一种绘制方法可以从您的模型中绘制文本。所以,这些 pageN() 方法也应该消失了。
正在使用的 Expo class 基本上只是一个快捷方式,如果有人想要 运行 我的代码,我可以 link 它。问题是只有用户可以点击的一个方块应该让他们前进,并且随机地,让你前进的方块会改变。所以在第一页上它是[失败][失败][通过],而下一页,也应该是[失败][失败][通过],是[通过][通过][通过]。
编辑:世博会 class 实际上对 post 来说太长了,这里有一个 link 可以查看 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;
}
}
}
实际上,当系统需要重新绘制 window 时,paint() 会在任何时候被调用。您可以调整大小、移动或隐藏 window。 paint() 每次都会被调用。将另一个 window 拖到你的上面,你会看到调用了多少次 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);
}
}
这就是想法 - 将逻辑移至控制器 - 在您的情况下为 mouselistener。 这仅是页码的示例。文本也应该这样做。所有文本都应该在模型中,一般来说,您应该只有一种绘制方法可以从您的模型中绘制文本。所以,这些 pageN() 方法也应该消失了。