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.