Arquivo da tag: thread

Bounce Thread

/**
* @version 1.20 1999-04-25
* @author Cay Horstmann
*/

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BounceThread {
public static void main(String[] args) {
JFrame frame = new BounceThreadFrame();
frame.show();
}
}

class BounceThreadFrame extends JFrame {
public BounceThreadFrame() {
setSize(300, 200);
setTitle("Bounce");

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

Container contentPane = getContentPane();
canvas = new JPanel();
contentPane.add(canvas, "Center");
JPanel p = new JPanel();
addButton(p, "Start", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Ball b = new Ball(canvas);
b.start();
}
});

addButton(p, "Close", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
canvas.setVisible(false);
System.exit(0);
}
});
contentPane.add(p, "South");
}

public void addButton(Container c, String title, ActionListener a) {
JButton b = new JButton(title);
c.add(b);
b.addActionListener(a);
}

private JPanel canvas;
}

class Ball extends Thread {
public Ball(JPanel b) {
box = b;
}

public void draw() {
Graphics g = box.getGraphics();
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}

public void move() {
if (!box.isVisible())
return;
Graphics g = box.getGraphics();
g.setXORMode(box.getBackground());
g.fillOval(x, y, XSIZE, YSIZE);
x += dx;
y += dy;
Dimension d = box.getSize();
if (x < 0) {
x = 0;
dx = -dx;
}
if (x + XSIZE >= d.width) {
x = d.width - XSIZE;
dx = -dx;
}
if (y < 0) {
y = 0;
dy = -dy;
}
if (y + YSIZE >= d.height) {
y = d.height - YSIZE;
dy = -dy;
}
g.fillOval(x, y, XSIZE, YSIZE);
g.dispose();
}

public void run() {
try {
draw();
for (int i = 1; i <= 1000; i++) {
move();
sleep(5);
}
catch (InterruptedException e) {
}
}

private JPanel box;

private static final int XSIZE = 10;

private static final int YSIZE = 10;

private int x = 0;

private int y = 0;

private int dx = 2;

private int dy = 2;
}

Como criar animações: Paint e Thread

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;

public class Animate extends JFrame {

private static int DELAY = 100;

Insets insets;

Color colors[] = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN,
Color.BLUE, Color.MAGENTA };

public void paint(Graphics g) {
super.paint(g);
if (insets == null) {
insets = getInsets();
}
// Calculate each time in case of resize
int x = insets.left;
int y = insets.top;
int width = getWidth() - insets.left - insets.right;
int height = getHeight() - insets.top - insets.bottom;
int start = 0;
int steps = colors.length;
int stepSize = 360 / steps;
synchronized (colors) {
for (int i = 0; i < steps; i++) {
g.setColor(colors[i]);
g.fillArc(x, y, width, height, start, stepSize);
start += stepSize;
}
}
}

public void go() {
TimerTask task = new TimerTask() {
public void run() {
Color c = colors[0];
synchronized (colors) {
System.arraycopy(colors, 1, colors, 0, colors.length - 1);
colors[colors.length - 1] = c;
}
repaint();
}
};
Timer timer = new Timer();
timer.schedule(task, 0, DELAY);
}

public static void main(String args[]) {
Animate f = new Animate();
f.setSize(200, 200);
f.show();
f.go();
}
}

Is Event Dispatcher Thread

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;

public class IsEDTExample extends JPanel {
private boolean keepRunning;

private static int RED = 0;

private static int BLUE = 1;

private static int GREEN = 2;

private static int VARIABLE = 3;

private static int SIZE = 3;

private int threadShade;

private ColorTableModel tableModel= new ColorTableModel();

private Thread colorShadeThread;

public IsEDTExample() {
JTable table = new JTable(tableModel);
table.setRowHeight(100);
table.setDefaultRenderer(Object.class, new ColorRenderer());
add(table);

add(new JLabel("Thread Color Shade:"));
ButtonGroup group = new ButtonGroup();
JRadioButton redOption = new JRadioButton("Red");
group.add(redOption);
redOption.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
threadShade = RED;
}
});

JRadioButton blueOption = new JRadioButton("Blue");
group.add(blueOption);
blueOption.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
threadShade = BLUE;
}
});

JRadioButton greenOption = new JRadioButton("Green");
group.add(greenOption);
greenOption.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
threadShade = GREEN;
}
});

redOption.setSelected(true);
this.threadShade = RED;

add(redOption);
add(greenOption);
add(blueOption);

add(new JButton(new RandomColorAction()));

this.keepRunning = true;
this.colorShadeThread = new Thread(new RandomColorShadeRunnable());
this.colorShadeThread.start();
}

private class RandomColorAction extends AbstractAction {
public RandomColorAction() {
super("Create Random Color");
}

public void actionPerformed(ActionEvent e) {
IsEDTExample.this.tableModel.generateRandomColor(VARIABLE);
}
}

private class ColorTableModel extends AbstractTableModel {
private Color[][] colors = new Color[3][3];

public ColorTableModel() {
for (int i = 0; i < SIZE; i++) {
for (int x = 0; x < SIZE; x++) {
colors[i][x] = Color.white;
}
}
}

public int getRowCount() {
return SIZE;
}

public int getColumnCount() {
return SIZE;
}

public Object getValueAt(int rowIndex, int columnIndex) {
return colors[rowIndex][columnIndex];
}

public void generateRandomColor(int type) {
Random random = new Random(System.currentTimeMillis());
final int row = random.nextInt(SIZE);
final int column = random.nextInt(SIZE);
final Color color;
if (type == RED) {
color = new Color(random.nextInt(256), 0, 0);
else if (type == BLUE) {
color = new Color(0, 0, random.nextInt(256));
else if (type == GREEN) {
color = new Color(0, random.nextInt(256), 0);
else {
color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}

if (SwingUtilities.isEventDispatchThread()) {
colors[row][column] = color;
fireTableCellUpdated(row, column);
else {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
colors[row][column] = color;
fireTableCellUpdated(row, column);
}
});
}
}
}

private class ColorRenderer implements TableCellRenderer {
private JLabel label;

public ColorRenderer() {
label = new JLabel();
label.setOpaque(true);
label.setPreferredSize(new Dimension(100, 100));
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
label.setBackground((Color) value);
return label;
}
}

private class RandomColorShadeRunnable implements Runnable {
public void run() {
while (keepRunning) {
tableModel.generateRandomColor(threadShade);
try {
Thread.sleep(500);
catch (InterruptedException e) {
}
}
}
}

public static void main(String[] a) {
JFrame f = new JFrame("Is Event Dispatch Thread Example");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new IsEDTExample());
f.pack();
f.setVisible(true);
}

}