Note that there are some explanatory texts on larger screens.

plurals
  1. POShow and start JPanel with drawn animation upon button click
    text
    copied!<p><img src="https://i.stack.imgur.com/z0sDQ.png" alt="enter image description here"></p> <p>I've been up to this the whole time since 24 hrs ago. I have a custom jpanel upon which a simple animation is drawn using timer. What i want is to show this panel at the center between Pikachu and Sasuke (which i have been for so long trying to do until now) such that the animation starts and happens only when this button is clicked.</p> <pre><code>import javax.swing.*; import java.awt.*; import java.awt.event.*; public class App extends JFrame { private static JPanel p53 = new JPanel(); private static JButton fire = new JButton("Attack"); private AttackFX attackfx = new AttackFX(); public App() { fire.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent atk) { p53.add(attackfx); Timer timer1 = new Timer(800, new ActionListener() { @Override public void actionPerformed(ActionEvent ld2) { } }); timer1.start(); timer1.setRepeats(false); } }); } public static void main(String[] a) { App Battleframe = new App(); Battleframe.add(fire); Battleframe.add(p53); Battleframe.setResizable(false); Battleframe.setTitle("OnFight.Combat_Arena_Beta"); Battleframe.setSize(381, 400); Battleframe.setLocationRelativeTo(null); Battleframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Battleframe.setVisible(true); } class AttackFX extends JPanel { private int xCoor = 1; public AttackFX() { Timer tm1 = new Timer(100, new TimerListener2()); tm1.start(); tm1.setRepeats(true); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); xCoor += 1; g.setColor(Color.cyan); g.fillOval(xCoor, 40, 8, 8); } class TimerListener2 implements ActionListener { @Override public void actionPerformed(ActionEvent e) { repaint(); } } } } </code></pre> <p>I really need your help on this sirs. Tomorrow's a presentation of this whole program I'm making. And this is the last step to accomplishing it. I've tried all I can to solve this on my own but to no success. </p>
 

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