Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7791989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:03:46+00:00 2026-06-01T22:03:46+00:00

I’m planning on making a game and want the window to have 3 different

  • 0

I’m planning on making a game and want the window to have 3 different modes: a small 800 x 500 window, a maximized window, and a fullscreen window. I plan on using active rendering to draw graphics into the jframe and to paint the components in the layeredpane. I am using a bufferstrategy with 2 buffers. In order to deal with the problem of setting an already visible screen to fullscreen, each time the window has it’s dimensions changed (it isnt resizeable by the window itself, only by buttons within the program) a new jframe is created, given the proper size, and a jpanel containing the components to paint is added to the frame. for some reason, every time I paint something with the graphics from the bufferstrategy, there is an offset (painting something at 0,0, would show up around -3, -20, and I’m not sure why. This happens when I either just draw with the graphics or call paint components with the graphics (on the current jframe’s layered panel). Any help you can give would be appreciated.enter code here

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

public class ScreenManager {

private GraphicsDevice device;
private JFrame window;
private int sizeState;
private JPanel contentPane;
private String title;
private Rectangle maxBounds;    

public static final int SMALL_WINDOW = 1;
public static final int MAXIMIZED_WINDOW = 2;
public static final int FULLSCREEN_WINDOW = 3;

private static final int SMALL_WIDTH = 800;
private static final int SMALL_HEIGHT = 500;

public ScreenManager(String s){     
    NullRepaintManager.install();
    title = s;      
    GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();        
    maxBounds = environment.getMaximumWindowBounds();
    device = environment.getDefaultScreenDevice();  
    contentPane = new JPanel();             
    resetJFrame();      
    setSmallWindow();       
}

public JPanel getPanel(){
    return contentPane;
}

public Graphics2D getGraphics(){
    while(true){
        if(window != null){     
    try{
    return (Graphics2D) window.getBufferStrategy().getDrawGraphics();
    }catch(Exception e){}
    }       
}
}
public void update(){
    if(window != null && !window.getBufferStrategy().contentsLost()){
        window.getBufferStrategy().show();
    }

    Toolkit.getDefaultToolkit().sync();
}

public int getWidth(){
    if(window != null){
        return contentPane.getWidth();
    }
    return 0;
}

public int getHeight(){
    if(window != null){
        return contentPane.getHeight();
    }
    return 0;
}

public void paintComponents(Graphics2D g){      
        if(window != null){
            contentPane.paintComponents(g);
                }
        }

public void closeWindow(){          
    System.exit(0);
}

public void setSmallWindow(){
    if(window != null && sizeState != ScreenManager.SMALL_WINDOW){
    resetJFrame();      
    window.setSize(ScreenManager.SMALL_WIDTH, ScreenManager.SMALL_HEIGHT);      
    contentPane.setSize(ScreenManager.SMALL_WIDTH, ScreenManager.SMALL_HEIGHT);
    window.setLocationRelativeTo(null);         
    window.setVisible(true);        
    window.createBufferStrategy(2);         
    sizeState = ScreenManager.SMALL_WINDOW;     
}
}

public void setMaximizedWindow(){
    if(window != null && sizeState != ScreenManager.MAXIMIZED_WINDOW){
    resetJFrame();
    window.setSize((int) maxBounds.getWidth(), (int) maxBounds.getHeight());    
    contentPane.setSize(window.getWidth(), window.getHeight());
    window.setLocation(0,0);
    window.setVisible(true);        
    window.createBufferStrategy(2);     
    sizeState = ScreenManager.MAXIMIZED_WINDOW;     
}
}

public void setFullScreenWindow(){
    if(window != null && sizeState != ScreenManager.FULLSCREEN_WINDOW){
        resetJFrame();      
        window.setUndecorated(true);
        device.setFullScreenWindow(window);
        contentPane.setSize(window.getWidth(), window.getHeight());
        window.createBufferStrategy(2);
        sizeState = ScreenManager.FULLSCREEN_WINDOW;            
    }
}

private void resetJFrame(){
    if(sizeState == ScreenManager.FULLSCREEN_WINDOW){
        device.setFullScreenWindow(null);
    }

    if(window != null){
        window.dispose();
        window = null;
    }

    window = new JFrame(title);         
    window.setResizable(false);         
    window.setIgnoreRepaint(true);
    window.addWindowListener(new WindowExitAdapter());
    window.add(contentPane);            
    contentPane.setOpaque(false);       
}

private class WindowExitAdapter extends WindowAdapter{

    public void windowClosing(WindowEvent 
e){                     
        closeWindow();
    }               
}
 }

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

public class ScreenManagerTest implements ActionListener{

private ScreenManager sm;
private JButton fullscreenButton;
private JButton smallScreenButton;
private JButton maxScreenButton;
private JButton quitButton;
private boolean isRunning = true;

private int sizeState = 1;


public static void main(String[] args) {
    new ScreenManagerTest().go();       
}

public void go(){
    sm = new ScreenManager("Nertz! Solitaire");
    sm.getPanel().setLayout(new BorderLayout());
    fullscreenButton = new JButton("Set Fullscreen");       
    sm.getPanel().add(fullscreenButton, BorderLayout.CENTER);
    smallScreenButton = new JButton("Set Small Screen");        
    sm.getPanel().add(smallScreenButton, BorderLayout.WEST);
    maxScreenButton = new JButton("Set Max Screen");        
    sm.getPanel().add(maxScreenButton, BorderLayout.EAST);
    quitButton = new JButton("Exit Program");       
    sm.getPanel().add(quitButton, BorderLayout.SOUTH);
    fullscreenButton.addActionListener(this);
    smallScreenButton.addActionListener(this);
    maxScreenButton.addActionListener(this);
    quitButton.addActionListener(this);

    while(isRunning == true){           
        switch(sizeState){

        case ScreenManager.FULLSCREEN_WINDOW:
            sm.setFullScreenWindow();
            break;
        case ScreenManager.MAXIMIZED_WINDOW:
            sm.setMaximizedWindow();
            break;
        case ScreenManager.SMALL_WINDOW:
            sm.setSmallWindow();
            break;
        }
        draw(sm.getGraphics());         
        try{
            Thread.sleep(20);
        }catch(Exception e){}
    }
    sm.closeWindow();
}

public void draw(Graphics2D g){ 
    sm.paintComponents(g);
    sm.update();

    g.dispose();
}

public void actionPerformed(ActionEvent event){
    if(event.getSource() == fullscreenButton){
        sm.setFullScreenWindow();
        sizeState = ScreenManager.FULLSCREEN_WINDOW;
    }
    if(event.getSource() == smallScreenButton){
        sm.setSmallWindow();
        sizeState = ScreenManager.SMALL_WINDOW;
    }
    if(event.getSource() == maxScreenButton){
        sm.setMaximizedWindow();    
        sizeState = ScreenManager.MAXIMIZED_WINDOW;
    }
    if(event.getSource() == quitButton){
        isRunning = false;  
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:03:49+00:00Added an answer on June 1, 2026 at 10:03 pm

    for some reason, every time I paint something with the graphics from
    the bufferstrategy, there is an offset (painting something at 0,0,
    would show up around -3, -20, and I’m not sure why.

    Just a wild guess… Maybe you are painting not from (0,0) but by frame bounds zero, since (-3, -20) point seems to be window zero coordinate (small border @ left and ~20px window header)?

    In case you are actually drawing from (0,0) but coordinates getting moved to (-3, -20) and this is actually a window border – you can add a small patch @ the start of paint method:

    protected void paintComponent ( Graphics g )
    {
        Point wl = SwingUtilities.getWindowAncestor ( this ).getLocationOnScreen ();
        Point los = this.getLocationOnScreen ();
        Point zero = new Point ( los.x-wl.x, los.y-wl.y );
        g.translate ( zero.x, zero.y );
    
        // ...
    }
    

    But still i can’t explain why that could happen. Maybe you are saving the zero coordinate when switching between window and fullscreen modes and that causes such problem…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I have an array which has BIG numbers and small numbers in it. I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.