Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp with creating a complex Swing GUI with animation
    primarykey
    data
    text
    <p>This is my first non-school related program. I have a few questions that you guys can hopefully answer with ease. I have 3 questions. How can I add my button to my JFrame even though it's in a different class than the button?</p> <p>Also, how would I go about making my shape and ten others like it about a quarter second after each other so I had a line of them. </p> <p>Then, how would I force them to follow a predetermined path that scales to somebody dragging the box around?</p> <p>Thanks a lot for reading and helping me out guys. Here are my three classes:</p> <p>gameRunner.java</p> <pre><code>import javax.swing.JFrame; public class gameRunner { public static void main(String args []){ Enemy e = new Enemy(); Buttons b = new Buttons(); JFrame f = new JFrame(); f.add(b); f.add(e); f.setVisible(true); f.setSize(1300, 700); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle("Tower Defense"); } } </code></pre> <p>Enemy.java </p> <pre><code>import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Rectangle2D; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.Timer; public class Enemy extends JPanel implements ActionListener { Timer t = new Timer(5, this); double x = 0; double y = 0; double velX = 3; double velY = .5; int health = 10; public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; Rectangle2D square = new Rectangle2D.Double(x, y, 10, 10); g2.fill(square); t.start(); } public double adjustHorizontalSpeed() { y += velY; return y; } public double adjustVerticalSpeed() { x += velX; return x; } public void actionPerformed(ActionEvent e) { adjustHorizontalSpeed(); adjustVerticalSpeed(); repaint(); } } </code></pre> <p>Buttons.java </p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JFrame; public class Buttons extends JFrame implements ActionListener{ private JButton shoot; public Buttons(){ shoot = new JButton("Shoot!"); shoot.setBounds(50,60,50,100); shoot.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload