如何修复小程序参数

发布于 2024-12-12 09:16:39 字数 1260 浏览 0 评论 0原文

仅当我移动“setBackground(color);”时,我的程序才能正常工作到“init()”。如何修复它? 我的任务是传入与 RGB 值相对应的三个整数,并使用适当的 Color 构造函数来设置背景颜色。

import java.applet.*;
import java.awt.*;

public class AnAppletSubclass2b extends Applet {
int r;
int g;
int b;
Color color;
public void init()
{

    String parmStringRED =  getParameter("red");
     r = Integer.parseInt(parmStringRED);
    String parmStringGREEN =  getParameter("green");
     g = Integer.parseInt(parmStringGREEN);
    String parmStringBLUE =  getParameter("blue");
     b = Integer.parseInt(parmStringBLUE);
    Color color = new Color (r,g,b);

    System.out.println("The parameter are: red: " + parmStringRED + ", green: " + parmStringGREEN + ", blue: " + parmStringBLUE);


}

public void paint(Graphics g)
{
    setBackground(color);
    System.out.println("In paint: n = " + n);
    n++;
}

int n;
}
<HTML>
<HEAD>
    <TITLE> AnApplet With Parms </TITLE>
</HEAD>
<BODY>
<p>Here is the output of my  Program:</p>   
<APPLET CODE="AnAppletSubclass2b.class" WIDTH=150 HEIGHT=100>
<PARAM name="red" value="25" >      
<PARAM name="green" value="250">
<PARAM name="blue" value="120">
</APPLET>
</BODY>
</HTML>

my program works fine only if i move "setBackground(color);" to "init()". how to fix it ?
my task is to Pass in three integers corresponding to an RGB value and use the appropriate Color constructor to set the background color.

import java.applet.*;
import java.awt.*;

public class AnAppletSubclass2b extends Applet {
int r;
int g;
int b;
Color color;
public void init()
{

    String parmStringRED =  getParameter("red");
     r = Integer.parseInt(parmStringRED);
    String parmStringGREEN =  getParameter("green");
     g = Integer.parseInt(parmStringGREEN);
    String parmStringBLUE =  getParameter("blue");
     b = Integer.parseInt(parmStringBLUE);
    Color color = new Color (r,g,b);

    System.out.println("The parameter are: red: " + parmStringRED + ", green: " + parmStringGREEN + ", blue: " + parmStringBLUE);


}

public void paint(Graphics g)
{
    setBackground(color);
    System.out.println("In paint: n = " + n);
    n++;
}

int n;
}
<HTML>
<HEAD>
    <TITLE> AnApplet With Parms </TITLE>
</HEAD>
<BODY>
<p>Here is the output of my  Program:</p>   
<APPLET CODE="AnAppletSubclass2b.class" WIDTH=150 HEIGHT=100>
<PARAM name="red" value="25" >      
<PARAM name="green" value="250">
<PARAM name="blue" value="120">
</APPLET>
</BODY>
</HTML>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2024-12-19 09:16:39

您有一个颜色变量定义为字段,另一个定义为局部变量。更改

Color color = new Color (r,g,b);

color = new Color (r,g,b);

you have a color variable defined as a field and another one defined as a local. Change

Color color = new Color (r,g,b);

to

color = new Color (r,g,b);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文