package com.yourcompany.struts.action;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JOptionPane;
public class MyActLit extends Applet implements ActionListener {
Toolkit tool;
Image image;
Button bu1;
Button bu2;
Button bu3;
Button bu4;
TextField text;
TextField text1;
int x1 = 0;
int y1 = 0;
int x2 = 600;
int width = 400;
int heigh = 20643;
int speed = 1;
int speed1 = 1000;
int x3 = 1050;
int y3 = 10;
boolean sd = false;
Thread th;
public void init() {
tool = getToolkit();
image = tool.getImage("E://1.jpg");
bu1 = new Button("向下");
bu2 = new Button("向上");
bu3 = new Button("确认自动滚动速度");
bu4 = new Button("确认手动滚动速度");
text = new TextField("1");
text1 = new TextField("1000");
this.add(bu1);
this.add(bu2);
this.add(text);
this.add(bu3);
this.add(text1);
this.add(bu4);
bu1.addActionListener(this);
bu2.addActionListener(this);
bu3.addActionListener(this);
bu4.addActionListener(this);
th = new dd();
th.start();
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.black);
g.drawImage(image, 10, 10, 450, 910, x1, y1, x2, y1 + 900, this);
g.drawImage(image, 500, 10, 550, 910, 0, 0, width, heigh, this);
g.setColor(Color.red);
g.drawRect(495, y3, 59, 40);
}
public boolean validatelenth() {
boolean s = true;
if (y1 + 1000 > heigh || y1 < 0) {
s = false;
}
return s;
}
public void validateSpeed() {
}
public void actionPerformed(ActionEvent ac) {
// TODO Auto-generated method stub
Button bnt = (Button) ac.getSource();
try {
if (bnt == bu1) {
y1 += speed1;
} else if (bnt == bu2) {
y1 -= speed1;
} else if (bnt == bu3) {
speed = Integer.valueOf(text.getText());
} else if (bnt == bu4) {
speed1 = Integer.valueOf(text1.getText());
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "输入的为非数字,确认后再点", "错误", JOptionPane.ERROR_MESSAGE);
}
if (!validatelenth())
return;
y3 = (900 * (y1) / (heigh - 900));
repaint();
}
class dd extends Thread {
// TODO Auto-generated method stub
public void run() {
while (1 > 0) {
if (sd) {
y1 -= speed;
} else {
y1 += speed;
}
y3 = (900 * (y1) / (heigh - 900));
if (!validatelenth()) {
if (!sd) {
sd = true;
} else {
sd = false;
}
}
repaint();
try {
Thread.sleep(238);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public Thread getTh() {
return th;
}
public void setTh(Thread th) {
this.th = th;
}
}