-2

I'm quite new to this. When I manually change the Jframe border size, it still updates at "19201080" , I'm trying to get the actual Jframe width and height so I'm able to have my Circle component change size based on jframe screen size,

CircleComponent class

    package advancedjava;
import java.awt.*;
import javax.swing.JComponent;
import java.awt.Toolkit;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
/**
 *
 * @author eeu8c6
 */
public class CircleComponent extends JComponent{

    @Override
    public void paintComponent(Graphics g){
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();

        int width = screenSize.width;
        int height = screenSize.height;


        System.out.println("test: " + width + height);
        double swidth = width * 0.8;
        double sheight = height * 0.8;
        Graphics2D g2 = (Graphics2D) g;



        Circle c1 = new Circle(50, 50);
        g2.setPaint(Color.RED);
        c1.draw(g2);
        Ellipse2D ellipse = new Ellipse2D.Double();
        g2.fill(ellipse);



    }

}

Circle Class:

package advancedjava;
import java.awt.*;
import java.awt.geom.*;

/**
 *
 * @author eeu8c6
 */
public class Circle {
    private double xLeft;
    private double xRight;

    public Circle(double x, double y){
        xLeft = x;
        xRight = y;
    }

    public void draw(Graphics2D g2){
        Ellipse2D.Double circle = new Ellipse2D.Double(xLeft, xRight, 400, 400);
        g2.draw(circle);
    }
Alex
  • 17
  • 4
  • That's the size of the screen -- your computer monitor in pixels. More importantly, you should look these sort of questions up in the API before asking as it's all answered there. – Hovercraft Full Of Eels Oct 05 '17 at 14:43

2 Answers2

3
Toolkit kit = Toolkit.getDefaultToolkit(); 
Dimension screenSize = kit.getScreenSize(); 

Is a bad approach, it does not take into account other screen elements, like tasks bars and docks, which could then be presented over the top of the app

I'm trying to get the actual Jframe width and height so I'm able to have my Circle component change size based on jframe screen size

Then call getWidth and getHeight on the component. The component represents the visible painting range you have available, anything larger won't be displayed (will be clipped)

public class CircleComponent extends JComponent{

    @Override
    public void paintComponent(Graphics g){
        int width = getWidth().width;
        int height = getHeight().height;
        //....

Remember, a component is "contained" inside another container, which has it's own size and the component's size may also be effected by layout constraints.

A frame is also made up of other elements (the frame border and title bar) which detract from the viewable space available to your app

If you want to size your window the maximum usable size of the monitor, you can simple use Frame#setExtendedState and pass it JFrame#MAXIMIZED_BOTH, this will simply maximise the window based on the requirements of the OS

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2
Toolkit kit = Toolkit.getDefaultToolkit(); 
Dimension screenSize = kit.getScreenSize(); 

is described with Gets the size of the screen. On systems with multiple displays, the primary display is used. Multi-screen aware display dimensions are available from GraphicsConfiguration and GraphicsDevice.

toolkit delivers you with your physical screen size. to get the size of the JFrame call frame.getSize();

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • I've instantiated the frame in my viewer class, but it won't let me use "frame.getScreenSize();" in my CircleComponent class which is where I need it to change the size of my circle too. I've updated showing the viewer class – Alex Oct 05 '17 at 14:53
  • 1
    @Alex: again, please check the API. JFrame doesn't have that method -- look up the appropriate method to use (hint: search the JFrame API for methods that have the word "size" in them). This site should not be used in place of you're doing a little research, and the API has it all. – Hovercraft Full Of Eels Oct 05 '17 at 14:56