0

I need my JFrame to display itself within the dimensions of my screen and it should display the frame with the message then disappear for 1 second then reappear 50 times here's my code so far:

 import javax.swing.*;
 import java.awt.*;
 import java.util.*;


public class OurMain {

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

    Random rand = new Random();

    int z = rand.nextInt(sSize.width);
    int c = rand.nextInt(sSize.height);

    int x = (int)((Math.random()* w) - z);
    int y = (int)((Math.random()* h) - 100);
    for(int g = 0; g < 51; g++){
    JFrame f = new MyFrame(z, 100, x, y);
    f.pack();
    f.setVisible(true);
    f.setVisible(false);
    }
}

}

and here's my JFrame:

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class MyFrame extends JFrame{

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

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("R and T's Main Frame");
    setSize(width, height);
    setLocation(x, y);


    int v;
    v = 5 + (int)(Math.random() * (1 - 5) + 1);

    JLabel label = new JLabel("Software Engineering");
    label.setFont(new Font("Impact", Font.BOLD, height/v));
    label.setForeground(Color.BLUE);
    getContentPane().add(label);
    setVisible(true);
}

}

So yeah I have two problems 1. the frame seems to go outside of the screen sometimes when it should be displayed within the dimensions of my screen resolution and 2. The second is the iteration of the loop 50 times and for 1 second it should be displayed and then disappear for 1 second and continue on like that for 50 frames appearing.

NarinderRSharma
  • 99
  • 1
  • 11
  • when i run it it seems to skip through the framse so fast it doesn't even feel like the frame was displayed 50 times cause i don't know how to make the timing for 1 second in my loop and disappear for 1 second in my loop – NarinderRSharma May 22 '16 at 22:41
  • I'm also concerned of the size of my frame it seems to be too small sometimes so the msg can't even show I'd also need to know how to readjust the frame so it's not as small which is obviously in my JFrame MyFrame where my Math.random() has to be adjusted – NarinderRSharma May 22 '16 at 22:42
  • I've also realized since I've added the for loop the frame repeats in the same spot when it should be showing randomly in different areas of the screen, help is definately needed – NarinderRSharma May 22 '16 at 22:44
  • 1
    You should put all this info in the main post, well written. Try to be organized, go through the problems 1 by 1, list them. Try to write less and explain more. If you are a native English speaker, this is not asking too much, is it ? Make a list of the problems because they are a lot, e.g.: *(Issue 1)* `it doesn't even feel like the frame was displayed 50 times` *(Issue 2)* `i don't know how to make the timing for 1 second` *(Issue 3)* ` the frame seems to go outside of the screen sometimes`etc. Comments are not to add information to the post, if you want to add something, edit it. – UDKOX May 22 '16 at 23:23
  • alright thanx i'm new to the site and I have adhd so I feel sometimes i have to add something I missed, but yeah do you havea solution bro? – NarinderRSharma May 22 '16 at 23:36
  • so it flips through it 50 times but really fast. and doesn't random it picks a spot and makes it large small but in the same spot. then ofcourse the timing 1 second to appear another to disappear (ofcourse in different locations) – NarinderRSharma May 22 '16 at 23:37
  • See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson May 23 '16 at 02:26

1 Answers1

3

First, I feel I have to tell you:

You just wrote 5 lines since your previous post.

Here we expect people to put more effort, try things and then ask, explining where they are stuck. We are not here to do your homework, we are here to help, so first, try it out by yourself. It doesn't look like you did any kind of research to me. If you want to get help, this must change because people notice that and don't like it.

Even if you did not get results, post what you tried, what were the results, what you expected them to be etc.

Code

This could be done in several ways, since you did not seem to care about it, neither added any preffered way to do it, I did it on my own.

Some possibilities are:

  • Create one JFrame and move it's location whenever you want to set it visible

  • Create a JFrame on every iteration of the loop (likely to be slower than the previous one?)

  • Many more...

I liked the first approach more, so I did that one. This is what it does:

  • Appear and dissappear 50 times

  • When it goes visible and invisible waits 1 second

  • Everytime it's set as visible the location and size of the frame changes

  • It will always be inside the screen

OurMain.java

public class OurMain {

    public static void main(String[] args) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;

        Random rand = new Random();

        MyFrame frame = new MyFrame();

        for (int g = 0; g < 50; g++) {
            int frameWidth = rand.nextInt(screenSize.width);
            int frameHeight = rand.nextInt(screenSize.height);

            int frameLocationX = rand.nextInt(screenWidth - frameWidth);
            int frameLocationY = rand.nextInt(screenHeight - frameHeight);

            frame.setSize(frameWidth, frameHeight);
            frame.setLocation(frameLocationX, frameLocationY);
            frame.setVisible(true);

            try {
                Thread.sleep(1000); // Waits 1000 miliseconds = 1 second
            } catch (InterruptedException e) { }

            frame.setVisible(false);

            try {
                Thread.sleep(1000); // Waits 1000 miliseconds = 1 second
            } catch (InterruptedException e) { }
        }
    }
}

MyFrame.java

public class MyFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    private JLabel label;

    public MyFrame() {
        super();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("R and T's Main Frame");

        label = new JLabel("Software Engineering");
        label.setForeground(Color.BLUE);
        getContentPane().add(label);
    }
}

I did not comment the code, it's pretty simple. Anyway, if you need some explanation feel free to ask in the comments.

EDIT: Answers to questions on comments

  • If I didn't want to change the height of it and keep it 100, frameLocationY = 100 ?

No, that would change its location, you need to change frameHeight to 100

  • interruptedException is empty cause nothing would go wrong

In this case, that's true, however, a Thread can be stopped (see interrupt()) and therefore it would execute what it's inside the catch brakets

  • an object called thread.sleep

public static void Thread.sleep(long milis) throws InterruptedException is a function inside the Thread class. Since it's static, it doesn't need an instance of Thread to be created

  • the f.pack(); so that within the frame the text is proportionate to the random frame size

If I am not mistaken, what pack() does is give all the space they need to each component of the frame, however, since the text is so little and the frame so big, giving the label it's preffered size would eventually wrap it, resizing the size we set before

  • Everytime it's set as visible the location and size of the frame changes'' as does the content in it is proportionate to its size

That's more tricky, nevertheless, since you only have a JLabel, we can calculate the value of the font it needs to take all the space of the frame. Look this link and try to add that to the code, tell us if you can't

Community
  • 1
  • 1
UDKOX
  • 738
  • 3
  • 15
  • so in a I/O view of it the try and catch has an object called thread.sleep what does thread pertain too? and the catch you used interruptedException e which obviously is empty cause nothing would go wrong if I'm right. there's nothing to really catch I guess. Now if I didn't want to change the height of it and keep it 100 I'm thinking I'd just instead of using a random variable use int frameLocationY = 100; – NarinderRSharma May 23 '16 at 00:11
  • damn the only thing is it works properly except for one thing you took out that i was trying to make possible fully. the f.pack(); so that within the frame the text is proportionate to the random frame size. now I tried putting it back in above frame.setVisble(true); with frame.pack(); but it becomes proportionate to the size of the frame but when the frame is randomly shown it is the same size each time... ugh i tried even putting it outside the for loop but no change. – NarinderRSharma May 23 '16 at 00:28
  • @NarinderRSharma I think you lack some basics of programming. I know it's hard to follow some tutorials regularly, but I think you should give it a try. This is the most basic staff you will read out there. Did you learn any other programming language before ? (I want to know this before answering your questions) – UDKOX May 23 '16 at 01:00
  • @NarinderRSharma I am sorry about the lectures, however, this is learnt by programming, lectures help, but if you are really willing to learn, you will for sure. Also, I must repeat, don't spam comments! This is not the right place to talk about your life. I must ask you to stay on topic, don't forget, this is internet, not a private diary! I suggest you erase those, as they are off-topic comments. You are doing the right thing, looks like you want to learn, sure you eventually will, so post the questions you have! – UDKOX May 23 '16 at 02:01
  • okay i have deleted the comments now i just want to know why my frame.path() won't work when it did before but with your code it will not but without the code i've understood i will not have iteration 50 times. – NarinderRSharma May 23 '16 at 06:38
  • all i wnat to know is this i've re-wrote your solution for what i want: -Everytime it's set as visible the location and size of the frame changes'' as does the content in it is proportionate to its size – NarinderRSharma May 23 '16 at 06:41
  • @NarinderRSharma I edited the post with the questions you had, let me know if I missed any. – UDKOX May 23 '16 at 13:18
  • Yeah someone was telling me to check out fontMetrics but since we didn't even touch that we just did one class last semester with ten minutes on as I said before copy and paste tutorial of Pack() saying that it fills the frame with whichever components I will try to add fontMetrics code it seems easy enough to understand and straightforward. oh and again thank you I'll let you know how it goes. – NarinderRSharma May 24 '16 at 17:09