如何在同一程序中调用两种不同的绘制方法?

问题描述 投票:0回答:1

我正在尝试简单地猜数字游戏,当用户从实际答案中猜出特定范围内的数字时,它将绘制一个具有不同颜色的矩形。到目前为止,我只是在测试,已经创建了2个paint方法,现在想知道如何将方法称为“ 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 methods graphics jframe paint
1个回答
0
投票

我根本看不到您在哪里调用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);
}

我不知道'Color'是否是用于传递颜色的正确对象,但重点是传递一些东西,它将帮助您选择制作矩形的颜色,从而不必写两个非常相似的方法。

© www.soinside.com 2019 - 2024. All rights reserved.