import javax.swing.*;
import java.awt.*;
import java.io.IOException;
class ColoreRectangle
{
private int width;
private int height;
private int x;
private int y;
private Color colorone;
private JFrame windows;
ColoreRectangle(int w,int h, int x,int y)
{
width=w;
height=h;
this.x=x;
this.y=y;
colorone=Color.BLUE;
windows=new JFrame("ColoreRectangle");
windows.setSize(width,height);
windows.setVisible(true);
}
void paint()
{
Graphics g=windows.getGraphics();
g.setColor(colorone);
g.fillRect(x, y, (width/10), (height/10));}}
public class Color {
public static void main(String [] args) throws IOException
{
ColoreRectangle one=new ColoreRectangle(200,200,50,50);
System.in.read();
one.paint();
}
}
以上是程序的内容,运行后出现以下提示
The method setColor(Color) in the type Graphics is not applicable for the arguments (Color) at ColoreRectangle.<init>(Color.java:20)
at Color.main(Color.java:38)
请高手解答一下这是什么原因,
多谢
import java.awt.*;
import java.io.IOException;
class ColoreRectangle
{
private int width;
private int height;
private int x;
private int y;
private Color colorone;
private JFrame windows;
ColoreRectangle(int w,int h, int x,int y)
{
width=w;
height=h;
this.x=x;
this.y=y;
colorone=Color.BLUE;
windows=new JFrame("ColoreRectangle");
windows.setSize(width,height);
windows.setVisible(true);
}
void paint()
{
Graphics g=windows.getGraphics();
g.setColor(colorone);
g.fillRect(x, y, (width/10), (height/10));}}
public class Color {
public static void main(String [] args) throws IOException
{
ColoreRectangle one=new ColoreRectangle(200,200,50,50);
System.in.read();
one.paint();
}
}
以上是程序的内容,运行后出现以下提示
The method setColor(Color) in the type Graphics is not applicable for the arguments (Color) at ColoreRectangle.<init>(Color.java:20)
at Color.main(Color.java:38)
请高手解答一下这是什么原因,
多谢
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
class ColoreRectangle
{
private int width;
private int height;
private int x;
private int y;
private Color colorone;
private JFrame windows;
ColoreRectangle(int w,int h, int x,int y)
{
width=w;
height=h;
this.x=x;
this.y=y;
colorone=Color.BLUE;
windows=new JFrame("ColoreRectangle");
windows.setSize(width,height);
windows.setVisible(true);}
void paint()
{
Graphics g=windows.getGraphics();
g.setColor(colorone);
g.fillRect(x, y, (width/10), (height/10));}}
public class MyColor {
public static void main(String [] args) throws IOException
{
ColoreRectangle one=new ColoreRectangle(200,200,50,50);
System.in.read();
one.paint();}
}
用的时候要
java.awt.Color很麻烦,所以不要和jdk类库重名,另外引包的时候不要使用
.*这样最容易出楼主现在的问题,出错不知道原因,
在eclipse引入类的快捷键是ctrl+shift+o
或者选中要引入的类比如选中Color后ctrl+shift+m会自动添加import java.awt.Color;
支持这种改法,或者也可以明确地指定你想用那个Color如:
private Color colorone; ==> private java.awt.Color colorone;colorone=Color.BLUE; ==> colorone=java.awt.Color.BLUE;
像下面一样:
支持这种改法,或者也可以明确地指定你想用那个Color如:
private Color colorone; ==> private java.awt.Color colorone; colorone=Color.BLUE; ==> colorone=java.awt.Color.BLUE; 看看我这个图片的验证码
package com.rand;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.*;
import java.awt.image.BufferedImage;import java.util.Random;
import javax.imageio.ImageIO;
/***
*date:2008-5-12;
*author:East(张栋芳)
****/public class RandomCodeServlet extends HttpServlet{
private int width = 60;//验证码的宽度
private int height = 20;//验证码的高度
protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphics2D g = buffImg.createGraphics();
//产生Random
Random random = new Random();
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
//字体
Font font = new Font("Dialog",Font.PLAIN,18);
g.setFont(font);
//边框
g.setColor(Color.BLACK);
g.drawRect(0,0,width-1,height-1);
//产生160条干扰线,图像中的认证码,不被看到
g.setColor(Color.GRAY);
for(int i=0;i<160;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
g.drawLine(x,y,x1,y1);
}
StringBuffer randomCode = new StringBuffer();//保存验证码
int red = 0,green = 0,blue = 0;
for(int i = 0;i < 4;i++){
String strRand=String.valueOf(random.nextInt(10));
//Color
red = random.nextInt(110);
green = random.nextInt(50);
blue = random.nextInt(50);
//set Color
g.setColor(new Color(red,green,blue));
g.drawString(strRand,13*i+6,16);
randomCode.append(strRand);//保存验证码
}
//搞验证码 到SESSION中去
HttpSession session=req.getSession();
session.setAttribute("randomCode",randomCode.toString());
//禁止图像缓存
resp.setHeader("Pragma","no-cache");
resp.setHeader("Cache-Control","no-cache");
resp.setDateHeader("Expires",0);
resp.setContentType("image/jpeg");
//将图像输出到Servlet输出 流中
ServletOutputStream sos=resp.getOutputStream();
ImageIO.write(buffImg,"jpeg",sos);
sos.close();
}
}
<head>
<script type="text/javascript">
function test(){
alert("this si test........");
}
</script>
</head>
<body onload="test()">
</body>
</html>
.*这样最容易出楼主现在的问题,出错不知道原因,
在eclipse引入类的快捷键是ctrl+shift+o
或者选中要引入的类比如选中Color后ctrl+shift+m会自动添加import java.awt.Color;
private Color colorone;
系统不知道用你的好,还是用自己的好了。呵呵