0

So I've checked out the other post which didn't help on here, I'm trying to get my frame with it's message to randomly appear on an area on the screen but when I run it, it says x and y cannot be resolved to a variable, here's the code:

    public class MyFrame extends JFrame{
       MyFrame(int width, int height, int x, int y){
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setTitle("R and Ts Main Frame");
       setSize(width, height);

       Random random = new Random();
       x = random.nextInt();
       y = random.nextInt();
       setLocation(x, y);

       JLabel label = new JLabel("Random Message");
       label.setFont(new Font("Impact", Font.BOLD|Font.PLAIN, height/3));
       label.setForeground(Color.BLUE);
       getContentPane().add(label);
}

}

and this is my main:

 public class OurMain {

  public static void main(String[] args) {
    Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
    int w = sSize.width;
    JFrame f = new MyFrame(w/3, 100, x, y); //my errors are underlined here under the x and y
    f.setVisible(true);
}

}

NarinderRSharma
  • 99
  • 1
  • 11
  • You never declare or assign values to `x` and `y` in your main class. – Bethany Louise May 18 '16 at 01:08
  • well in my main class that launches the program 'OurMain' I just put x and y in the MyFrame constructor its in my 'MyFrame' class that I assigned random to them right? so how would I go about doing it? – NarinderRSharma May 18 '16 at 01:11
  • Since you assign random values to them within the constructor, I'd recommend declaring them within the constructor rather than passing them to it. – Bethany Louise May 18 '16 at 01:13
  • see that's what I don't know how to do I tried many things even declaring them in setLocation; Like I mean i'm saying I can't just put 'int x = random.nextInt(),' right? that'll just give me an error – NarinderRSharma May 18 '16 at 01:16
  • You can do that exactly. Just modify the signature of the constructor so `x` and `y` are not passed to it, and you'll be able to declare them within the constructor instead. – Bethany Louise May 18 '16 at 01:18
  • I always learn from my mistakes so if I can see how to declare it by showing me the code then I would know how exactly to go about doing it in the future also I'm using the book by J Liang Java edition 8 – NarinderRSharma May 18 '16 at 01:18
  • `int x` declares the variable `x`. `int x = random.nextInt()` declares and assigns a value to it. – Bethany Louise May 18 '16 at 01:19
  • MyFrame(int width, int height, int x = random.nextInt(), int y = random.nextInt()); I'm getting an error under the equal signs. and then obviously it asks for a ';' which gives me another error – NarinderRSharma May 18 '16 at 01:21
  • No, see, now you're attempting to declare and initialize them within the constructor signature rather than in the body of the constructor. See the answer that UDKOX posted; that's what I was trying to explain to do. – Bethany Louise May 18 '16 at 01:24

2 Answers2

3

This will solve your problem. You did not declare/create x and y before using them, but you don't need them, so just use them locally.

public class OurMain {
    public static void main(String[] args) {
        Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
        int w = sSize.width;

        Random random = new Random();
        int x = random.nextInt(sSize.width);
        int y = random.nextInt(sSize.height);

        JFrame f = new MyFrame(w/3, 100, x, y);
    }
}

public class MyFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    public MyFrame(int width, int height, int x, int y) {
        super();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("R and Ts Main Frame");
        setSize(width, height);

        setLocation(x, y);

        JLabel label = new JLabel("Random Message");
        label.setFont(new Font("Impact", Font.BOLD | Font.PLAIN, height / 3));
        label.setForeground(Color.BLUE);

        getContentPane().add(label);
        setVisible(true);
    }
}

Result (it is loaded in a different location each time):

Screen print

UDKOX
  • 738
  • 3
  • 15
  • It shows no errors, it seems to run, but my message or frame doesn't show up. can someone try running it in their program? I'm using eclipse. maybe it's my font or something to do with my frame. but I swear everything look correct – NarinderRSharma May 18 '16 at 01:40
  • LO, I forgot what you wanted the code to do, I just cleared the errrors, it should work now. – UDKOX May 18 '16 at 01:52
  • how do i contact you on this. I looked at your profile i need some help but there's no message button or anything to send you or reach out to you. – NarinderRSharma May 22 '16 at 22:52
  • @NarinderRSharma Well, you just reached me, what's up? However, private messaging is not supported on Stack Overflow, they want all messages to be on the website, it's better to share information with all the readers/viewers. – UDKOX May 22 '16 at 22:58
  • http://stackoverflow.com/questions/37380389/how-to-make-a-frame-display-50-times-for-1-second-and-disappear-each-time-its-d – NarinderRSharma May 22 '16 at 23:00
  • need help with that it displays all 50 within 3 seconds when it should be displaying and disappearing the frame for 1 second meaning the program should last for 100 seconds which is a minute and 40 seconds – NarinderRSharma May 22 '16 at 23:01
  • just let me know if you can help me out with that question, i'm learning as I go I wish i went to university but I went to college which has less theory so they make it for you to go online and learn yourself mainly. that's why I joined. this is my learning experience. help me if you can on that question: http://stackoverflow.com/questions/37380389/how-to-make-a-frame-display-50-times-for-1-second-and-disappear-each-time-its-d – NarinderRSharma May 22 '16 at 23:07
  • 1
    @NarinderRSharma You have to understand that messaging people to answer your questions is not a good behavior here, we are not your employees. If you keep doing this, you may get banned, be careful. Also, next time, don't spam comments, just edit them if you want to change something. I will check it out, but don't do this again. – UDKOX May 22 '16 at 23:13
  • yeah i'm sorry i'm so new to this thing I do not what's considered right or wrong questions to ask. I'm sorry if I've offended anyone. I'm just trying to learn this stuff and new methods and how to affect the timings of my programs etc.. – NarinderRSharma May 22 '16 at 23:55
  • and for the record I would never think of you as an employee UDKOK I look as you more of a mentor that has a vast knowledge whom I can learn from. Thanks for everything. – NarinderRSharma May 22 '16 at 23:57
  • if anything i'm the new employye who has an apprenticeship under all of you whom have helped me learn more about java. why do you think I reached out to you here.. cause I know you are smart and have the knowledge i can learn from. – NarinderRSharma May 22 '16 at 23:58
  • @NarinderRSharma It's ok, just try not to spam comments and edit them instead. Now, check the answer I wrote and comment there if you have any issue. – UDKOX May 23 '16 at 00:06
-1

You should just add some attributes and replace some things:

MyFrame:

public class MyFrame extends JFrame {

    int x;
    int y;

    MyFrame(int width, int height, int x, int y) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("R and Ts Main Frame");
        setSize(width, height);
        this.x = x;
        this.y = y;
        setLocation(x, y);

        JLabel label = new JLabel("Random Message");
        label.setFont(new Font("Impact", Font.BOLD | Font.PLAIN, height / 3));
        label.setForeground(Color.BLUE);
        getContentPane().add(label);
    }
}

OurMain:

public class OurMain {

    public static void main(String[] args) {
        Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
        int w = sSize.width;
        Random random = new Random();
        int x = random.nextInt();
        int y = random.nextInt();
        JFrame f = new MyFrame(w / 3, 100, x, y); //my errors are underlined here under the x and y
        f.setVisible(true);
    }
}


EDIT:
However, random.nextInt(); is not a good idea, because your screen width&heigth has a little less then 2^32 pixels... so I would limit it to e.g.:

 public static void main(String[] args) {
       Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
       int w = sSize.width;
       int h = sSize.height;
       int x = (int)((Math.random()* w) - w/3);
       int y = (int)((Math.random()* h) - 100);
       System.out.println(x + " " + y);
       JFrame f = new MyFrame(w / 3, 100, x, y); 
       f.setVisible(true);
 }

EDIT of EDIT:

public class OurMain {

    public static void main(String[] args) {
        Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
        int w = sSize.width;
        int h = sSize.height;

        Random rand1 = new Random();

        int randomWidth = rand1.nextInt(w);
        int randomHeight = rand1.nextInt(h);

        Random rand2 = new Random(); // I would still use rand1

        int randomX = rand2.nextInt(w - randomWidth + 1);
        int randomY = rand2.nextInt(h - randomHeight + 1);

        JFrame f = new MyFrame(randomWidth, randomHeight, randomX, randomY);
        f.setVisible(true);
    }
}
Yev
  • 321
  • 3
  • 9
  • Okay so it just happens there's a couple other things I forgot I needed the frame to be random size which I accomplished by adding another rand and placing it: – NarinderRSharma May 18 '16 at 21:06
  • @NarinderRSharma since I know you preffer `Random` class I've edited my answer according to your last question/comment. You should also probably scale your "Random Message" JLabel – Yev May 19 '16 at 00:39
  • no it works i'm trying to figure out how to use pack() in my label to make the text the same size as the frame but i'm having trouble – NarinderRSharma May 19 '16 at 01:29
  • @NarinderRSharma If you want new features in the software, edit your main question, so that people know what the answers are. @Yev If you know the code can be improved by erasing `Random rand2`, do it. Also, what are the differences between the Edits ? You didn't explain nothing. Apart from that, there is no point in keeping the previous code samples if they do not solve the issue. – UDKOX May 19 '16 at 15:29
  • @NarinderRSharma check [this question](http://stackoverflow.com/questions/2715118/how-to-change-the-size-of-the-font-of-a-jlabel-to-take-the-maximum-size) and accepted answer for your last comment. – Yev May 22 '16 at 10:18
  • @UDKOX I've edited my answer according to new questions in comments from the asking user. I've implemented `rand2`, because the user wanted to solve his problem " by adding **another** rand and placing it". And I left a comment, that its not necessary. Your answer has a poor explanation aswell. Downvoting just because my answer was more suitable for the user and he changed his dicision (to unaccept your answer and accept my one) is a bad practice on SO. Shame on you – Yev May 22 '16 at 10:33
  • @Yev **(1)** `by adding another rand and placing it` I think he did not mean another `Random` class, but another `value`. I though you missunderstood. **(2)** My explanation explains why he had a problem, and how I solved it, yours doesn't. Is your description even useful ? `You should just add some attributes and replace some things` What attributes? What things? Why? What's wrong? You just did his homework without teaching him what caused it, and that's not the goal of Stack Overflow **(3)** You still haven't add any explanation **(4)** I did not downvote your answer. – UDKOX May 22 '16 at 16:49