Friday 15 January 2010

java - Initializing an existing object -



java - Initializing an existing object -

i trying initialize paintcall global variable ballgui object exists. have able phone call repaint method in run function multithreadng new gui isnt made each individual ball. imports left out space.

import java.awt.color; public class ballt extends thread implements runnable { private static int xcord, ycord, ychange, xchange; private static boolean xup; private static boolean yup; private color color; private ballgui paintcall=//? existing object ballgui ; public ballt(int x, int y) { yup = false; xup = false; xcord = x; ycord = y; color = new color((int) math.random(), (int) math.random(), (int) math.random()); } public color getcolor() { homecoming color; } public int getx() { homecoming xcord; } public int gety() { homecoming ycord; } public void run() { while (true) { seek { thread.sleep(50); } grab (interruptedexception e) { } if (xup == true) { xcord = xcord + xchange; } else { xcord = xcord - xchange; } if (yup == true) { ycord = ycord + ychange; } else { ycord = ycord - ychange; } if (xcord <= 0) { xup = true; xchange = (int) math.random() * 5; } else if (xcord > 340) { xup = false; xchange = (int) math.random() * 5; } if (ycord <= 0) { yup = true; ychange = (int) math.random() * 5; } else if (ycord > 340) { yup = false; ychange = (int) math.random() * 5; } paintcall.repaint(); } } } @suppresswarnings("serial") public class ballgui extends jframe { public jpanel ballappear; private final int maxballs = 20; private ballt[] balls; private int count; private executorservice threadpool = executors.newcachedthreadpool(); public ballgui() { super("bouncing"); ballappear = new jpanel(); count = 0; balls = new ballt[20]; addmouselistener(new mouseadapter() { public void mousepressed(mouseevent e) { makeball(e); } }); add(ballappear); } private void makeball(mouseevent e) { if (count < maxballs - 1) { int x = e.getx(); int y = e.gety(); balls[count] = new ballt(x, y); threadpool.execute(balls[count]); count++; } } @override public void paint(graphics g) { super.paint(g); (int = 0; < count; i++) { g.setcolor(color.black); g.filloval(balls[i].getx(), 340, 10, 10); g.setcolor(balls[i].getcolor()); g.filloval(balls[i].getx(), balls[i].gety(), 10, 10); } } }

firstly simple solution - alter ballt constructor take ballgui instance:

public ballt(int x, int y, ballgui ballgui) { this.paintcall = ballgui; yup = false; //...

next, pass in instance during construction in gui object:

private void makeball(mouseevent e) { if (count < maxballs - 1) { // ... balls[count] = new ballt(x, y, this); } }

however, front end according model-view-controller (mvc) design practice. generally, view (ballgui) should know model (ballt) model should ignorant of view. ballgui maintains list of ballt's presumably queries in paint() method. consider having controller had responsibility calling updateposition() method on each ballt in turn , calling repaint() on gui. have advantage aren't creating new thread each ball, need 1 controller thread.

java multithreading object global-variables

No comments:

Post a Comment