I have code supposes to draw ring in random position within JPanel but for some reason my ring sometimes gets out of this bounds, for instance, it appears in bottom half-cut.
As far as I understand, I have JPanel lying within JFrame, in order to be drawn within this boundaries, my ring should be drawn with this expression:
xRing = (int)((frameWidth -ringSize)* (Math.random()));
yRing = (int)((frameHeight-ringSize)*(Math.random()));
setParamsRing(xRing, yRing, ringSize);
What is the problem?
Here is all code:
public class mull extends JPanel implements ActionListener{
private static JPanel p;
Timer timer;
ActionListener updateProBar;
private double esize;
private double maxSize = 0;
private boolean initialize = true;
private int squareX = 50;
private int squareY = 50;
private int squareSize = 200;
private int ringX = 50;
private int ringY = 50;
private int ringSize = 100;
private static int frameWidth = 300;
private static int frameHeight = 300;
private int jPanelHeight;
private int jPanelWidth;
private int i = 0;
public mull() {
timer = new Timer(200, this);
timer.setInitialDelay(200);
timer.start();
}
int xRing;
int yRing;
@Override
public void actionPerformed(ActionEvent e) {
xRing = (int)((frameWidth -ringSize)* (Math.random()));
yRing = (int)((frameHeight-ringSize)*(Math.random()));
setParamsRing(xRing, yRing, ringSize);
repaint();
}
// public void getJPanelSize(){
// Rectangle r = p.getBounds();
// jPanelHeight = r.height;
// jPanelWidth = r.width;
// }
public void setParamsRing(int xpos, int ypos, int size){
this.ringX = xpos;
this.ringY = ypos;
this.ringSize = size;
}
public void setParamsSquare(int xpos, int ypos, int size){
this.squareX = xpos;
this.squareY = ypos;
this.squareSize = size;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawOval(ringX, ringY, ringSize, ringSize);
g.setColor(Color.BLUE );
g.fillOval(ringX, ringY, ringSize, ringSize);
// g.setColor(Color.BLACK);
// g.drawRect(ringX, ringY, ringSize, ringSize);
// g.setColor(Color.BLUE );
// g.fillRect(ringX, ringY, ringSize, ringSize);
}
public static void main(String[] args) throws InterruptedException {
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new JPanel();
p.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
// moveSquare(e.getX(),e.getY());
}
});
f.add(p);
f.add(new mull());
f.setSize(frameWidth, frameHeight);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
Thanks this approach worked!
I need to draw both ring and square but not to overlap each other.
What is the issue? My code seems not to work (although it seems to be correct):
@Override
public void actionPerformed(ActionEvent e) {
xRing = (int)((getWidth() -ringSize)* (Math.random()));
yRing = (int)((getHeight()-ringSize)*(Math.random()));
xSquare = (int)((getWidth() -squareSize)* (Math.random()));
ySquare = (int)((getHeight()-squareSize)*(Math.random()));
while( (xSquare>=xRing && xSquare<=ringSize) && (ySquare>=yRing && ySquare<=ringSize) ){
xSquare = (int)((getWidth() -squareSize)* (Math.random()));
ySquare = (int)((getHeight()-squareSize)*(Math.random()));
}
setParamsRing(xRing, yRing, ringSize);
setParamsSquare(xSquare, ySquare, squareSize);
repaint();
}
The problem in your code is, that the panel you are painting on is not of dimensions
frameWidthxframeHeightbut smaller. The frame’s bounds are set to these values but you have to consider the title bar and border which makes your actual panel a bit smaller than that.You can instead use the panel`s width and height: