import java.awt.*;
import java.awt.event.*;
import javax.swing.*;import org.w3c.dom.css.RGBColor;public class draw {
public static void main(String[] args) {
new Paint();
}
}
class Paint extends JFrame{
Toolkit tk=Toolkit.getDefaultToolkit();
Paint(){
Container con=this.getContentPane();
this.setTitle("实验");
this.setBounds((tk.getScreenSize().width-800)/2, (tk.getScreenSize().height-600)/2, 800, 600);
// con.setBackground(Color.black);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
int fx=getX();
int fy=getY();
g.setColor(new Color(14, 190, 58));
while(fy<getY()+getSize().height){
g.fillOval(fx, fy, 10, 10);
fy=fy+10;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.awt.event.*;
import javax.swing.*;import org.w3c.dom.css.RGBColor;public class draw {
public static void main(String[] args) {
new Paint();
}
}
class Paint extends JFrame{
Toolkit tk=Toolkit.getDefaultToolkit();
Paint(){
Container con=this.getContentPane();
this.setTitle("实验");
this.setBounds((tk.getScreenSize().width-800)/2, (tk.getScreenSize().height-600)/2, 800, 600);
// con.setBackground(Color.black);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
int fx=getX();
int fy=getY();
g.setColor(new Color(14, 190, 58));
while(fy<getY()+getSize().height){
g.fillOval(fx, fy, 10, 10);
fy=fy+10;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
int fx=getX();
int fy=getY();
这个分别获取的是这个窗口在屏幕上的x和ywhile(fy<getY()+getSize().height){
g.fillOval(fx, fy, 10, 10);
fy=fy+10;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后这段干的是:在fy小于 窗体的y + 窗体高度 的时候
从窗体的x,y这个位置开始每隔100毫秒画一个直径为10的圆这个地方我估计是你的理解有问题:
getX(),和getY()是获取窗口相对于屏幕左上角的坐标,比如现在是100,200
然后你在窗口的100,200这个位置上开始画东西
也就是你开始画的点距离屏幕左上角是200,400
这个代码看起很奇怪,不知道你需求是不是这样
int fx=getX();
int fy=getY();
你这定义在了方法内部,所以每次调用这个方法的时候,fx和fy都等于初始化了一次。这样paint(g)调用了两次自然初始化了2次,当然不对了。
你把int fx和fy定义在方法外部,然后再内部给赋值就行了。
现在搞javaEE的多,SE的市场小多了,现在问老师们SE的问题,有时候人家都懒得回答了。