Note that there are some explanatory texts on larger screens.

plurals
  1. POJava- starting thread issue
    primarykey
    data
    text
    <p>Here's part of my simple code.This code contains moveable oval and static oval with coords <code>newX=100,newY=100</code>.I'm trying to achieve moving that movable oval automatically after hitting left mouse button.Move code lines are in new Thread <code>thread</code>.Thrad really starts with clicking mouse button, but nothing happens.Just after doing one move using arrow keys, oval starts moving.I tried calling <code>repaint()</code> method at different places but it doesn't seem to help.Any suggestions? Thank you!</p> <pre><code>public class Buffer extends JPanel implements Runnable,KeyListener,MouseListener{ public static int x; public static int y; public static int newX; public static int newY; public static Thread thread; public static boolean check; public static JFrame frame; public static int pointX; public static int pointY; public static boolean repaint; public void paintComponent(Graphics g){ super.paintComponent(g); g.drawOval(x, y, 20, 20); newX=100; newY=100; g.fillOval(newX, newY, 20, 20); if(repaint) repaint(); } public static void main(String args[]){ Buffer z=new Buffer(); z.setBackground(Color.white); frame=new JFrame(); frame.setSize(500,500); frame.addKeyListener(z); frame.addMouseListener(z); frame.add(z); frame.setVisible(true); frame.requestFocusInWindow(); thread=new Thread(){ public void run(){ try{ for(int i=0;i&lt;=5;i++){ x=x+i; repaint=true; thread.sleep(1000); } }catch(InterruptedException v){System.out.println(v);} } }; } public void keyPressed(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_LEFT){ x=x-10; repaint(); } if(e.getKeyCode()==KeyEvent.VK_RIGHT){ x=x+10; repaint(); } if(e.getKeyCode()==KeyEvent.VK_UP){ y=y-10; repaint(); } if(e.getKeyCode()==KeyEvent.VK_DOWN){ y=y+10; repaint(); } } public void mouseClicked(MouseEvent e) { thread.start(); } } </code></pre>
    singulars
    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.
    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