Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
69 views
in Technique[技术] by (71.8m points)

java - Passing current Date

Seems like as not possible to fix Graphics2D lack in code, there I must to set fix size for animations, otherwise some Start outside Rectangle 490 x 490 freeze or shaking on the screen

my SSCCE

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.table.*;

public class OneTwoThree implements Runnable {

    private String testS;
    private JFrame frame = new JFrame("frameTitle");
    private JScrollPane tblS = new JScrollPane();
    private JTable tbl;
    private Vector<String> rOrH;
    private long t1 = 0L;
    private long t2 = 0L;
    private AlphaChars aChars = new AlphaChars();
    private GradientViewPortOriginal tableViewPort;
    private JViewport viewport = new JViewport();
    private Random random = new Random();
    private Vector<Vector<Integer>> vec = new Vector<Vector<Integer>>();
    private boolean bolo = false;

    public OneTwoThree() {
        t1 = System.currentTimeMillis();
        int regLenght = 3500;
        int chars = 0;
        testS = aChars.getNext(regLenght);
        rOrH = new Vector<String>();
        Vector<Vector<String>> rowD = new Vector<Vector<String>>();
        for (int e = 0; e < regLenght;) {
            chars++;
            if (chars > 50) {
                chars = 1;
                rowD.add(rOrH);
                rOrH = new Vector<String>();
            }
            String str = (testS.substring(e, (e + 1))).toString();
            if (str != null) {
                rOrH.add(str);
            } else {
                rOrH.add("");
            }
            e++;
        }
        rOrH = new Vector<String>();
        for (int i = 0; i < 50; i++) {
            rOrH.add(String.valueOf(i + 1));
        }
        tbl = new JTable(rowD, rOrH);
        TableColumnModel tcm = tbl.getColumnModel();
        for (int i = 0; i < (tcm.getColumnCount()); i++) {
            tcm.getColumn(i).setPreferredWidth(4);
        }
        tbl.setGridColor(tbl.getBackground());
        tbl.setRowHeight(10);
        tbl.setTableHeader(null);
        tbl.setPreferredScrollableViewportSize(tbl.getPreferredSize());
        tbl.setRowSelectionAllowed(false);
        tbl.setColumnSelectionAllowed(false);
        tableViewPort = new GradientViewPortOriginal(tbl);
        viewport = tableViewPort.getViewport();
        RepaintManager.setCurrentManager(new RepaintManager() {

            @Override
            public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
                Container con = c.getParent();
                while (con instanceof JComponent) {
                    if (!con.isVisible()) {
                        return;
                    }
                    if (con instanceof GradientViewPortOriginal) {
                        c = (JComponent) con;
                        x = 0;
                        y = 0;
                        w = con.getWidth();
                        h = con.getHeight();
                    }
                    con = con.getParent();
                }
                super.addDirtyRegion(c, x, y, w, h);
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(tableViewPort, BorderLayout.CENTER);
        frame.setLocation(20, 20);
        frame.pack();
        frame.setVisible(true);
        addColumnRenderes();
        for (int i = 0; i < 35; i++) {
            Star star = new Star(new Point(random.nextInt(490), random.nextInt(490)));
            star.setColor(new Color(100 + random.nextInt(155), 100 + random.nextInt(155), 100 + random.nextInt(155)));
            star.setxIncr(-3 + random.nextInt(7));
            star.setyIncr(-3 + random.nextInt(7));
            tableViewPort.add(star);
        }
    }

    private void addColumnRenderes() {
        for (int i = 0; i < tbl.getColumnCount(); i++) {
            RowColorRenderer rowRenderer = new RowColorRenderer(i);
            TableColumn column = tbl.getColumnModel().getColumn(i);
            column.setCellRenderer(rowRenderer);
        }
        addTableData();
    }

    private void addTableData() {
        tbl.setValueAt("8", 4, 25);tbl.setValueAt("4", 5, 24);tbl.setValueAt("1", 5, 25);tbl.setValueAt("4", 5, 26);
        tbl.setValueAt("4", 6, 23);tbl.setValueAt("4", 6, 25);tbl.setValueAt("1", 6, 26);tbl.setValueAt("4", 6, 27);
        tbl.setValueAt("1", 6, 28);tbl.setValueAt("1", 7, 21);tbl.setValueAt("4", 7, 22);tbl.setValueAt("1", 7, 23);
        tbl.setValueAt("4", 7, 24);tbl.setValueAt("1", 7, 27);tbl.setValueAt("8", 7, 28);tbl.setValueAt("4", 8, 23);
        tbl.setValueAt("1", 8, 24);tbl.setValueAt("1", 8, 25);tbl.setValueAt("8", 8, 26);tbl.setValueAt("8", 8, 27);
        tbl.setValueAt("7", 9, 24);tbl.setValueAt("1", 9, 26);tbl.setValueAt("4", 9, 25);tbl.setValueAt("4", 10, 24);
        tbl.setValueAt("1", 10, 25);tbl.setValueAt("8", 10, 26);tbl.setValueAt("4", 11, 23);tbl.setValueAt("4", 11, 27);
        tbl.setValueAt("1", 12, 21);tbl.setValueAt("4", 12, 22);tbl.setValueAt("8", 12, 28);tbl.setValueAt("4", 13, 21);
        tbl.setValueAt("8", 13, 29);tbl.setValueAt("1", 14, 19);tbl.setValueAt("4", 14, 20);tbl.setValueAt("8", 14, 30);
        tbl.setValueAt("4", 15, 19);tbl.setValueAt("4", 15, 20);tbl.setValueAt("4", 15, 21);tbl.setValueAt("1", 15, 22);
        tbl.setValueAt("4", 15, 23);tbl.setValueAt("1", 15, 31);tbl.setValueAt("1", 15, 32);tbl.setValueAt("4", 16, 22);
        tbl.setValueAt("4", 17, 21);tbl.setValueAt("4", 18, 20);tbl.setValueAt("4", 19, 20);tbl.setValueAt("4", 20, 19);
        tbl.setValueAt("4", 21, 17);tbl.setValueAt("4", 21, 18);tbl.setValueAt("4", 22, 16);tbl.setValueAt("4", 22, 17);
        tbl.setValueAt("4", 22, 18);tbl.setValueAt("4", 22, 19);tbl.setValueAt("4", 22, 20);tbl.setValueAt("4", 24, 18);
        tbl.setValueAt("4", 25, 17);tbl.setValueAt("4", 25, 17);tbl.setValueAt("4", 26, 16);tbl.setValueAt("4", 27, 15);
        tbl.setValueAt("4", 27, 16);tbl.setValueAt("4", 27, 18);tbl.setValueAt("4", 28, 17);tbl.setValueAt("4", 29, 16);
        tbl.setValueAt("4", 30, 15);tbl.setValueAt("4", 31, 14);tbl.setValueAt("4", 32, 13);tbl.setValueAt("4", 33, 12);
        tbl.setValueAt("4", 33, 13);tbl.setValueAt("4", 33, 14);tbl.setValueAt("4", 33, 18);tbl.setValueAt("4", 34, 17);
        tbl.setValueAt("4", 35, 16);tbl.setValueAt("4", 36, 15);tbl.setValueAt("4", 37, 14);tbl.setValueAt("4", 38, 13);
        tbl.setValueAt("4", 39, 12);tbl.setValueAt("4", 40, 11);tbl.setValueAt("4", 41, 10);tbl.setValueAt("4", 42, 9);
        tbl.setValueAt("4", 42, 10);tbl.setValueAt("4", 42, 11);tbl.setValueAt("4", 38, 18);tbl.setValueAt("4", 39, 18);
        tbl.setValueAt("4", 40, 18);tbl.setValueAt("4", 41, 17);tbl.setValueAt("4", 42, 16);tbl.setValueAt("4", 43, 15);
        tbl.setValueAt("4", 44, 14);tbl.setValueAt("4", 46, 13);tbl.setValueAt("4", 47, 12);tbl.setValueAt("4", 48, 11);
        tbl.setValueAt("4", 49, 10);tbl.setValueAt("4", 50, 9);tbl.setValueAt("4", 51, 8);tbl.setValueAt("4", 52, 7);
        tbl.setValueAt("4", 53, 6);tbl.setValueAt("4", 54, 5);tbl.setValueAt("4", 55, 4);tbl.setValueAt("4", 56, 6);
        tbl.setValueAt("4", 56, 8);tbl.setValueAt("4", 56, 9);tbl.setValueAt("4", 56, 10);tbl.setValueAt("4", 56, 12);
        tbl.setValueAt("4", 56, 13);tbl.setValueAt("4", 58, 10);tbl.setValueAt("4", 59, 11);tbl.setValueAt("4", 59, 12);
        tbl.setValueAt("4", 60, 13);tbl.setValueAt("4", 61, 15);tbl.setValueAt("4", 61, 16);tbl.setValueAt("4", 59, 17);
        tbl.setValueAt("4", 58, 18);tbl.setValueAt("4", 57, 19);tbl.setValueAt("4", 62, 18);tbl.setValueAt("4", 63, 19);
        tbl.setValueAt("4", 63, 20);tbl.setValueAt("3", 62, 23);tbl.setValueAt("3", 63, 23);tbl.setValueAt("3", 64, 23);
        tbl.setValueAt("3", 65, 23);tbl.setValueAt("3", 66, 23);tbl.setValueAt("3", 67, 23);tbl.setValueAt("3", 62, 28);
        tbl.setValueAt("3", 63, 28);tbl.setValueAt("3", 64, 28);tbl.setValueAt("3", 65, 28);tbl.setValueAt("3", 66, 28);
        tbl.setValueAt("3", 67, 28);tbl.setValueAt("8", 14, 26);tbl.setValueAt("8", 14, 27);tbl.setValueAt("8", 16, 27);
        tbl.setValueAt("8", 16, 28);tbl.setValueAt("8", 17, 29);tbl.setValueAt("8", 18, 30);tbl.setValueAt("8", 19, 31);
        tbl.setValueAt("8", 20, 32);tbl.setValueAt("8", 21, 31);tbl.setValueAt("8", 22, 30);tbl.setValueAt("8", 22, 29);
        tbl.setValueAt("3", 20, 27);tbl.setValueAt("3", 21, 26);tbl.setValueAt("3", 22, 25);tbl.setValueAt("3", 23, 24);
        tbl.setValueAt("3", 24, 24);tbl.setValueAt("3", 24, 25);tbl.setValueAt("3", 24, 26);tbl.setValueAt("8", 22, 32);
        tbl.setValueAt("8", 23, 33);tbl.setValueAt("8", 24, 34);tbl.setValueAt("8", 25, 35);tbl.setValueAt("8", 26, 36);
        tbl.setValueAt("8", 27, 37);tbl.setValueAt("8", 29, 34);tbl.setValueAt("8", 29, 35);tbl.setValueAt("8", 28, 36);
        tbl.setValueAt("8", 30, 32);tbl.setValueAt("8", 31, 33);tbl.setValueAt("8", 32, 34);tbl.setValueAt("8", 33, 35);
        tbl.setValueAt("8", 34, 36);tbl.setValueAt("8", 34, 37);tbl.setValueAt("3", 27, 30);tbl.setValueAt("3", 28, 29);
        tbl.setValueAt("3", 29, 28);tbl.setValueAt("3", 30, 28);tbl.setValueAt("3", 31, 29);tbl.setValueAt("3", 32, 30);
        tbl.setValueAt("3", 31, 24);tbl.setValueAt("3", 32, 23);tbl.setValueAt("3", 33, 22);tbl.setValueAt("3", 34, 22);
        tbl.setValueAt("3", 34, 23);tbl.setValueAt("3", 34, 24);tbl.setValueAt("3", 37, 20);tbl.setValueAt("3", 38, 19);
        tbl.setValueAt("3", 39, 18);tbl.setValueAt("3", 40, 18);tbl.setValueAt("3", 41, 19);tbl.setValueAt("3", 42, 20);
        tbl.setValueAt("3", 36, 34);tbl.setValueAt("3", 37, 33);tbl.setValueAt("3", 38, 32);tbl.setValueAt("3", 39, 32);
        tbl.setValueAt("3", 39, 33);tbl.setValueAt("3", 39, 34);tbl.setValueAt("3", 40, 24);tbl.setValueAt("3", 41, 23);
        tbl.setValueAt("3", 42, 22);tbl.setValueAt("3", 43, 22);tbl.setValueAt("3", 43, 23);tbl.setValueAt("3", 43, 24);
        tbl.setValueAt("3", 42, 30);tbl.setValueAt("3", 43, 29);tbl.setValueAt("3", 44, 28);tbl.setValueAt("3", 45, 28);
        tbl.setValueAt("3", 46, 29);tbl.setValueAt("3", 47, 30);tbl.setValueAt("3", 50, 21);tbl.setValueAt("3", 51, 20);
        tbl.setValueAt("3", 52, 19);tbl.setValueAt("3", 53, 19);tbl.setValueAt("3", 53, 20);tbl.setValueAt("3", 53, 21);
        tbl.setValueAt("3", 50, 31);tbl.setValueAt("3", 51, 30);tbl.setValueAt("3", 52, 29);tbl.setValueAt("3", 53, 29);
        tbl.setValueAt("3", 53, 30);tbl.setValueAt("3", 53, 31);tbl.setValueAt("3", 47, 15);tbl.setValueAt("3", 48, 14);
        tbl.setValueAt("3", 49, 13);tbl.setValueAt("3", 50, 13);tbl.setValueAt("3", 51, 14);tbl.setValueAt("3", 52, 15);
        tbl.setValueAt("3", 51, 25);tbl.setValueAt("3", 52, 24);tbl.setValueAt("3", 53, 23);tbl.setValueAt("3", 54, 23);
        tbl.setValueAt("3", 55, 24);tbl.setValueAt("3", 46, 25);tbl.setValueAt("3", 50, 38);tbl.setValueAt("3", 51, 37);
        tbl.setValueAt("3", 52, 36);tbl.setValueAt("3", 53, 36);tbl.setValueAt("3", 54, 37);tbl.setValueAt("3", 55, 38);
        tbl.setValueAt("3", 58, 28);tbl.setValueAt("3", 59, 27);tbl.setValueAt("3", 60, 26);tbl.setValueAt("3", 58, 26);
        tbl.setValueAt("3", 59, 27);tbl.setValueAt("3", 60, 28);tbl.setValueAt("8", 37, 36);tbl.setValueAt("8", 38, 37);
        tbl.setValueAt("8", 38, 38);tbl.setValueAt("8", 40, 39);tbl.setValueAt("8", 41, 40);tbl.setValueAt("8", 42, 41);
        tbl.setValueAt("8", 43, 41);tbl.setValueAt("8", 44, 38);tbl.setValueAt("8", 44, 39);tbl.setValueAt("8", 44, 40);
        tbl.setValueAt("8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The example below creates MAX ornaments for your tree. It uses a ComponentListener to relocate any outlying ornaments.

enter image description here

package merrychristmas;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Baubles extends JPanel implements ActionListener {

    private static final int MAX = 64;
    private static final String TITLE = "Baubles";
    private static final Random rnd = new Random();
    private static final AlphaComposite ac =
        AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f);
    private final Timer timer = new Timer(100, this);
    private final Queue<Bauble> queue = new LinkedList<Bauble>();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame(TITLE);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Baubles dt = new Baubles();
                f.add(dt);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }

    public Baubles() {
        this.setPreferredSize(new Dimension(8 * MAX, 8 * MAX));
        this.addComponentListener(new ComponentAdapter() {

            @Override // relocate any outliers
            public void componentResized(ComponentEvent e) {
                for (Bauble bauble : queue) {
                    if (bauble.x > getWidth() || bauble.y > getHeight()) {
                        bauble.x = rnd.nextInt(getWidth());
                        bauble.y = rnd.nextInt(getHeight());
                    }
                }
            }
        });
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (queue.isEmpty()) {
            for (int i = 0; i < MAX; i++) {
                queue.add(randomBauble());
            }
        }
        queue.add(randomBauble());
        queue.remove();
        this.repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.black);
        g2d.fillRect(0, 0, getWidth(), this.getHeight());
        g2d.setComposite(ac);
        for (Bauble b : queue) {
            g2d.setColor(b.c);
            g2d.fillOval(b.x, b.y, b.d, b.d);
        }
    }

    private Bauble randomBauble() {
        int x = rnd.nextInt(getWidth());
        int y = rnd.nextInt(getHeight());
        int r = rnd.nextInt(MAX) + MAX / 2;
        Color c = new Color(rnd.nextInt());
        return new Bauble(x, y, r, c);
    }

    private static class Bauble {

        private int x, y, d;
        private Color c;

        public Bauble(int x, int y, int r, Color c) {
            this.x = x - r;
            this.y = y - r;
            this.d = 2 * r;
            this.c = c;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...