Passa la data corrente

Sembra che non sia ansible correggere la mancanza di codice in Graphics2D, ma devo impostare la dimensione della correzione per le animazioni, altrimenti alcuni iniziano fuori dal rettangolo 490 x 490 o si bloccano sullo schermo

il mio 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 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> vec = new Vector<Vector>(); private boolean bolo = false; public OneTwoThree() { t1 = System.currentTimeMillis(); int regLenght = 3500; int chars = 0; testS = aChars.getNext(regLenght); rOrH = new Vector(); Vector<Vector> rowD = new Vector<Vector>(); for (int e = 0; e  50) { chars = 1; rowD.add(rOrH); rOrH = new Vector(); } String str = (testS.substring(e, (e + 1))).toString(); if (str != null) { rOrH.add(str); } else { rOrH.add(""); } e++; } rOrH = new Vector(); 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  Merry Christmast < - - "); t2 = System.currentTimeMillis(); System.out.println("miliSec:" + (t2 - t1)); //aver. 32 miliSec. new Thread(this).start(); } @Override public void run() { while (true) { try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } if (!bolo) { for (int i = 0; i < 30; i++) { Vector vecLocal = new Vector(); final int r1 = random.nextInt(tbl.getRowCount() - 1); final int c1 = random.nextInt(tbl.getColumnCount() - 1); vecLocal.add(r1); vecLocal.add(c1); vec.add(vecLocal); addSnowFlag(); try { Thread.sleep(250); } catch (Exception e) { e.printStackTrace(); } } bolo = true; } else { Vector vecLocal = new Vector(); final int r = random.nextInt(tbl.getRowCount() - 1); final int c = random.nextInt(tbl.getColumnCount() - 1); vecLocal.add(r); vecLocal.add(c); vec.add(vecLocal); addSnowFlag(); try { Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } } } } private void addSnowFlag() { if (vec.size() > -1) { Vector vecLocal = vec.elementAt(vec.size() - 1); final int r = vecLocal.elementAt(0); final int c = vecLocal.elementAt(1); String str = String.valueOf(tbl.getValueAt(r, c)); if (!str.isEmpty()) { if (!Pattern.compile("\\d").matcher(str).find()) { if (!Pattern.compile("[123456789]").matcher(str).find()) { Runnable doRun = new Runnable() { @Override public void run() { tbl.setValueAt("9", r, c); } }; SwingUtilities.invokeLater(doRun); if (bolo) { Vector vecLocal1 = vec.elementAt(0); final int r1 = vecLocal1.elementAt(0); final int c1 = vecLocal1.elementAt(1); Runnable doRun1 = new Runnable() { @Override public void run() { tbl.setValueAt(aChars.getNext(1), r1, c1); vec.removeElementAt(0); } }; SwingUtilities.invokeLater(doRun1); } } } } } } private class GradientViewPortOriginal extends JScrollPane { private static final long serialVersionUID = 1L; private final int h = 80; private BufferedImage img = null; private BufferedImage shadow = new BufferedImage(1, h, BufferedImage.TYPE_INT_ARGB); private JViewport viewPort; private ArrayList stars = new ArrayList(); public GradientViewPortOriginal(JComponent com) { super(com); viewPort = this.getViewport(); viewPort.setScrollMode(JViewport.BLIT_SCROLL_MODE); viewPort.setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE); viewPort.setScrollMode(JViewport.SIMPLE_SCROLL_MODE); setPreferredSize(this.getPreferredSize()); paintBackGround(new Color(250, 250, 150)); } public void add(Star star) { stars.add(star); } // private Timer timer = new Timer(33, new ActionListener() { @Override public void actionPerformsd(ActionEvent e) { for (Star star : stars) { star.move(); } repaint(); } }); public void startAnimation() { if (!timer.isRunning()) { timer.start(); } } @Override public void addNotify() { super.addNotify(); timer.start(); } private void paintBackGround(Color g) { Graphics2D g2 = shadow.createGraphics(); g2.setPaint(g); g2.fillRect(0, 0, 1, h); g2.setComposite(AlphaComposite.DstIn); g2.setPaint(new GradientPaint(0, 0, new Color(0, 0, 0, 0f), 0, h, new Color(0.8f, 0.8f, 0.8f, 0.5f))); g2.fillRect(0, 0, 1, h); g2.dispose(); } @Override public void paint(Graphics g) { if (img == null || img.getWidth() != getWidth() || img.getHeight() != getHeight()) { img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); } Graphics2D g2 = img.createGraphics(); super.paint(g2); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Rectangle bounds = getViewport().getVisibleRect(); g2.scale(bounds.getWidth(), -1); int y = (getColumnHeader() == null) ? 0 : getColumnHeader().getHeight(); g2.drawImage(shadow, bounds.x, -bounds.y - y - h, null); g2.scale(1, -1); g2.drawImage(shadow, bounds.x, bounds.y + bounds.height - h + y, null); g2.dispose(); g.drawImage(img, 0, 0, null); for (Star star : stars) { g.setColor(star.getColor()); g.fillPolygon(star); } } } private class RowColorRenderer extends DefaultTableCellRenderer { private static final long serialVersionUID = 1L; private int colNo = 0; RowColorRenderer(int col) { colNo = col; } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (!isSelected) { if (table.getValueAt(row, colNo) != null) { String str = table.getValueAt(row, colNo).toString(); if (!str.isEmpty()) { if (Pattern.compile("\\d").matcher(str).find()) { if (((Pattern.compile("[9]").matcher(str).find()))) { setBackground(new Color(250, 250, 150)); setForeground(new Color(250, 250, 150)); setFont(new Font("Serif", Font.BOLD, 9)); } else if (((Pattern.compile("[1]").matcher(str).find()))) { setBackground(tbl.getBackground()); setForeground(Color.orange); setFont(new Font("Serif", Font.BOLD, 8)); } else { setBackground(tbl.getBackground()); setForeground(Color.gray); setFont(new Font("Serif", Font.BOLD, 9)); } } else { setBackground(tbl.getBackground()); setForeground(Color.lightGray); setFont(new Font("Serif", Font.PLAIN, 6)); } } } } setHorizontalAlignment(CENTER); return this; } } private class AlphaChars { public static final int MIN_LENGTH = 2000; private java.util.Random rand = new java.util.Random(); private char[] AlphaChars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',}; /*'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-', '*', '/', '', '&', '#', '@', '{', '}', '?', ':', '_', '"', '!', ')', '('};*/ public String getNext() { StringBuilder strbuf = new StringBuilder(); for (int i = 0; i < MIN_LENGTH; i++) { strbuf.append(getAlphaChars()[getRand().nextInt(getAlphaChars().length)]); } return strbuf.toString(); } public String getNext(int reqLenght) { StringBuilder strbuf = new StringBuilder(); for (int i = 0; i < reqLenght; i++) { strbuf.append(getAlphaChars()[getRand().nextInt(getAlphaChars().length)]); } return strbuf.toString(); } public java.util.Random getRand() { return rand; } public void setRand(java.util.Random aRand) { rand = aRand; } public char[] getAlphaChars() { return AlphaChars; } public void setAlphaChars(char[] aAlphaChars) { AlphaChars = aAlphaChars; } } private class Star extends Polygon { private static final long serialVersionUID = 1L; private Point location = null; private Color color = Color.YELLOW; private int xIncr, yIncr; static final int WIDTH = 500, HEIGHT = 500; Star(Point location) { int x = location.x; int y = location.y; this.location = location; this.addPoint(x, y + 8); this.addPoint(x + 8, y + 8); this.addPoint(x + 11, y); this.addPoint(x + 14, y + 8); this.addPoint(x + 22, y + 8); this.addPoint(x + 17, y + 12); this.addPoint(x + 21, y + 20); this.addPoint(x + 11, y + 14); this.addPoint(x + 3, y + 20); this.addPoint(x + 6, y + 12); } public void setColor(Color color) { this.color = color; } public void move() { if (location.x  WIDTH) { xIncr = -xIncr; } if (location.y  WIDTH) { yIncr = -yIncr; } translate(xIncr, yIncr); location.setLocation(location.x + xIncr, location.y + yIncr); } public void setxIncr(int xIncr) { this.xIncr = xIncr; } public void setyIncr(int yIncr) { this.yIncr = yIncr; } public Color getColor() { return color; } } public static void main(String args[]) { Runnable doRun = new Runnable() { @Override public void run() { OneTwoThree hnvit = new OneTwoThree(); } }; SwingUtilities.invokeLater(doRun); } } 

Mi auguro Buon Natale per tutti i contributori Java qui

MODIFICARE:

per favore change private class Star extends Polygon { metodo move() in —>

     public void move() { if (location.x  frame.getContentPane().getWidth() - 20) { xIncr = -xIncr; } if (location.y  frame.getContentPane().getHeight() - 20) { yIncr = -yIncr; } translate(xIncr, yIncr); location.setLocation(location.x + xIncr, location.y + yIncr); } 

    L’esempio seguente crea MAX ornamenti per il tuo albero. Usa un ComponentListener per spostare qualsiasi ornamento esterno.

    inserisci la descrizione dell'immagine qui

     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 queue = new LinkedList(); 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 actionPerformsd(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(bc); g2d.fillOval(bx, by, bd, bd); } } 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; } } }