作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有代码假设在 JPanel 内的随机位置绘制环,但由于某种原因,我的环有时会超出此范围,例如,它出现在下半切中。据我了解,我有 JPanel 位于 JFrame 内,为了在这个边界内绘制,我的环应该用这个表达式绘制:
xRing = (int)((frameWidth -ringSize)* (Math.random()));
yRing = (int)((frameHeight-ringSize)*(Math.random()));
setParamsRing(xRing, yRing, ringSize);
有什么问题吗?这是所有代码:
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);
}
}
<小时/>我需要绘制圆环和正方形,但不要彼此重叠。有什么问题吗?我的代码似乎不起作用(尽管它似乎是正确的):
@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();
}
最佳答案
代码中的问题是,您正在绘制的面板的尺寸不是frameWidth
xframeHeight
,而是更小。框架的边界设置为这些值,但您必须考虑标题栏和边框,这使得您的实际面板比这要小一些。
您可以使用面板的宽度和高度:
xRing = (int) ((getWidth() - ringSize) * Math.random());
yRing = (int) ((getHeight() - ringSize) * Math.random());
setParamsRing(xRing, yRing, ringSize);
关于java - 如何在 JPanel 和 JFrame 范围内绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8368481/
我是一名优秀的程序员,十分优秀!