Click here to Skip to main content
15,895,746 members

Comments by naeemashfaq (Top 4 by date)

naeemashfaq 4-Oct-12 13:47pm View    
I got it from the internet. I am really grateful to you for giving me your time.
I tried your code but it also overwrites the 1st object. When I tried the different layouts of JFrame, it shows both objects but the second object disappears after 2 or 3 seconds. I am copying the code.

public class DrawRect extends JFrame{

public static void main(String[] args)
{

JFrame f = new JFrame();
Ballbewegung ball = new Ballbewegung(10,100);
Ballbewegung ball1 = new Ballbewegung(15,300);
f.getContentPane().add(ball, BorderLayout.CENTER);
f.getContentPane().add(ball1, BorderLayout.WEST);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 500);
f.setVisible(true);
}
}
naeemashfaq 4-Oct-12 6:56am View    
This is my class that creates a circle shape with specific x and y coordinates. Similarly I have another same class but with different x and y coordinate positions. I create 1 object of each class and want to display them on specific positions. I tried the code, it works well with your example but not working with my classes.
class Ballbewegung2 extends JPanel implements Runnable
{
// Initialisierung der Variablen
int x_pos = 10; // x - Position des Balles
int y_pos = 100; // y - Position des Balles
int radius = 20; // Radius des Balles
public void init()
{
setBackground (Color.blue);
}
public void start ()
{
Thread th = new Thread (this);
th.start ();
}
public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
x_pos ++;
if(x_pos >= 400)
x_pos = 10;
repaint();
try
{
Thread.sleep (20);
}
catch (InterruptedException ex)
{}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint (Graphics g)
{
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
}
naeemashfaq 3-Oct-12 15:57pm View    
I am from Sweden. I am new to Java programming. I am using the same Border Layout but the coordinates where I am displaying the objects are different. I tried this code but it also shows last object. My classes are extending JPanel.then how can I show both objects?
naeemashfaq 3-Oct-12 15:10pm View    
Thanks for replying. How can I have direct extension to Jpanel? I have also tried with default layout but all in vain. I am also unable to view the code