用JAVA编写一个程序,以随机颜色绘制随机长度的直线

用JAVA编写一个程序,以随机颜色绘制随机长度的直线,第1张

gdrawOval(0, 0, 36, 36)

第一对坐标是一个能把你做的图形用最小的面积包围起来的矩形 的左上角的坐标,第二对是这个圆(确切的说是椭圆)的长轴和短轴的长度。

gdrawString("1",36, 36);

所画字体从36,36的位置开始,你想把它放进圆中,就要根据字体高、宽来计算圆的位置和大小。

至于

drawPanelsetBorder(BorderFactorycreateLineBorder(Colorred));

这句是肯定起了作用的,只不过太快了,你还没反映过来。

在把边框设置为红色后,被你画的圆和字符覆盖了,把

public void paint(Graphics g){

gdrawOval(0, 0, 36, 36);

gdrawString("1",36, 36);

}

}

注释掉,你就能看到了。

不知道最终的目的是什么,但比可以寻求另外的解决方案,如嵌套等。

如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!

import javautilArrays;

import javautilScanner;

public class Poker {

    private static final int NUM = 1;

    private static int SUM = 52;

    private static final String P = "A,2,3,4,5,6,7,8,9,10,J,Q,K";

    private static String[][] POKER = {

        PreplaceAll("([^,]+)", "黑桃$1")split(","),

        PreplaceAll("([^,]+)", "红桃$1")split(","),

        PreplaceAll("([^,]+)", "梅花$1")split(","),

        PreplaceAll("([^,]+)", "方片$1")split(",")

    };

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System in );

        int count = NUM;

        String[] hand = new String[0];

        Systemoutprintln("随机发 " + count + " 张牌");

        SUM -= count;

        for (int i = 0; i < count; i++) {

            int row = (int)(Mathrandom()  POKERlength);

            int col = (int)(Mathrandom()  POKER[row]length);

            String[] css = new String[handlength + 1];

            Systemarraycopy(hand, 0, css, 0, handlength);

            css[csslength - 1] = POKER[row][col];

            hand = css;

        }

        Systemoutprintln("然后看手里的牌:");

        Systemoutprintln(ArraystoString(hand));

        scannerclose();

    }

}

packagecomhe;

importjavaxservlet;

importjavaxservlet>

importjavaio;

importjavautil;

importjavaawt;

importjavaawtimage;

importjavaximageio;

publicclassCodeFact

extends>

publicvoiddoGet(request,response)throws

,IOException{

//设置页面不缓存

responsesetHeader("Pragma","No-cache");

responsesetHeader("Cache-Control","no-cache");

response("Expires",0);

//在内存中创建图象

intwidth=60,height=20;

image=new(width,height,TYPE_INT_RGB);

//获取图形上下文

Graphicsg=imagegetGraphics();

//生成随机类

Randomrandom=newRandom();

//设定背景色

gsetColor((200,250));

gfillRect(0,0,width,height);

//设定字体

gsetFont(newFont("TimesNewRoman",FontPLAIN,18));

//画边框

gsetColor(newColor(33,66,99));

gdrawRect(0,0,width-1,height-1);

//随机产生155条干扰线,使图象中的认证码不易被其它程序探测到

gsetColor((160,200));

for(inti=0;i

intx=randomnextInt(width);

inty=randomnextInt(height);

intxl=randomnextInt(12);

intyl=randomnextInt(12);

gdrawLine(x,y,xxl,yyl);

}

//取随机产生的认证码(4位数字)

StringsRand="";

for(inti=0;i

Stringrand=StringvalueOf(randomnextInt(10));

sRand=rand;

//将认证码显示到图象中

gsetColor(newColor(20randomnextInt(110),20randomnextInt(110),20randomnextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成

gdrawString(rand,13i6,16);

}

//将认证码存入SESSION

>

session("rand",sRand);

//图象生效

gdispose();

//输出图象到页面

ImageIOwrite(image,"JPEG",response());

}

publicvoiddoPost(request,response)throws

,IOException{

doGet(request,response);

}

//给定范围获得随机颜色

privateColor(intfc,intbc){

Randomrandom=newRandom();

if(fc

fc=255;

}

if(bc

bc=255;

}

intr=fcrandomnextInt(bc-fc);

intg=fcrandomnextInt(bc-fc);

intb=fcrandomnextInt(bc-fc);

returnnewColor(r,g,b);

}

}

你试试!!

import javaawt;

import javaawtgeomEllipse2D;

import javaawtgeomRectangle2D;

import javautil;

import javautilMapEntry;

import javautilRandom;

import javaxswing;

/

@author Jeky

/

public class RandomShapeDemo extends JFrame {

public RandomShapeDemo() {

super("Random Shape Demo");

shapes = CollectionssynchronizedMap(new HashMap<Shape, Color>());

thissetContentPane(new JPanel() {

@Override

protected void paintComponent(Graphics g) {

superpaintComponent(g);

Graphics2D g2d = (Graphics2D) g;

//画出当前所有生成好的图形

try {

for (Entry<Shape, Color> entry : shapesentrySet()) {

g2dsetColor(entrygetValue());

g2dfill(entrygetKey());

}

} catch (Exception e) {

}

}

});

thissetSize(800, 600);

thissetDefaultCloseOperation(HIDE_ON_CLOSE);

thissetVisible(true);

}

public static void main(String[] args) {

RandomShapeDemo demo = new RandomShapeDemo();

demosetVisible(true);

new RandomFillCircleThread(demo)start();

new RandomFillRectangleThread(demo)start();

}

/

随机生成新的圆形

/

public void fillCircle() {

Random random = new Random();

int x = randomnextInt(800);

int y = randomnextInt(600);

int radius = randomnextInt(100);

//随机颜色

Color c = new Color(randomnextInt(256), randomnextInt(256), randomnextInt(256));

//随机圆形

Shape circle = new Ellipse2DDouble(x, y, radius, radius);

//添加入表中

shapesput(circle, c);

//刷新面板

repaint();

}

/

随机生成新的矩形

/

public void fillRectangle() {

Random random = new Random();

int x = randomnextInt(800);

int y = randomnextInt(600);

int radius = randomnextInt(100);

//随机颜色

Color c = new Color(randomnextInt(256), randomnextInt(256), randomnextInt(256));

//随机圆形

Shape circle = new Rectangle2DDouble(x, y, radius, radius);

//添加入表中

shapesput(circle, c);

//刷新面板

repaint();

}

private static class RandomFillCircleThread extends Thread {

private RandomFillCircleThread(RandomShapeDemo demo) {

thisdemo = demo;

}

@Override

public void run() {

//当demo面板还在显示的时候

while (demoisVisible()) {

//向demo添加新的随机圆形

demofillCircle();

//休眠一段时间

try {

sleep(SLEEP_TIME);

} catch (Exception e) {

eprintStackTrace(Systemerr);

}

}

try {

//释放demo

demodispose();

} catch (Exception e) {

}

}

private static final long SLEEP_TIME = 100L;

private RandomShapeDemo demo;

}

private static class RandomFillRectangleThread extends Thread {

private RandomFillRectangleThread(RandomShapeDemo demo) {

thisdemo = demo;

}

@Override

public void run() {

//当demo面板还在显示的时候

while (demoisVisible()) {

//向demo添加新的随机矩形

demofillRectangle();

//休眠一段时间

try {

sleep(SLEEP_TIME);

} catch (Exception e) {

eprintStackTrace(Systemerr);

}

}

try {

//释放demo

demodispose();

} catch (Exception e) {

}

}

private static final long SLEEP_TIME = 100L;

private RandomShapeDemo demo;

}

private Map<Shape, Color> shapes;

}

以上就是关于用JAVA编写一个程序,以随机颜色绘制随机长度的直线全部的内容,包括:用JAVA编写一个程序,以随机颜色绘制随机长度的直线、用java程序的if else语句模拟从一副52张牌中随机抽出一张牌,程序应该输出牌的花色和大小。、VB识别图片验证码_图片验证码自动识别输入等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/10147660.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存