Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的绘画方法运行了两次,我不知道为什么。我如何解决这个问题,有人知道为什么会发生这种情况吗?_Java_Paint - Fatal编程技术网

Java 我的绘画方法运行了两次,我不知道为什么。我如何解决这个问题,有人知道为什么会发生这种情况吗?

Java 我的绘画方法运行了两次,我不知道为什么。我如何解决这个问题,有人知道为什么会发生这种情况吗?,java,paint,Java,Paint,正在使用的Expo类基本上只是一个快捷方式,如果有人想运行我的代码,我可以链接它。问题是,用户只能点击一个方块来推进它们,并且,随机地,推进你的方块会改变。所以第一页是[Fail][Fail][Pass],下一页也应该是[Fail][Fail][Pass],是[Pass][Pass][Pass] 编辑:世博会课程实际上太长了,不能在这里发布,但这里有一个链接可以查看 实际上,当系统需要重新绘制窗口时,会随时调用paint()。您可以调整窗口大小、移动或隐藏窗口。每次都会调用paint()。将另一

正在使用的Expo类基本上只是一个快捷方式,如果有人想运行我的代码,我可以链接它。问题是,用户只能点击一个方块来推进它们,并且,随机地,推进你的方块会改变。所以第一页是[Fail][Fail][Pass],下一页也应该是[Fail][Fail][Pass],是[Pass][Pass][Pass]

编辑:世博会课程实际上太长了,不能在这里发布,但这里有一个链接可以查看

实际上,当系统需要重新绘制窗口时,会随时调用paint()。您可以调整窗口大小、移动或隐藏窗口。每次都会调用paint()。将另一个窗口拖到您的窗口上,您将看到调用paint()的次数

所以,不要使用任何逻辑。使用图案并将您的绘画与逻辑分开

特别是-在模型中保留颜色,并根据鼠标单击进行更改。然后,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);
    }

}
在您的案例中,这就是将逻辑移动到控制器鼠标侦听器的想法。
这只是pagenumber的示例。文本也应如此。所有文本都应该在模型中,一般来说,您应该只有一种从模型中绘制文本的绘制方法。所以,这些pageN()方法也应该消失。

不幸的是,我的Java不是很流利,如果你能添加一个例子和/或把你说的话记下来,那会有很大帮助。非常感谢你,伙计,我真的无法表达我的感激之情。
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);
    }

}