简体   繁体   中英

How do I make my positive integer become negative and stay negative until the end of my action?

I'm trying to test out removing an image from the screen by converting its x and y values into negative values (my screen is a 500 by 500 and cannot be resized) and then returning it to the screen in the next slide by converting the x and y values back into positives so I can then convert them back to negative to remove them again.

In one class (which shows a red pointer on the screen that I can move to the boundaries of other items) I have this:

import java.awt.Rectangle;
public void checkBoundariesTouching() {
    NextSlide next = MainFrame.getSlide();

    for (int i = 0; i < 1; i++) {
        if (getBounds().intersects(next.getBounds())) {
            MainFrame.slide += 1;

In my MainFrame, this slide increase changes the background to make my slides interesting and changes the writing shown.

I have another class that produces black rectangles that I remove one by one to uncover a bullet point of writing that I then explain. I remove the rectangles like this:

import java.awt.Rectangle;
public class BlackRectangle extends Entity {

public BlackRectangle(int x, int y) {
    super(x, y);

}    
public void update() {
    int slide = MainFrame.getSlide();
    checkBoundariesTouching();
    if (slide >= 2) {
        if (x < 0) {
            x = -x;
        }
        if (y < 0) {
            y = -y;
        }
        checkBoundariesTouching();
    }
}

public void checkBoundariesTouching() {

    Pointer pointer = MainFrame.getPointer();
    NextSlide next = MainFrame.getSlide();
    if (getBounds().intersects(pointer.getBounds())) {
        x = -x;
        y = -y;


    }

When the slide is equal to 1, I can remove the black squares without problem. However, when I have increased to the next slide (from slide 2 onwards) when the pointer is touching a black rectangle, it is moved off screeen but once I move the pointer away from it, the black rectangle returns and covers the information. Is there a way I can fix this code as it stands only changing the update method?

I admit I do not know very much Java at all and started two days ago but I am trying to use Java code for my presentation to prove that I can grasp basic concepts of a program in a short space of time and build on it as I go along (I need to prove this to get into my course at uni that has nothing to do with Computer Science.)

I would seriously counsel you NOT to do this.

If the prerequisites for the course are that you have a basic grasp of programming ... and you don't have that basic grasp ... then you are going to get into real problems down the track. Especially, since if this is not a CS course, they are UNLIKELY to help you out if you run into problems due to poor programming skills.

OTOH, if the problem is that you are competent at (say) C# and not at Java, then the right solution is to fork out the money for a developer license. 'Cos you are going to need it in order to do the development work as part of your course ... or in the future.

(But surely you can find a cheaper way of getting a Visual Studio license. I can get "Visual Studio Professional 2012 with MSDN" from the MS Store for less than $1.2k AUD ... with no student discount.)


I acknowledge your point that they are not specifically testing your Java competence. They are, however testing, you on your general competence as a programmer. And surely that includes your ability to choose the right tool for the job ... which includes not trying to do some programming to a tight deadline in a programming language that you are not familiar with!!

If the code works fine when slide = 1, I would suggest removing this:

if (slide >= 2) {
    if (x < 0) {
        x = -x;
    }
    if (y < 0) {
        y = -y;
    }
    checkBoundariesTouching();
}

If you need that code to move the boxes back on the screen, you can put the if statements inside a separate method and call that method when you change slides.

Check for an additional condition within the update method. The method must look like this one below:-

        public void update() {
            int slide = MainFrame.getSlide();
            checkBoundariesTouching();
            if (slide >= 2) {
                if (x < 0) {
                    x = x*-1;
                }
                if (y < 0) {
                    y = y*-1;
                }


            }
        }


public void checkBoundariesTouching() {

    Pointer pointer = MainFrame.getPointer();
    NextSlide next = MainFrame.getSlide();
    if (getBounds().intersects(pointer.getBounds())) {
    if(x>0){
        x = -x;
           }
    if(y>0){
        y = -y;
           }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM