Java e、 getY()返回值高于预期值
当我按下窗口左上角附近的某个位置时,由于某种原因,Java e、 getY()返回值高于预期值,java,panel,mousepress,Java,Panel,Mousepress,当我按下窗口左上角附近的某个位置时,由于某种原因,e.getY()的输出结果比e.getX()高出大约40。我不明白为什么。。。你…吗?关于e.getY(),我有些不清楚 //BBDemo.java-主程序和窗口创建 导入javax.swing.*; 公共类BBDemo扩展了JApplet{//这不是Applet! 公共整数偏移=400; 公共静态void main(字符串[]args){ //TODO自动生成的方法存根 JFrame win=新JFrame(“弹跳球演示”); win.setD
e.getY()
的输出结果比e.getX()
高出大约40。我不明白为什么。。。你…吗?关于e.getY()
,我有些不清楚
//BBDemo.java-主程序和窗口创建
导入javax.swing.*;
公共类BBDemo扩展了JApplet{//这不是Applet!
公共整数偏移=400;
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
JFrame win=新JFrame(“弹跳球演示”);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(新BBPanel());
win.pack();
win.setVisible(true);
}
}//endclass BBDemo
//BBPanel.java—组织GUI的JPanel
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
///////////////////////////////////////////////////////////////////BBPanel
公共类BBPanel扩展了JPanel{
BallInBox m_bb;//弹跳球面板///以便从m_bb获取计时器->设置动画
鲍尔;
//===========================================================================================构造函数
/**创建带有控件和弹跳球显示的面板*/
BBPanel(){
//…创建组件
m_bb=新的BallInBox();
BallN=新球(400400);
JButton startButton=新JButton(“开始”);
JButton stopButton=新JButton(“停止”);
//…添加侦听器
addActionListener(新的StartAction());
addActionListener(新的StopAction());
addMouseListener(新的PressBall());
//…用两个按钮水平布置内部面板///为什么是内部和外部?
JPanel buttonPanel=新的JPanel();
buttonPanel.setLayout(新的FlowLayout());///Flow???
按钮面板添加(开始按钮);
按钮面板。添加(停止按钮);
//…布局外部面板,按钮面板位于弹跳球上方/???
this.setLayout(new BorderLayout());///这个???///板???
this.add(buttonPanel,BorderLayout.NORTH);///this,NORTH???
this.add(m_bb,BorderLayout.CENTER);///this,CENTER???
}//端构造函数
//////////////////////////////////////内部侦听器类StartAction
类StartAction实现ActionListener{
已执行的公共无效操作(操作事件e){
m_bb.setAnimation(真);
}
}
////////////////////////////////////////内部侦听器类停止操作
类StopAction实现ActionListener{
已执行的公共无效操作(操作事件e){
m_bb.setAnimation(假);
}
}
公共类Presspall实现鼠标侦听器{
公共无效鼠标按下(MouseEvent e){
//TODO自动生成的方法存根
//System.out.print(BallN.m_x-BallN.getDiameter()/2+“”);
System.out.print(e.getX()+);
//System.out.print(BallN.m_x+BallN.getDiameter()/2+“”);
//System.out.print(BallN.m_y-BallN.getDiameter()/2+“”);
System.out.print(e.getY()+“”);
//System.out.println(BallN.m_y+BallN.getDiameter()/2+“”);
如果((例如getButton()==1)
&&(e.getX()>=BallN.m_x-BallN.getDiameter()/2和&e.getX()=BallN.m_y-BallN.getDiameter()/2和&e.getY()=400-m_ball.m_x和&e.getX()=
400-m_ball.m_x&e.getY()m_rightbund){//如果在右侧或右侧以外
m_x=m_rightbund;//靠右边缘放置。
m_velocityX=-m_velocityX;//反向。
}
如果(m_y<0){//如果我们在顶部
m_y=0;
m_-velocityY=-m_-velocityY;
}如果(m_y>m_bottomBound){//如果我们在底部
m_y=m_底界;
m_-velocityY=-m_-velocityY;
}*/
}
//====================================================================================================================================抽签
公共空间绘制(图g){
g、 setColor(Color.green);
g、 圆角(m_x,m_y,直径,直径);
}
//========================================================================================设置位置
公共无效设置位置(intx,inty){
m_x=x;
m_y=y;
}
}
如果窗口是装饰的,那么您必须考虑标题栏的高度。这可能是由于大多数GUI程序顶部的蓝色条带(标题栏,不确定这是否是正确的术语)造成的。标题栏被考虑在偏移量中。明白了,按钮是偏移量的根本原因。
//BBDemo.java - The main program and window creation
import javax.swing.*;
public class BBDemo extends JApplet{ //this isn't Applet !
public int offset=400;
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame win = new JFrame("Bouncing Ball Demo");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setContentPane(new BBPanel());
win.pack();
win.setVisible(true);
}
}//endclass BBDemo
//BBPanel.java - The JPanel which organizes the GUI
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/////////////////////////////////////////////////////////////////// BBPanel
public class BBPanel extends JPanel {
BallInBox m_bb; // The bouncing ball panel ///in order to take the Timer->setAnimation from m_bb
Ball BallN;
//========================================================== constructor
/** Creates a panel with the controls and bouncing ball display. */
BBPanel() {
//... Create components
m_bb = new BallInBox();
BallN=new Ball(400,400,400);
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
//... Add Listeners
startButton.addActionListener(new StartAction());
stopButton.addActionListener(new StopAction());
addMouseListener(new PressBall());
//... Layout inner panel with two buttons horizontally ///why inner and outer?
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout()); ///Flow???
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
//... Layout outer panel with button panel above bouncing ball ///???
this.setLayout(new BorderLayout()); ///this ??? ///Board???
this.add(buttonPanel, BorderLayout.NORTH); ///this, NORTH, ???
this.add(m_bb , BorderLayout.CENTER); ///this, CENTER, ???
}//end constructor
////////////////////////////////////// inner listener class StartAction
class StartAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
m_bb.setAnimation(true);
}
}
//////////////////////////////////////// inner listener class StopAction
class StopAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
m_bb.setAnimation(false);
}
}
public class PressBall implements MouseListener {
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.print(BallN.m_x-BallN.getDiameter()/2+" ");
System.out.print(e.getX()+" ");
//System.out.print(BallN.m_x+BallN.getDiameter()/2+" ");
//System.out.print(BallN.m_y-BallN.getDiameter()/2+" ");
System.out.print(e.getY()+" ");
//System.out.println(BallN.m_y+BallN.getDiameter()/2+" ");
if ((e.getButton() == 1)
&& (e.getX() >= BallN.m_x-BallN.getDiameter()/2 && e.getX() <=
BallN.m_x+BallN.getDiameter()/2 && e.getY() >= BallN.m_y-BallN.getDiameter()/2 && e.getY() <=
BallN.m_y+BallN.getDiameter()/2 ))
{ m_bb.setAnimation(false);
}else{}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
}//endclass BBPanel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/////////////////////////////////////////////////////////////// BouncingBall
public class BallInBox extends JPanel {
//============================================== fields
public Ball m_ball = new Ball(400,400,400);
private int m_interval = 40; // Milliseconds between updates.
private Timer m_timer; // Timer fires to animate one step.
//=================================================== constructor
public BallInBox() {
setPreferredSize(new Dimension(800, 800));
setBorder(BorderFactory.createLineBorder(Color.BLACK));
m_timer = new Timer(m_interval, new TimerAction());
}
//========================================================= setAnimation
public void setAnimation(boolean turnOnOff) {
if (turnOnOff) {
m_timer.start(); // start animation by starting the timer.
} else {
m_timer.stop(); // stop timer
}
}
//======================================================= paintComponent
public void paintComponent(Graphics g) {
super.paintComponent(g); // Paint background, border
m_ball.draw(g); // Draw the ball.
// m_ball.changeColor(g);
}
class TimerAction implements ActionListener {
//================================================== actionPerformed
public void actionPerformed(ActionEvent e) {
m_ball.setBounds(getWidth(), getHeight()); ///???
m_ball.move(); // Move the ball.
repaint(); // Repaint indirectly calls paintComponent. ///why "indirectly"?
}
/* public void mousePressed(MouseEvent e) {
System.out.println("dfvsd");
if ((e.getButton() == 1)
&& (e.getX() >= 400- m_ball.m_x && e.getX() <= 400+ m_ball.m_x && e.getY() >=
400- m_ball.m_x && e.getY() <= 400+ m_ball.m_x)) {m_timer.stop();
// System.out.println("dfvsd");
}else{}
}*/
}
//////////////////////////////////////// inner listener class StopAction
}//endclass
import java.awt.*;
///////////////////////////////////////////////////////////////// BallModel
public class Ball {
//... Constants
final static int DIAMETER = 50;
//... Instance variables
static int m_x; // x and y coordinates upper left
static int m_y;
/*
private int m_velocityX; // Pixels to move each time move() is called.
private int m_velocityY;*/
private int m_rightBound; // Maximum permissible x, y values.
private int m_bottomBound;
public int i=0;
public int offset=400;
//======================================================== constructor
public Ball(int x, int y, int offset) {
m_x = x;
m_y = y;
//offset=400;
}
//======================================================== setBounds
public void setBounds(int width, int height) {
m_rightBound = width - DIAMETER;
m_bottomBound = height - DIAMETER;
}
//============================================================== move
public void move() {
double degrees=(double) i;
double radians=Math.toRadians(degrees);
double Sinu=Math.sin(radians);
double Sinu200=Math.sin(radians)*300;
int SinuInt=(int) Sinu200;
m_y=offset+SinuInt;
//z=offset-SinuInt;
double Cos=Math.cos(radians);
double Cos200=Math.cos(radians)*300;
int CosInt=(int) Cos200;
m_x=offset+CosInt;
i++; // j--;
if (i==360) i=0;
//w=offset-CosInt;
/*
//... Bounce the ball off the walls if necessary.
if (m_x < 0) { // If at or beyond left side
m_x = 0; // Place against edge and
m_velocityX = -m_velocityX; // reverse direction.
} else if (m_x > m_rightBound) { // If at or beyond right side
m_x = m_rightBound; // Place against right edge.
m_velocityX = -m_velocityX; // Reverse direction.
}
if (m_y < 0) { // if we're at top
m_y = 0;
m_velocityY = -m_velocityY;
} else if (m_y > m_bottomBound) { // if we're at bottom
m_y = m_bottomBound;
m_velocityY = -m_velocityY;
}*/
}
//============================================================== draw
public void draw(Graphics g) {
g.setColor(Color.green);
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
//======================================================== setPosition
public void setPosition(int x, int y) {
m_x = x;
m_y = y;
}
}