我有一個動畫類,它只畫兩個圓圈和一個線條(無主要方法),在另一個類中我有一個主要方法,它將動畫類作爲對象傳遞,並應顯示我繪製了兩個圓圈,但沒有顯示,它只顯示窗口中沒有任何圓圈或我已經完成的線條,如果我將主要方法放在我的動畫類中,它將完美地工作,這是一個不知怎的,用戶錯誤,但我不知道什麼或爲什麼。JApplet沒有顯示繪畫或繪製方法
單獨的類中的動畫方法。
import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.Graphics.*;
import javax.swing.JApplet;
public class Animation extends JApplet{
public void init(){
}
public static void createAndShowGUI(JApplet j)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setSize(500,500);
frame.getContentPane().add("Center", j);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
int x=50;
int y=10;
g2.setPaint(Color.black);
g2.draw(new Line2D.Double(x,y,50,400));
drawT(g2);
drawH(g2);
//create a method that translates
}
public void drawH(Graphics2D g2)
{
int y=25;
g2.setColor(Color.blue);
drawCircle(y,g2);
}
public void drawT(Graphics2D g2){
int y=100;
g2.setColor(Color.green);
drawCircle(y,g2);
}/*
public void raceLoop(){
long startingTime = System.currentTimeMillis();
long cumTime=startingTime;
while(mve.hposition<70){
long timePassed = System.currentTimeMillis()-cumTime;
cumTime += timePassed;
//mve.update(timePassed);
}
}*/
public void drawCircle(int y, Graphics2D g2)
{
g2.fillOval(50,y,50,50);
}
}
Main方法
import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.Graphics.*;
import javax.swing.JApplet;
public class Race {
public static void main(String[] args) {
JApplet applet = new JApplet();
Animation animie = new Animation();
animie.createAndShowGUI(applet);
}
}
感謝您的信息有網頁指示,我使用JApplet的各種tutoritals,因此ÿ我用它。一個特殊的例子是類動畫擴展JApplet實現的Runnable,這是我不得不做的後我不知道我將如何做與JPanel,除非當然它是相同的過程 –
這幾乎是相同的過程,只是改爲它開始包含在一個小程序,你必須提供自己的幀 – MadProgrammer