首页 > 解决方案 > 如何将作为参数从 html 文件传递​​到 java applet 程序中的矩形的颜色设置

问题描述

一个 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);
    }
}

标签: java

解决方案


   ''' 
    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.parseInt(getParameter("y")),150,100); }
} '''


推荐阅读