如何在Applet画布上制作彩绘对象? [英] How to make painted objects on Applet canvas blink?

查看:98
本文介绍了如何在Applet画布上制作彩绘对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import java.awt。*; 
import java.applet。*;
public class rain extends Applet
{
int i = 0,j = 0,k = 0;
AudioClip ac;
public void init()
{
ac = getAudioClip(getCodeBase(),rain.wav);
ac.play();
}
public void paint(Graphics g)
{
setBackground(Color.darkGray);

g.setColor(Color.gray);
g.fillOval(100,100,75,25);
g.fillOval(115,115,75,25);
g.fillOval(115,85,75,25);
g.fillOval(165,75,75,25);
g.fillOval(165,125,75,25);
g.fillOval(205,115,75,25);
g.fillOval(205,85,75,25);
g.fillOval(220,100,75,25);
g.fillOval(160,95,80,35);

g.setColor(Color.gray);
g.fillOval(300,100,75,25);
g.fillOval(315,115,75,25);
g.fillOval(315,85,75,25);
g.fillOval(365,75,75,25);
g.fillOval(365,125,75,25);
g.fillOval(405,115,75,25);
g.fillOval(405,85,75,25);
g.fillOval(420,100,75,25);
g.fillOval(360,95,80,35);

g.setColor(Color.gray);
g.fillOval(500,100,75,25);
g.fillOval(515,115,75,25);
g.fillOval(515,85,75,25);
g.fillOval(565,75,75,25);
g.fillOval(565,125,75,25);
g.fillOval(605,115,75,25);
g.fillOval(605,85,75,25);
g.fillOval(620,100,75,25);
g.fillOval(560,95,80,35);

g.setColor(Color.gray);
g.fillOval(700,100,75,25);
g.fillOval(715,115,75,25);
g.fillOval(715,85,75,25);
g.fillOval(765,75,75,25);
g.fillOval(765,125,75,25);
g.fillOval(805,115,75,25);
g.fillOval(805,85,75,25);
g.fillOval(820,100,75,25);
g.fillOval(760,95,80,35);
在这里输入代码
g.setColor(Color.cyan);

for(int i = 0; i< = 820; i = i + 50)
{
g.drawLine(100 + i,160,100 + i,180);
repaint();
}
for(int i = 0; i< = 820; i = i + 50)
{
g.drawLine(100 + i,200,100 + i,220) ;
}
for(int i = 0; i< = 820; i = i + 50)
{
g.drawLine(100 + i,240,100 + i,260) ;
}
for(int i = 0; i< = 820; i = i + 50)
{
g.drawLine(100 + i,280,100 + i,300) ;
}
for(int i = 0; i< = 820; i = i + 50)
{
g.drawLine(100 + i,320,100 + i,340) ;
}
for(int i = 0; i< = 820; i = i + 50)
{
g.drawLine(100 + i,360,100 + i,380) ;
}
for(int i = 0; i< = 820; i = i + 50)
{
g.drawLine(100 + i,400,100 + i,420) ;
}
for(int i = 0; i< = 820; i = i + 50)
{
g.drawLine(100 + i,440,100 + i,460) ;
}
for(int i = 0; i< = 820; i = i + 50)
{
g.drawLine(100 + i,480,100 + i,500) ;
}
for(int i = 0; i< = 820; i = i + 50)
{
g.drawLine(100 + i,520,100 + i,540) ;
}

g.setColor(Color.black);
g.fillRect(0,590,1000,100);

g.setColor(Color.white); (b = b; i <= 900; i = i + 150)
$
$ b $ R $
}

g.setColor(new Color(139,69,19));

g.fillOval(100,600,35,15);
g.fillOval(300,600,30,10);
g.fillOval(500,600,25,10);
g.fillOval(700,600,35,15);
g.fillOval(125,650,35,15);
g.fillOval(175,650,35,15);
g.fillOval(230,670,35,15);
g.fillOval(280,640,35,15);
g.fillOval(340,630,35,9);
g.fillOval(400,650,35,19);
g.fillOval(450,610,35,10);
g.fillOval(500,610,35,18);
g.fillOval(600,675,35,17);
g.fillOval(700,675,25,15);
g.fillOval(710,675,30,15);
g.fillOval(770,623,40,15);
g.fillOval(690,655,25,15);
g.fillOval(830,660,25,15);
g.fillOval(810,670,23,15);
g.fillOval(880,613,29,15);
}
}
/ *< applet code = rain height = 1000 width = 1000>
< / applet> * /

我如何只闪烁for循环部分该计划?我想闪烁各种for循环中包含的行。或者请告诉任何其他方式闪烁程序的一部分..任何帮助将非常感谢。

解决方案

让我们从Applet开始正式弃用,参见

  import java.awt.Color; 
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

公共类测试{

public static void main(String [] args){
new Test();
}

public Test(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}

JFrame frame = new JFrame(Testing);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

公共类TestPane扩展JPanel {

private int count = 0;
private Color lineColor = Color.DARK_GRAY;
private Color [] lineColors = new Color [] {Color.CYAN,Color.DARK_GRAY};

public TestPane(){
Timer timer = new Timer(500,new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
count ++;
lineColor = lineColors [count%2];
repaint();
}
});
timer.start();
}

@Override
public Dimension getPreferredSize(){
return new Dimension(1000,1000);
}

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g.create();
setBackground(Color.darkGray);

g2d.setColor(Color.gray);
g2d.fillOval(100,100,75,25);
g2d.fillOval(115,115,75,25);
g2d.fillOval(115,85,75,25);
g2d.fillOval(165,75,75,25);
g2d.fillOval(165,125,75,25);
g2d.fillOval(205,115,75,25);
g2d.fillOval(205,85,75,25);
g2d.fillOval(220,100,75,25);
g2d.fillOval(160,95,80,35);

g2d.setColor(Color.gray);
g2d.fillOval(300,100,75,25);
g2d.fillOval(315,115,75,25);
g2d.fillOval(315,85,75,25);
g2d.fillOval(365,75,75,25);
g2d.fillOval(365,125,75,25);
g2d.fillOval(405,115,75,25);
g2d.fillOval(405,85,75,25);
g2d.fillOval(420,100,75,25);
g2d.fillOval(360,95,80,35);

g2d.setColor(Color.gray);
g2d.fillOval(500,100,75,25);
g2d.fillOval(515,115,75,25);
g2d.fillOval(515,85,75,25);
g2d.fillOval(565,75,75,25);
g2d.fillOval(565,125,75,25);
g2d.fillOval(605,115,75,25);
g2d.fillOval(605,85,75,25);
g2d.fillOval(620,100,75,25);
g2d.fillOval(560,95,80,35);

g2d.setColor(Color.gray);
g2d.fillOval(700,100,75,25);
g2d.fillOval(715,115,75,25);
g2d.fillOval(715,85,75,25);
g2d.fillOval(765,75,75,25);
g2d.fillOval(765,125,75,25);
g2d.fillOval(805,115,75,25);
g2d.fillOval(805,85,75,25);
g2d.fillOval(820,100,75,25);
g2d.fillOval(760,95,80,35);


g2d.setColor(lineColor);

for(int i = 0; i< = 820; i = i + 50){
g2d.drawLine(100 + i,160,100 + i,180);
}
for(int i = 0; i< = 820; i = i + 50){
g2d.drawLine(100 + i,200,100 + i,220);
}
for(int i = 0; i< = 820; i = i + 50){
g2d.drawLine(100 + i,240,100 + i,260);
}
for(int i = 0; i< = 820; i = i + 50){
g2d.drawLine(100 + i,280,100 + i,300);
}
for(int i = 0; i< = 820; i = i + 50){
g2d.drawLine(100 + i,320,100 + i,340);
}
for(int i = 0; i< = 820; i = i + 50){
g2d.drawLine(100 + i,360,100 + i,380);
}
for(int i = 0; i< = 820; i = i + 50){
g2d.drawLine(100 + i,400,100 + i,420);
}
for(int i = 0; i< = 820; i = i + 50){
g2d.drawLine(100 + i,440,100 + i,460);
}
for(int i = 0; i< = 820; i = i + 50){
g2d.drawLine(100 + i,480,100 + i,500);
}
for(int i = 0; i< = 820; i = i + 50){
g2d.drawLine(100 + i,520,100 + i,540);
}

g2d.setColor(Color.black);
g2d.fillRect(0,590,1000,100);

g2d.setColor(Color.white);

for(int i = 0; i< = 900; i = i + 150){
g2d.fillRect(30 + i,625,100,25);
}

g2d.setColor(new Color(139,69,19));

g2d.fillOval(100,600,35,15);
g2d.fillOval(300,600,30,10);
g2d.fillOval(500,600,25,10);
g2d.fillOval(700,600,35,15);
g2d.fillOval(125,650,35,15);
g2d.fillOval(175,650,35,15);
g2d.fillOval(230,670,35,15);
g2d.fillOval(280,640,35,15);
g2d.fillOval(340,630,35,9);
g2d.fillOval(400,650,35,19);
g2d.fillOval(450,610,35,10);
g2d.fillOval(500,610,35,18);
g2d.fillOval(600,675,35,17);
g2d.fillOval(700,675,25,15);
g2d.fillOval(710,675,30,15);
g2d.fillOval(770,623,40,15);
g2d.fillOval(690,655,25,15);
g2d.fillOval(830,660,25,15);
g2d.fillOval(810,670,23,15);
g2d.fillOval(880,613,29,15);
g2d.dispose();
}

}
}

但是,可能看起来更好的是,你可以为下雨的动画做好准备...



  import java.awt.Color; 
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

公共类测试{

public static void main(String [] args){
new Test();
}

public Test(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
ex.printStackTrace();
}

JFrame frame = new JFrame(Testing);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

公共类TestPane扩展JPanel {

private final int limit = 100;
private List< DropLet> drops =新的ArrayList<>(限制);

public TestPane(){
for(int index = 0; index< limit; index ++){
drops.add(new DropLet(150,675));
}
计时器计时器=新计时器(5,新的ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Iterator< DropLet> it = drops.()。($。b $ b DropLet dropLet = it.next();
if(dropLet.update(675)){
}
while(it.hasNext()) it.remove();
}
}
while(drops.sgt()< limit){
drops.add(new DropLet(150,150));
}
repaint();
}
});
timer.start();
}

@Override
public Dimension getPreferredSize(){
return new Dimension(1000,1000);
}

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g.create();
setBackground(Color.darkGray);

g2d.setColor(Color.gray);
g2d.fillOval(100,100,75,25);
g2d.fillOval(115,115,75,25);
g2d.fillOval(115,85,75,25);
g2d.fillOval(165,75,75,25);
g2d.fillOval(165,125,75,25);
g2d.fillOval(205,115,75,25);
g2d.fillOval(205,85,75,25);
g2d.fillOval(220,100,75,25);
g2d.fillOval(160,95,80,35);

g2d.setColor(Color.gray);
g2d.fillOval(300,100,75,25);
g2d.fillOval(315,115,75,25);
g2d.fillOval(315,85,75,25);
g2d.fillOval(365,75,75,25);
g2d.fillOval(365,125,75,25);
g2d.fillOval(405,115,75,25);
g2d.fillOval(405,85,75,25);
g2d.fillOval(420,100,75,25);
g2d.fillOval(360,95,80,35);

g2d.setColor(Color.gray);
g2d.fillOval(500,100,75,25);
g2d.fillOval(515,115,75,25);
g2d.fillOval(515,85,75,25);
g2d.fillOval(565,75,75,25);
g2d.fillOval(565,125,75,25);
g2d.fillOval(605,115,75,25);
g2d.fillOval(605,85,75,25);
g2d.fillOval(620,100,75,25);
g2d.fillOval(560,95,80,35);

g2d.setColor(Color.gray);
g2d.fillOval(700,100,75,25);
g2d.fillOval(715,115,75,25);
g2d.fillOval(715,85,75,25);
g2d.fillOval(765,75,75,25);
g2d.fillOval(765,125,75,25);
g2d.fillOval(805,115,75,25);
g2d.fillOval(805,85,75,25);
g2d.fillOval(820,100,75,25);
g2d.fillOval(760,95,80,35);

g2d.setColor(Color.black);
g2d.fillRect(0,590,1000,100);

g2d.setColor(Color.white);

for(int i = 0; i< = 900; i = i + 150){
g2d.fillRect(30 + i,625,100,25);
}

g2d.setColor(new Color(139,69,19));

g2d.fillOval(100,600,35,15);
g2d.fillOval(300,600,30,10);
g2d.fillOval(500,600,25,10);
g2d.fillOval(700,600,35,15);
g2d.fillOval(125,650,35,15);
g2d.fillOval(175,650,35,15);
g2d.fillOval(230,670,35,15);
g2d.fillOval(280,640,35,15);
g2d.fillOval(340,630,35,9);
g2d.fillOval(400,650,35,19);
g2d.fillOval(450,610,35,10);
g2d.fillOval(500,610,35,18);
g2d.fillOval(600,675,35,17);
g2d.fillOval(700,675,25,15);
g2d.fillOval(710,675,30,15);
g2d.fillOval(770,623,40,15);
g2d.fillOval(690,655,25,15);
g2d.fillOval(830,660,25,15);
g2d.fillOval(810,670,23,15);
g2d.fillOval(880,613,29,15);

for(DropLet dropLet:drops){
dropLet.paint(g2d);
}

g2d.dispose();
}

公共类DropLet {
私有点位置;
private int yDelta;

public DropLet(int from,int to){
location = new Point(100 +(int)(Math.random()* 800),from +(int)(Math。 random()*(to - from)));
yDelta =(int)(Math.random()* 5)+ 1;
}

public boolean update(int range){
location.y + = yDelta;
返回location.y>范围;
}

public void paint(Graphics2D g2d){
g2d.setColor(Color.CYAN);
g2d.fillOval(location.x - 3,location.y - 3,6,6);
}

}

}
}

虽然这可能看起来有些复杂,但这个概念非常基础。你有一个 List DroplLet s,代表 DropLet 以及它下降的速度。你基本上循环遍历列表并更新 DropLet 的位置,直到它们超出你想要的范围,这一点他们被删除了。



主循环保持一个恒定数量的液滴,在这种情况下为100,这样它将继续下雨。



为了简单起见,液滴的位置和速度是随机的


import java.awt.*;
import java.applet.*;
public class rain extends Applet 
{
    int i=0,j=0,k=0;
    AudioClip ac;
    public void init()
    {
        ac=getAudioClip(getCodeBase(),"rain.wav");
        ac.play();
    }
    public void paint(Graphics g)
    {
        setBackground(Color.darkGray);

        g.setColor(Color.gray);
        g.fillOval(100,100,75,25);
        g.fillOval(115,115,75,25);
        g.fillOval(115,85,75,25);
        g.fillOval(165,75,75,25);
        g.fillOval(165,125,75,25);
        g.fillOval(205,115,75,25);
        g.fillOval(205,85,75,25);
        g.fillOval(220,100,75,25);
        g.fillOval(160,95,80,35);

        g.setColor(Color.gray);
        g.fillOval(300,100,75,25);
        g.fillOval(315,115,75,25);
        g.fillOval(315,85,75,25);
        g.fillOval(365,75,75,25);
        g.fillOval(365,125,75,25);
        g.fillOval(405,115,75,25);
        g.fillOval(405,85,75,25);
        g.fillOval(420,100,75,25);
        g.fillOval(360,95,80,35);

        g.setColor(Color.gray);
        g.fillOval(500,100,75,25);
        g.fillOval(515,115,75,25);
        g.fillOval(515,85,75,25);
        g.fillOval(565,75,75,25);
        g.fillOval(565,125,75,25);
        g.fillOval(605,115,75,25);
        g.fillOval(605,85,75,25);
        g.fillOval(620,100,75,25);
        g.fillOval(560,95,80,35);

        g.setColor(Color.gray);
        g.fillOval(700,100,75,25);
        g.fillOval(715,115,75,25);
        g.fillOval(715,85,75,25);
        g.fillOval(765,75,75,25);
        g.fillOval(765,125,75,25);
        g.fillOval(805,115,75,25);
        g.fillOval(805,85,75,25);
        g.fillOval(820,100,75,25);
        g.fillOval(760,95,80,35);
enter code here
        g.setColor(Color.cyan);

        for(int i=0;i<=820;i=i+50)
        {
            g.drawLine(100+i,160,100+i,180);
            repaint();
        }
        for(int i=0;i<=820;i=i+50)
        {
            g.drawLine(100+i,200,100+i,220);
        }
        for(int i=0;i<=820;i=i+50)
        {
            g.drawLine(100+i,240,100+i,260);
        }
        for(int i=0;i<=820;i=i+50)
        {
            g.drawLine(100+i,280,100+i,300);
        }
        for(int i=0;i<=820;i=i+50)
        {
            g.drawLine(100+i,320,100+i,340);
        } 
        for(int i=0;i<=820;i=i+50)
        {
            g.drawLine(100+i,360,100+i,380);
        }
        for(int i=0;i<=820;i=i+50)
        {
            g.drawLine(100+i,400,100+i,420);
        }
        for(int i=0;i<=820;i=i+50)
        {
            g.drawLine(100+i,440,100+i,460);
        }
        for(int i=0;i<=820;i=i+50)
        {
            g.drawLine(100+i,480,100+i,500);
        }
        for(int i=0;i<=820;i=i+50)
        {
            g.drawLine(100+i,520,100+i,540);
        }

        g.setColor(Color.black);
        g.fillRect(0,590,1000,100);

        g.setColor(Color.white);

        for(i=0;i<=900;i=i+150)
            {
                g.fillRect(30+i,625,100,25);
            }

        g.setColor(new Color(139,69,19));

        g.fillOval(100,600,35,15);
        g.fillOval(300,600,30,10);
        g.fillOval(500,600,25,10);
        g.fillOval(700,600,35,15);
        g.fillOval(125,650,35,15);
        g.fillOval(175,650,35,15);
        g.fillOval(230,670,35,15);
        g.fillOval(280,640,35,15);
        g.fillOval(340,630,35,9);
        g.fillOval(400,650,35,19);
        g.fillOval(450,610,35,10);
        g.fillOval(500,610,35,18);
        g.fillOval(600,675,35,17);
        g.fillOval(700,675,25,15);
        g.fillOval(710,675,30,15);
        g.fillOval(770,623,40,15);
        g.fillOval(690,655,25,15);
        g.fillOval(830,660,25,15);
        g.fillOval(810,670,23,15);
        g.fillOval(880,613,29,15);
    }
}
/*<applet code=rain height=1000 width=1000>
</applet>*/

How Do I blink the only the for loop part of the program? I want to blink the lines contained in the various for loops. Or please tell any other way for blinking a part of the program..Any help will be dearly appreciated.

解决方案

Let's start with Applets are offically deprecated, see Java Plugin support deprecated and Moving to a Plugin-Free Web for more details.

The next equivalent is Swing, like most GUI frameworks, Swing is single threaded and NOT thread safe, this means, you can't perform blocking or long running actions within their event threads nor should you try to update their states from outside of their event threads

See Concurrency in Swing for more details.

In Swing, that leaves you a few options, probably the simplest would be a Swing Timer, see How to use Swing Timers for more details

Now, strictly speaking, this will change the line color for the objects painted within the loops, making them blink

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int count = 0;
        private Color lineColor = Color.DARK_GRAY;
        private Color[] lineColors = new Color[]{Color.CYAN, Color.DARK_GRAY};

        public TestPane() {
            Timer timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    count++;
                    lineColor = lineColors[count % 2];
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1000, 1000);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            setBackground(Color.darkGray);

            g2d.setColor(Color.gray);
            g2d.fillOval(100, 100, 75, 25);
            g2d.fillOval(115, 115, 75, 25);
            g2d.fillOval(115, 85, 75, 25);
            g2d.fillOval(165, 75, 75, 25);
            g2d.fillOval(165, 125, 75, 25);
            g2d.fillOval(205, 115, 75, 25);
            g2d.fillOval(205, 85, 75, 25);
            g2d.fillOval(220, 100, 75, 25);
            g2d.fillOval(160, 95, 80, 35);

            g2d.setColor(Color.gray);
            g2d.fillOval(300, 100, 75, 25);
            g2d.fillOval(315, 115, 75, 25);
            g2d.fillOval(315, 85, 75, 25);
            g2d.fillOval(365, 75, 75, 25);
            g2d.fillOval(365, 125, 75, 25);
            g2d.fillOval(405, 115, 75, 25);
            g2d.fillOval(405, 85, 75, 25);
            g2d.fillOval(420, 100, 75, 25);
            g2d.fillOval(360, 95, 80, 35);

            g2d.setColor(Color.gray);
            g2d.fillOval(500, 100, 75, 25);
            g2d.fillOval(515, 115, 75, 25);
            g2d.fillOval(515, 85, 75, 25);
            g2d.fillOval(565, 75, 75, 25);
            g2d.fillOval(565, 125, 75, 25);
            g2d.fillOval(605, 115, 75, 25);
            g2d.fillOval(605, 85, 75, 25);
            g2d.fillOval(620, 100, 75, 25);
            g2d.fillOval(560, 95, 80, 35);

            g2d.setColor(Color.gray);
            g2d.fillOval(700, 100, 75, 25);
            g2d.fillOval(715, 115, 75, 25);
            g2d.fillOval(715, 85, 75, 25);
            g2d.fillOval(765, 75, 75, 25);
            g2d.fillOval(765, 125, 75, 25);
            g2d.fillOval(805, 115, 75, 25);
            g2d.fillOval(805, 85, 75, 25);
            g2d.fillOval(820, 100, 75, 25);
            g2d.fillOval(760, 95, 80, 35);


            g2d.setColor(lineColor);

            for (int i = 0; i <= 820; i = i + 50) {
                g2d.drawLine(100 + i, 160, 100 + i, 180);
            }
            for (int i = 0; i <= 820; i = i + 50) {
                g2d.drawLine(100 + i, 200, 100 + i, 220);
            }
            for (int i = 0; i <= 820; i = i + 50) {
                g2d.drawLine(100 + i, 240, 100 + i, 260);
            }
            for (int i = 0; i <= 820; i = i + 50) {
                g2d.drawLine(100 + i, 280, 100 + i, 300);
            }
            for (int i = 0; i <= 820; i = i + 50) {
                g2d.drawLine(100 + i, 320, 100 + i, 340);
            }
            for (int i = 0; i <= 820; i = i + 50) {
                g2d.drawLine(100 + i, 360, 100 + i, 380);
            }
            for (int i = 0; i <= 820; i = i + 50) {
                g2d.drawLine(100 + i, 400, 100 + i, 420);
            }
            for (int i = 0; i <= 820; i = i + 50) {
                g2d.drawLine(100 + i, 440, 100 + i, 460);
            }
            for (int i = 0; i <= 820; i = i + 50) {
                g2d.drawLine(100 + i, 480, 100 + i, 500);
            }
            for (int i = 0; i <= 820; i = i + 50) {
                g2d.drawLine(100 + i, 520, 100 + i, 540);
            }

            g2d.setColor(Color.black);
            g2d.fillRect(0, 590, 1000, 100);

            g2d.setColor(Color.white);

            for (int i = 0; i <= 900; i = i + 150) {
                g2d.fillRect(30 + i, 625, 100, 25);
            }

            g2d.setColor(new Color(139, 69, 19));

            g2d.fillOval(100, 600, 35, 15);
            g2d.fillOval(300, 600, 30, 10);
            g2d.fillOval(500, 600, 25, 10);
            g2d.fillOval(700, 600, 35, 15);
            g2d.fillOval(125, 650, 35, 15);
            g2d.fillOval(175, 650, 35, 15);
            g2d.fillOval(230, 670, 35, 15);
            g2d.fillOval(280, 640, 35, 15);
            g2d.fillOval(340, 630, 35, 9);
            g2d.fillOval(400, 650, 35, 19);
            g2d.fillOval(450, 610, 35, 10);
            g2d.fillOval(500, 610, 35, 18);
            g2d.fillOval(600, 675, 35, 17);
            g2d.fillOval(700, 675, 25, 15);
            g2d.fillOval(710, 675, 30, 15);
            g2d.fillOval(770, 623, 40, 15);
            g2d.fillOval(690, 655, 25, 15);
            g2d.fillOval(830, 660, 25, 15);
            g2d.fillOval(810, 670, 23, 15);
            g2d.fillOval(880, 613, 29, 15);
            g2d.dispose();
        }

    }
}

BUT, what might look better, is you could animate the rain falling...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private final int limit = 100;
        private List<DropLet> droplets = new ArrayList<>(limit);

        public TestPane() {
            for (int index = 0; index < limit; index++) {
                droplets.add(new DropLet(150, 675));
            }
            Timer timer = new Timer(5, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Iterator<DropLet> it = droplets.iterator();
                    while (it.hasNext()) {
                        DropLet dropLet = it.next();
                        if (dropLet.update(675)) {
                            it.remove();
                        }
                    }
                    while (droplets.size() < limit) {
                        droplets.add(new DropLet(150, 150));
                    }
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1000, 1000);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            setBackground(Color.darkGray);

            g2d.setColor(Color.gray);
            g2d.fillOval(100, 100, 75, 25);
            g2d.fillOval(115, 115, 75, 25);
            g2d.fillOval(115, 85, 75, 25);
            g2d.fillOval(165, 75, 75, 25);
            g2d.fillOval(165, 125, 75, 25);
            g2d.fillOval(205, 115, 75, 25);
            g2d.fillOval(205, 85, 75, 25);
            g2d.fillOval(220, 100, 75, 25);
            g2d.fillOval(160, 95, 80, 35);

            g2d.setColor(Color.gray);
            g2d.fillOval(300, 100, 75, 25);
            g2d.fillOval(315, 115, 75, 25);
            g2d.fillOval(315, 85, 75, 25);
            g2d.fillOval(365, 75, 75, 25);
            g2d.fillOval(365, 125, 75, 25);
            g2d.fillOval(405, 115, 75, 25);
            g2d.fillOval(405, 85, 75, 25);
            g2d.fillOval(420, 100, 75, 25);
            g2d.fillOval(360, 95, 80, 35);

            g2d.setColor(Color.gray);
            g2d.fillOval(500, 100, 75, 25);
            g2d.fillOval(515, 115, 75, 25);
            g2d.fillOval(515, 85, 75, 25);
            g2d.fillOval(565, 75, 75, 25);
            g2d.fillOval(565, 125, 75, 25);
            g2d.fillOval(605, 115, 75, 25);
            g2d.fillOval(605, 85, 75, 25);
            g2d.fillOval(620, 100, 75, 25);
            g2d.fillOval(560, 95, 80, 35);

            g2d.setColor(Color.gray);
            g2d.fillOval(700, 100, 75, 25);
            g2d.fillOval(715, 115, 75, 25);
            g2d.fillOval(715, 85, 75, 25);
            g2d.fillOval(765, 75, 75, 25);
            g2d.fillOval(765, 125, 75, 25);
            g2d.fillOval(805, 115, 75, 25);
            g2d.fillOval(805, 85, 75, 25);
            g2d.fillOval(820, 100, 75, 25);
            g2d.fillOval(760, 95, 80, 35);

            g2d.setColor(Color.black);
            g2d.fillRect(0, 590, 1000, 100);

            g2d.setColor(Color.white);

            for (int i = 0; i <= 900; i = i + 150) {
                g2d.fillRect(30 + i, 625, 100, 25);
            }

            g2d.setColor(new Color(139, 69, 19));

            g2d.fillOval(100, 600, 35, 15);
            g2d.fillOval(300, 600, 30, 10);
            g2d.fillOval(500, 600, 25, 10);
            g2d.fillOval(700, 600, 35, 15);
            g2d.fillOval(125, 650, 35, 15);
            g2d.fillOval(175, 650, 35, 15);
            g2d.fillOval(230, 670, 35, 15);
            g2d.fillOval(280, 640, 35, 15);
            g2d.fillOval(340, 630, 35, 9);
            g2d.fillOval(400, 650, 35, 19);
            g2d.fillOval(450, 610, 35, 10);
            g2d.fillOval(500, 610, 35, 18);
            g2d.fillOval(600, 675, 35, 17);
            g2d.fillOval(700, 675, 25, 15);
            g2d.fillOval(710, 675, 30, 15);
            g2d.fillOval(770, 623, 40, 15);
            g2d.fillOval(690, 655, 25, 15);
            g2d.fillOval(830, 660, 25, 15);
            g2d.fillOval(810, 670, 23, 15);
            g2d.fillOval(880, 613, 29, 15);

            for (DropLet dropLet : droplets) {
                dropLet.paint(g2d);
            }

            g2d.dispose();
        }

        public class DropLet {
            private Point location;
            private int yDelta;

            public DropLet(int from, int to) {
                location = new Point(100 + (int)(Math.random() * 800), from + (int)(Math.random() * (to - from)));
                yDelta = (int)(Math.random() * 5) + 1;
            }

            public boolean update(int range) {
                location.y += yDelta;
                return location.y > range;
            }

            public void paint(Graphics2D g2d) {
                g2d.setColor(Color.CYAN);
                g2d.fillOval(location.x - 3, location.y - 3, 6, 6);
            }

        }

    }
}

While this might look some what complex, the concept is very basic. You have a List of DroplLets, which represent the position of a DropLet and the speed at which it drops. You basically loop over the list and update the position of the DropLets until they fall beyond the range you want, at that point they are removed.

The "main loop" maintains a constant number droplets, in this case 100 so that it will continue to rain.

To make things simpler, the positions and speed of the droplets are randomised

这篇关于如何在Applet画布上制作彩绘对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆