有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在java applet程序中设置从html文件作为参数传递到矩形的颜色

一个java小程序,用于显示一个矩形,其中指定的坐标和颜色作为参数从HTML文件中传递。 我可以从html文件中检索并设置作为参数传递的矩形的坐标,但我无法从html文件中设置作为参数传递的颜色,如何做到这一点?我试过这个代码,但它不能正常工作

import java.awt.*;
import java.applet.*;
/* <applet code="ParaRect" width=1200 height=1000>
<param name=x value=100>
<param name=y value=200>
<param name=color value="red">
</applet> */
public class ParaRect extends Applet{
    public void paint(Graphics g){ 
       String x=getParameter("color");
       g.setColor(Color.x);
       g.fillRect(Integer.parseInt(getParameter("x")),Integer.parseInt(getParameter("y")),150,100);
    }
}

共 (1) 个答案

  1. # 1 楼答案

       ''' 
        import java.awt.*;
        import java.applet.*;
        /* <applet code="ParaRect" width=1200 height=1000>
            <param name=x value=100>
           <param name=y value=200>
           <param name="color" value="FF0000">
           </applet> */
           public class ParaRect extends Applet{  
           public void paint(Graphics g) {
           Color col =  new 
           Color(Integer.parseInt(this.getParameter("color"),16));
           g.setColor(col);
    

    g.fillRect(Integer.parseInt(getParameter(“x”)),Integer。parseI nt(getParameter(“y”)),150100; }
    }“'