2013-06-01 12:40:04Morris
[JAVA][作業] 做個拉霸?
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.awt.LinearGradientPaint;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.util.Timer;
import java.util.TimerTask;
public class DrawMain extends JFrame {
Image img[] = new Image[10];
private String imgNames[] = { "zero.png", "one.png", "two.png",
"three.png", "four.png", "five.png", "six.png", "seven.png",
"eight.png", "nine.png" };
private MediaTracker tracker = null;
DigitPanel[] dpanel = new DigitPanel[8];
public void loadImages() {
tracker = new MediaTracker(this);
try {
for (int i = 0; i < imgNames.length; i++) {
img[i] = java.awt.Toolkit.getDefaultToolkit().getImage(
imgNames[i]);
tracker.addImage(img[i], i);
}
} catch (Exception e) {
System.err.println("Error loading image");
}
try {
tracker.waitForAll();
} catch (Exception e) {
System.err.println("Unknown error while loading images");
}
}
public DrawMain() {
loadImages();
this.setTitle("DrawMain");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setResizable(false);
this.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 8));
for (int i = 0; i < 8; i++) {
dpanel[i] = new DigitPanel();
dpanel[i].runnable = false;
panel.add(dpanel[i]);
}
JButton button = new JButton("NEXT");
button.addActionListener(new ActionListener() {
int idx = 0;
public void actionPerformed(ActionEvent e) {
dpanel[idx].showNumber = (int) (Math.random() * 10);
dpanel[idx].count = 5;
dpanel[idx].runnable = true;
idx = (idx + 1) % 8;
}
});
this.add(panel, BorderLayout.CENTER);
this.add(button, BorderLayout.SOUTH);
this.setVisible(true);
}
public class DigitPanel extends JPanel {
int showNumber;
int[] X = new int[10];
int[] Y = new int[10];
boolean runnable;
int count;
Timer timer = new Timer();
public DigitPanel() {
for (int i = 0; i < 10; i++) {
X[i] = 0;
Y[i] = (9 - i) * 200;
}
timer.schedule(new Heart(), 1000, 10);
runnable = true;
}
public class Heart extends TimerTask {
public void run() {
if (runnable == false)
return;
for (int i = 0; i < 10; i++) {
Y[i] = Y[i] + 25;
Y[i] %= 2000;
}
repaint();
}
}
public void setShowNumber(int x) {
this.showNumber = x;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Point2D start = new Point2D.Float(this.getWidth(), 0);
Point2D end = new Point2D.Float(this.getWidth(), this.getHeight());
float[] dist = { 0.0f, 0.40f, 0.80f, 1.0f };
Color c1 = Color.BLACK, c2 = Color.WHITE, c3 = Color.WHITE, c4 = Color.BLACK;
Color[] colors = { c1, c2, c3, c4 };
LinearGradientPaint p = new LinearGradientPaint(start, end, dist,
colors);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
int pcount = count;
for (int i = 0; i < 10; i++) {
g.drawImage(img[i], X[i], Y[i], 100, 200, null);
if (Y[i] == 200 && i == showNumber) {
count--;
if (count == 0)
runnable = false;
}
}
if (pcount != count) {
timer.cancel();
timer = new Timer();
timer.schedule(new Heart(), 50, 10 * (5 - count));
}
}
}
public static void main(String[] args) {
new DrawMain();
}
}
北歐巨狼
2013-08-06 21:42:33
請問貴版主是用那一套JAVA軟體
版主回應
eclipse+jdk1.7.0_15
2013-08-07 06:44:21
Thank you so much for sharing all this wonderful information !!!! It is so appreciated!! You have good humor in your blogs. So much helpful and easy to read!
https://www.sevenmentor.com/java-classes-in-bangalore