i figured it out lol. the try was outside of the while loop. Another question tho... i created a new class file in order to stop confusing myself, so how do i import that into the main file i am using?
oh figured that out too. now another...
i have this code
[source lang="java"]import game.java.createDOTS;import java.applet.*;import java.awt.*;import javax.swing.*;import java.awt.geom.*;import java.util.concurrent.*;public class game extends Applet implements Runnable{ int xpos = 100; int ypos = 100; int radius = 5; int xspeed = 0; int yspeed = 0; int randomx; int randomy; static final int WIDTH = 400; static final int HEIGHT = 400; private Image dbImage; private Graphics dbg; public void update(Graphics g) { if(dbImage == null) { dbImage = createImage(this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics(); } dbg.setColor(getBackground()); dbg.fillRect(0, 0, this.getSize().width, this.getSize().height); dbg.setColor(getForeground()); paint(dbg); g.drawImage(dbImage, 0, 0, this); } public void init() { this.setSize(WIDTH, HEIGHT); } public void start() { Thread th = new Thread(this); th.start();//start main game Thread createDOTS = new createDOTS(); createDOTS.start(); System.out.println(randomx); System.out.println(randomy); } public void stop() { } public void destroy() { } public boolean keyDown (Event e, int key) { if(key == Event.LEFT) { xspeed = -5; yspeed = 0; } if(key == Event.RIGHT) { xspeed = 5; yspeed = 0; } if(key == Event.UP) { yspeed = -5; xspeed = 0; } if(key == Event.DOWN) { yspeed = 5; xspeed = 0; } return true; } public void run() { while(true) { Thread.currentThread().setPriority(Thread.MAX_PRIORITY); repaint(); if (xpos < 1) { xpos = 399; } if (xpos > 399) { xpos = 1; } if (ypos < 1) { ypos = 399; } if (ypos > 399) { ypos = 1; } ypos += yspeed; xpos += xspeed; try { Thread.sleep(20); } catch(InterruptedException ex) { } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void paint(Graphics g) { g.setColor(Color.black); g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius); }}[/source]
with this class in another file
[source lang="java"]package game.java;import java.applet.*;import java.awt.*;public class createDOTS extends Thread //create new dots every 10 seconds at random points on the screen{ int randomx; int randomy; int high = 399; int low = 1; int dotradius = 10; public void create_dots(int randomx, int randomy, Graphics g) { while(true) { randomx = (int)(Math.random() * (high - low + 1)) + low;//create random x randomy = (int)(Math.random() * (high - low + 1)) + low;//create random y System.out.println(randomx + " " + randomy); g.setColor(Color.black); g.fillOval(randomx, randomy, dotradius * 2, dotradius * 2); try { Thread.sleep(5000); }catch(InterruptedException e){} } }}[/source]
and what i want to do with the createDOTS class is generate a random coordinate every 5 seconds and place a dot on the screen in that coordinate, but its not working and im not sure what i have to do to get it to work
Edited by burnt_casadilla, 05 August 2012 - 07:02 PM.