如何在Applet画布上制作彩绘对象?

时间:2017-07-17 08:23:49

标签: java 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);
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>*/

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

1 个答案:

答案 0 :(得分:0)

让我们从Applet开始被弃用,有关详细信息,请参阅Java Plugin support deprecatedMoving to a Plugin-Free Web

下一个等价物是Swing,就像大多数GUI框架一样,Swing是单线程的而且不是线程安全的,这意味着,你不能在他们的事件线程中执行阻塞或长时间运行的操作,也不应该尝试从外部更新他们的状态他们的事件线程

有关详细信息,请参阅Concurrency in Swing

在Swing中,您可以选择一些选项,最简单的可能是Swing Timer,有关详细信息,请参阅How to use Swing Timers

现在,严格地说,这将改变循环中绘制的对象的线条颜色,使它们闪烁

Raining

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();
        }

    }
}

但是,看起来更好看的是,你可以为雨水制作动画......

Bring The Rain

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

        }

    }
}

虽然这可能看起来有些复杂,但这个概念非常基础。您有List DroplLetDropLet,代表update的位置及其下降的速度。您基本上会遍历列表并DropLet .trx的位置,直到它们超出您想要的范围,此时它们将被删除。

“主循环”保持恒定数量的液滴,在这种情况下为100,以便它继续下雨。

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