hey there. I have been struggling with separating classes in java and figuring out what methods go in what classes. when i build my apps in one file, i do them right, but when i try to separate classes, i mess up. like for example:
I have these two classes:
1.

import javax.swing.*;
import java.awt.*;
    public class Bully extends JPanel{
       public Bully(){
         JFrame korniza = new JFrame();
         korniza.getContentPane().add(this);
         korniza.setSize(500, 500);
         korniza.setTitle("Bull's Eye");
         korniza.setVisible(true);
      }
   
       public void paintComponent(Graphics g){
 	      g.setColor(Color.yellow);
         g.fillRect(0, 0, 500, 500);
         //Rrathet(g);
			
			Testi aa = new Testi();
			aa.paintsquares(20, 20, 20, 400, g);
			Tooopi bb = new Tooopi(); 
			bb.tupi(g);
      }
   		
             }
   
       public static void main(String[] args){
         new Bully();
		
      }
   }

and 2.

import javax.swing.*;
import java.awt.*;

public class Tooopi extends JPanel{
private int x_posita = 215;
private int y_posita = 215;
private int diametri = 10;
private int a = -1;
private int b = 3;
private int c = 2;

/*
public Tooopi(){
JFrame korniza = new JFrame();
korniza.getContentPane().add(this);
korniza.setSize(550, 550);
korniza.setVisible(true);
korniza.setTitle("toooopi");
}

public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, 550, 550);
toopi(g);
}*/
public void tupi(Graphics g){
g.setColor(Color.blue);
g.fillOval(x_posita, y_posita, diametri, diametri);
x_posita = x_posita + a;
y_posita = y_posita + b;
diametri = diametri + c;
if(y_posita == 215 || y_posita == 335){
a = -a;
b = - b;
c = -c;
}
 try { Thread.sleep(20);}
catch (InterruptedException e) { }
repaint();
}

/*public static void main(String[] args){
new Tooopi();
}
*/
}

my third class is Testi, it paints the squared bull's eye.

import java.awt.*;

/** paintSquares paints n squares across the left-to-right diagonal
* of a graphics window
* @param x_position - of the upper left corner of the first square
* @param y_position - of the upper left corner of the first square
* @param n - the number of squares to paint
* @size - the width of each square
* @param g - the graphics pen */

public class Testi{
public void paintsquares(int x_position, int y_position, int n, int size, Graphics g){
int new_x = x_position;
int new_y = x_position;
int count = 0;
int length = size;
int dif = size/n;
Color col = Color.red;
while(count != n){
g.setColor(col);
g.fillRect(new_x, new_y, length, length);
count = count + 1;
length = length - dif;
new_x = new_x + dif/2;
new_y = new_y + dif/2;
if(col == Color.red){col = Color.black;}
else if(col == Color.black){col = Color.white;}
else{col = Color.red;}
}
}
}

the effect I am trying to reach is this: in a window should appear a bull's eye made of squares, in stead of circles, in red, black, and white colors, with a blue ball moving up and down between the center of the bull's eye and its bottom.

I have reached that effect putting my methods in a single class. however, I am not content as I can't figure out how to use the OOP paradigm.

what I get using the two classes posted above is a bull's eye with a small ball in its center, which does not move. please help if you have any idea how I would make the ball move up and down using these two classes.

what is Testi() ?? need more info.

Your code looks ok to me, except I think you need to override the paintComponent method in the Tooopi class with the code you have in the tupi method? I can't see where the tupi method is being called at the moment...

@zyaday:
I just posted my third class Testi, as well. it paints the squared bull's eye.

@darkagn.
I added a System.out.println(variable); statment inside my Tooopi class and verified that the tupi class is only being ran once. which means that the variables inside the class remain the way they are when initialized resulting in a static ball.

What error or warnings do you get?

I do not get any error. The reults is not what I expect it to be. I expect to see a ball that moves up and down -- the lower down it is, it grows bigger, and the higher up it is, it grows smaller, thus looking as if it was bouncing and I was watching it from above. Instead, what I get is a ball that does not bounce. If you've copied the classes, you only need to do a few changes in your countroller class (Bully) to see the effect.

add a repaint(); statment in paintcomponent.
instatiate the following methods out of the paintcomponent:
Testi aa = new Testi();
Tooopi bb = new Tooopi();

however, in this case, the background of the picture you see is repainted all the time. I wonder if there is a way to reach the effect, using separate classes, without a repaint inside paintComponent.

on the other hand, when I built the application using one single class, I built it without a repaint inside my paintComponent, which I suppose is more efficient use of memory resources.

Are you sure that ther is any arror in your code?
In fact there is:
Look at your Bully class:
The main fuction is declared out ouf the calss and there is an orhpelan "}" .

try to chage this class as follow:

import javax.swing.*;
import java.awt.*;

public class Bully extends JPanel {

    public Bully() {
        JFrame korniza = new JFrame();
        korniza.getContentPane().add(this);
        korniza.setSize(500, 500);
        korniza.setTitle("Bull's Eye");
        korniza.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.yellow);
        g.fillRect(0, 0, 500, 500);
        //Rrathet(g);

        Testi aa = new Testi();
        aa.paintsquares(20, 20, 20, 400, g);
        Tooopi bb = new Tooopi();
        bb.tupi(g);
    }

    public static void main(String[] args) {
        new Bully();

    }
}

Hope it helps.

@moutanna:
I copied and pasted your code. the ball remains static. it won't move. the class I posted above, with minor modifications (adding a repaint inside paintComponent), works fine. but if I build the app in a single file, it works fine without a repaint in paintcomponent. this is the problem.

I don't understand why you are using the repaint function. What happens when you use the two classes without it?

bibiki use Runnable interface with method run() , an start new Thread

import javax.swing.*;
import java.awt.*;

public class Bully extends JPanel implements Runnable {

    private Testi aa;
    private Tooopi bb;

    public Bully() {
        JFrame korniza = new JFrame();
        korniza.getContentPane().add(this);
        korniza.setSize(500, 500);
        korniza.setTitle("Bull's Eye");
        korniza.setVisible(true);
        korniza.setDefaultCloseOperation(3);
        aa = new Testi();
        bb = new Tooopi();
        //
        Thread th = new Thread(this);
        th.start();
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.yellow);
        g.fillRect(0, 0, 500, 500);
        aa.paintsquares(20, 20, 20, 400, g);
        bb.tupi(g);
    }

    public void run() {
        while (true) {
            repaint();
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                //
            }
        }
    }

    public static void main(String[] args) {
        new Bully();
    }
}

or use: TimerTask,Timer classes
http://www.java-tips.org/java-se-tips/java.awt/how-to-create-animation-paint-and-thread.html

quuba, I just tried your code. it works, but with a minor defect. teh ball moves too slowly. I set the Thread.sleep to 0 (Thread.sleep(0)) and it worked just fine. however, I tried removing entirely the try-catch and the app failed to work. I would appreciate some explanation on what makes the try-catch necessary and a little more insight onto what is going on 'under the hood'. if that's too much, never mind. thanks alot for your answer. I surely will analyze it further.

In class Tooopi comment lines 37-40

//try { Thread.sleep(20);}
//catch (InterruptedException e) { }
//repaint();
//}

#11 line 34
change to

Thread.sleep(20);

20ms== 50Hz
10ms==100Hz
Values less then 10 are not good. Slows down other tasks.
//
Write your own vession with TimerTask and Timer classes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.