I am a student and having trubble making one of my projects this is only one bit of what my final project will actual be like but i am trying to compartmentalize it as much as possible.
the specific problem I have is related to creating a java application that displays a frame with a button allowing the user to create multiple balls on screen and then have you be able to select each one by clicking on them.
I have tryed to add small modifications but with non of them working i removed it so that it gives you more freedom when finding a solution.
Here is the code of Simulation
class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Simulation {
int noOfBallClicks = 0;
Simulation() {
buildTheGUI();
}
JFrame frame = new JFrame();
JFrame frame2 = new JFrame();
JPanel panal = new JPanel();
JButton add = new JButton("add a new object");
public void buildTheGUI() {
frame.setVisible(true);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panal);
panal.add(add);
add.addActionListener(new ButtonClickHandler());
}
public static void main(String[] args) {
new Simulation();
}
class ButtonClickHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
noOfBallClicks = noOfBallClicks++;
frame.add(new Ball());
frame.validate();
}
}
}
Here is the code of Ball
class:
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class Ball extends JPanel {
private int x;
private int y;
private int w;
private int h;
Ball() {
this.x = 200;
this.y = 200;
this.w = 100;
this.h = 100;
}
Ball(int a) {
Random rand = new Random();
this.w = 100;
this.h = 100;
this.x = rand.nextInt(300);
this.y = rand.nextInt(300);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(x, y, h, w);
}
}
You never mention what is wrong/what you want to achieve... Besides the above and title: selecting objects, that have been repainted
However I see a few problems in the code given:
1) Dont set
JFrame
visible until all components have been added (or you will have problems such as content not being visible)2) Dont call
setSize
onJFrame
rather overridegetPreferredSize()
ofJPanel
and returnDimension
s which fit the drawings onGraphics
object and/or use an appropriateLayoutManager
. Than we can callpack()
instead ofsetSize(...)
onJFrame
.3) Swing components should be created and manipulated on Event Dispatch Thread via
SwingUtilities.invokeLater(Runnable r)
block. Read Concurrency in Swing for more.4) When you use
validate()
it should be followed by a call torepaint()
to reflect changes made.5) You are also using a default
JFrame
Layout ofBorderLayout
to which you add a panel, and than balls (using the button listener) by defaultBorderLayout
will add to itsBorderLayout.CENTER
so on each call toJFrame#add(Component c)
you are replacing the old ball/JPanel
with another.6) As it stands using
JPanel
like you have if 2Ball
s end up in an overlapping position the topBall
and itsJPanel
will cover the bottomBall
... You need a transparent panel i.eJComponent#setOpaque(false)
.7) When I do custom painting, I rarely use
JComponent
or its extensions. I rather create anObject
which will act as virtual representation of what I need to be drawn/displayed or whatever (maybe personal preference). These objects will than be visualized in myJPanel
paintComponent(..)
which will call theirdraw(..)
method passing theGraphics
object of theJPanel
to eachBall
which will than draw itself according to the the fields data.Here is a short example I made (with the above in mind):
All green
balls
have been selected i.e clicked on, while the red has not.