我已經作出了這個節目能夠吸引的使用這兩個類繪製多個移動圖形
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.Timer;
public class move extends JPanel implements ActionListener
{
Timer t = new Timer(7, this);
int x = 10, y = 10, velX = 7, velY = 7;
public void paintComponent(Graphics g, Graphics h)
{
super.paintComponent(h);
super.paintComponent(g);
System.out.println(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D circle = new Ellipse2D.Double(x, y, 40, 40);
g2.fill(circle);
t.start();
}
public void actionPerformed(ActionEvent e) {
if(x<0 || x > getWidth())
{
velX = -velX;
}
if(y < 0 || y > getHeight())
{
velY = -velY;
}
x += velX;
y += velY;
repaint();
}
}
該類只是簡單地繪製球,周圍的屏幕小球彈跳的實例,並提供邏輯爲計時器,這種
import java.awt.Color;
import javax.swing.JFrame;
public class Gui {
public static void main(String[] args)
{
move s = new move();
JFrame f = new JFrame("move");
f.add(s);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1000, 1000);
f.setTitle("Moving Circle");
f.setBackground(Color.GREEN);
}
}
接下來的這個類只是把它全部在一個JFrame,很簡單的東西,我知道,但我只是想同的JFrame內繪製多個實例。我只是嘗試瞭解我的代碼知識,一些代碼實現會很棒。
如何繪製多個移動圖形?
'public void paintComponent(Graphics g,Graphics h)'And ...什麼調用該代碼? –
毫無疑問,您會喜歡通過@trashgod瀏覽[動力學模型](https://sites.google.com/site/drjohnbmatthews/kineticmodel)。 –
+1 GagandeepBali很好找。 @ user1808763另請參閱此處的一些遊戲邏輯示例:http://stackoverflow.com/questions/13999506/threads-with-key-bindings/14001011#14001011 –