有办法让Java图形形状彼此交互吗?

huangapple 未分类评论50阅读模式
英文:

Is there a way to make Java graphic shapes interact with each other?

问题

private void moveBall() {

    if (balldown == true) {
        y++;
    }

    if (balldown == false) {
        y--;
    }

    if (y == getHeight() - border) {
        balldown = false;
        bounce++;
    }
    if (y == 0) {
        balldown = true;
        bounce++;
    }

    if (ballright == true) {
        x++;
    }

    if (ballright == false) {
        x--;
    }

    if (x == getWidth()) {
        ballright = false;
        bounce++;
    }
    if (x == 0) {
        ballright = true;
        bounce++;
    }
}

public void paint(Graphics g) {

    super.paint(g);
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, 1080, 760);

    g.setColor(Color.WHITE);
    g.fillOval(x, y, 30, 30);

    g.setColor(Color.WHITE);
    g.fillRect(0, a, 30, 200);

    g.setColor(Color.WHITE);
    g.fillRect(980, b, 30, 200);

    g.fillRect(520, 0, 10, 760);
}

Note: The provided code appears to be related to a Java program that involves moving a ball within a graphical context and bouncing it off the window edges and rectangles. If you have any questions or need further assistance with the code, feel free to ask.

英文:
private void moveBall(){
    
    
    
    if (balldown==true){
        y++; 
        
    }
    
    
    if (balldown==false){
        y--;
        
    }
    
    if(y==getHeight()-border){
        
        
        balldown=false;
        bounce++;
    }
    if(y==0){
        
        
        balldown=true;
        bounce++;
    }
    
    if (ballright==true){
        x++;          
    }
    
    if (ballright==false){
        x--;
        
    }
    
    if(x==getWidth()){
        
        
        ballright=false;
        bounce++;
    }
    if(x==0){
        
        
        ballright=true;
        bounce++;
        
    }
    
    
    
}

public void paint(Graphics g){
    
    
    super.paint(g);
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, 1080, 760);
    
    
    g.setColor(Color.WHITE);
    g.fillOval(x, y, 30, 30);
                
    g.setColor(Color.WHITE);
    g.fillRect(0, a, 30, 200);
    
    g.setColor(Color.WHITE);
    g.fillRect(980, b, 30, 200);
    
    g.fillRect(520, 0, 10, 760);

Im a java beginner
Is there a way to make my ball bounce off the graphic rectangles the same way it bounces off the window edges?If I add the width or height measures to an if statements it just treats it as the whole screens so I am at a loss on how to solve this issue.Is there some sort of way to tread only the rectangle part as a screen edge instead of the whole side?

huangapple
  • 本文由 发表于 2020年5月4日 21:57:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/61593992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定