答案取決於你想達到什麼,但基本上,你需要一些方法將結果繪製到屏幕上。
如果數據不經常更改,最好使用後備緩衝區,因爲它對渲染器更快,而不必每次循環訪問數據集。
退房Performing Custom Painting
動畫
import java.awt.BorderLayout;
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 java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Dotty {
public static void main(String[] args) {
new Dotty();
}
public Dotty() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private int count = 0;
private int dotCount = 1000;
private BufferedImage background;
public TestPane() {
background = new BufferedImage(600, 600, BufferedImage.TYPE_INT_ARGB);
Timer timer;
timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
count++;
if (count < dotCount) {
int x = (int) Math.round((Math.random() * 600));
int y = (int) Math.round((Math.random() * 600));
Graphics2D g2d = background.createGraphics();
g2d.setColor(Color.BLACK);
g2d.drawRect(x, y, 1, 1);
g2d.dispose();
repaint();
} else {
((Timer) e.getSource()).stop();
}
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - background.getWidth())/2;
int y = (getHeight() - background.getHeight())/2;
g2d.drawImage(background, x, y, this);
g2d.dispose();
}
}
}
直渲染
import java.awt.BorderLayout;
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 java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Dotty {
public static void main(String[] args) {
new Dotty();
}
public Dotty() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private int count = 0;
private int dotCount = 1000;
private BufferedImage background;
public TestPane() {
background = new BufferedImage(600, 600, BufferedImage.TYPE_INT_ARGB);
for (int count = 0; count < dotCount; count++) {
int x = (int) Math.round((Math.random() * 600));
int y = (int) Math.round((Math.random() * 600));
Graphics2D g2d = background.createGraphics();
g2d.setColor(Color.BLACK);
g2d.drawRect(x, y, 1, 1);
g2d.dispose();
repaint();
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - background.getWidth())/2;
int y = (getHeight() - background.getHeight())/2;
g2d.drawImage(background, x, y, this);
g2d.dispose();
}
}
}