【问题标题】:How to call 2 different paint methods in the same program?如何在同一个程序中调用 2 种不同的绘制方法?
【发布时间】:2019-11-25 19:00:39
【问题描述】:

我试图做一个简单的猜数字游戏,当用户从实际答案中猜出一个特定范围内的数字时,它会绘制一个不同颜色的矩形。到目前为止,我只是在测试并创建了 2 个绘制方法,现在想知道如何调用方法“paint2”。

import java.awt.*;
import hsa.Console;
import javax.swing.JFrame;
import java.util.Random;
import java.awt.Canvas;
import java.awt.Graphics;

public class MashGuessTheNumber extends Canvas {
    static Console c; // The output console

    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame("My Drawing");
        Canvas canvas = new MashGuessTheNumber();
        canvas.setSize(400, 400);
        frame.getContentPane().add(canvas);
        frame.pack();
        frame.setVisible(true);
        MashGuessTheNumber sm = new MashGuessTheNumber();
        c = new Console();
        int loop = 0;
        while (loop == 0) { // loop used to continue looping the questions after one is answered
            int answer = 0;
            c.println("Welcome to the guess the number game!");
            c.println("What is your name?");
            String name = c.readLine();
            c.print("why, hello there ");
            c.println(name);
            c.println("What diffuculty would you like to play?(easy/medium/hard)");
            String diff = c.readLine();
            if (diff.equalsIgnoreCase("easy")) {
                // *Location of random number generator*
                c.println("So you chose easy, huh");
                c.println("I'm thinking of a number between 1 and 10");
                int guess = 1;
                answer = (int) (Math.random() * ((10 - 1) + 1));
                while (guess != answer) {
                    c.println("What is it?");
                    guess = c.readInt();
                    c.println(answer);
                    if ((((guess - answer) < 3) && ((guess - answer) > 0))
                            || (((answer - guess) < 3) && ((answer - guess) > 0))) {
                        c.println("EXTREMELY HOT");
                    }
                }
                if (guess == answer) {
                    c.println("You did it!");
                }

            }
            if (diff.equalsIgnoreCase("medium")) {
                // *Location of random number generator*
                c.println("So you chose medium, huh");
                c.println("I'm thinking of a number between 1 and 100");
                c.println("What is it?");
                String guess = c.readLine();
                answer = (int) (Math.random() * ((100 - 1) + 1));
            }
            if (diff.equalsIgnoreCase("hard")) {
                // *Location of random number generator*
                c.println("So you chose hard, huh");
                c.println("I'm thinking of a number between 1 and 1000");
                c.println("What is it?");
                String guess = c.readLine();
                answer = (int) (Math.random() * ((1000 - 1) + 1));
            }

            c.println("Would you like to play again?(y/n)"); // if answerd with "y" the loop will repeat
            String cont = c.readLine();
            if (cont.equalsIgnoreCase("y")) {
                loop = 0;
            } else {
                for(int i=1;i<=24;i++){
                    c.println(" ");             
                c.setCursor(12, 30);
                c.println("See you next time. Bye!");
                loop = 1; // Stops the loop and says bye to the user
            }
        }
        // Place your program here. 'c' is the output console
    }

    public void paint(Graphics g) {
        int x[] = { 35, 75, 75, 35 };
        int y[] = { 10, 10, 200, 200 };
        g.setColor(Color.black);
        int numberofpoints = 4;
        g.drawPolygon(x, y, numberofpoints);
    }

    public void paint2(Graphics g) {
        int x[] = { 35, 75, 75, 35 };
        int y[] = { 10, 10, 200, 200 };
        g.setColor(Color.blue);
        int numberofpoints = 4;
        g.drawPolygon(x, y, numberofpoints);
    }
    // main method
} // MashGuessTheNumber class

到目前为止,我只是想在第一个矩形上方绘制一个不同的矩形,如果有另一种不使用两种方法的方法也会有帮助

【问题讨论】:

    标签: java swing graphics jframe paint


    【解决方案1】:

    我根本看不到你在哪里调用paint(),但我会创建第二个参数,它可以作为一个标志,告诉我是绘制蓝色矩形还是黑色矩形。这将摆脱重复的代码。

    public void paint (Graphics g, Color color)
    {
        int x[] = {35, 75, 75, 35};
        int y[] = {10, 10, 200, 200};
        g.setColor (color);
        int numberofpoints = 4;
        g.drawPolygon (x, y, numberofpoints);
    }
    

    我不知道“颜色”是否是用于传递颜色的正确对象,但关键是传递一些可以帮助您选择制作矩形的颜色的东西,这样您就不会不得不写两个非常相似的方法。你会像这样调用paint():

    if(some condition) {
        paint(g, Color.blue);
    }
    else {
        paint(g, Color.black);
    }
    

    【讨论】:

    • @HamiClash 让我知道这是否是您要找的。​​span>
    • 我在尝试调用main方法中的方法,但是不起作用?
    • 你无权访问的paint方法放在哪里了?
    • 在Java Swing框架中,paint方法是由框架调用的,而不是用户调用的。
    【解决方案2】:

    在 Java Swing 框架中,您不需要自己调用 Canvas.paint()。但是,您可以使用自己的实例变量。

    如果您向 MashGuessNumber 类添加实例变量

    private Color myColor = Color.BLACK;
    

    然后修改你的 if 语句

    if (count == answer) {
        System.out.println("You did it1);
        myColor == Color.BLUE;
        repaint();              // already a Canvas method
    }
    

    然后修改你的paint()方法

    public void paint (Graphics g, Color color)
    {
        int x[] = {35, 75, 75, 35};
        int y[] = {10, 10, 200, 200};
        g.setColor (myColor);
        int numberofpoints = 4;
        g.drawPolygon (x, y, numberofpoints);
    }
    

    这应该可以解决问题。

    对 repaint() 的调用告诉 Swing 通过调用您的 paint() 方法来重绘画布。否则系统将无法知道您的 Canvas 需要重新绘制。

    【讨论】:

    • 请问为什么我的回答被否决了?我想改进我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 2013-06-02
    相关资源
    最近更新 更多