4

I want go full screen and keep everything inside in order.

How should i put the JFrame into full screen AND rescale everything inside: images, generated drawings etc.(sth like zooming it up so the content will fit the screen).

The problem is I am making full screen app, but I don't know on what screen it will be displayed.

This will put the frame into fullscreen, but the content will not be rescaled

   frame.dispose();
   frame.setUndecorated(true);
   frame.setLocation(0, 0);
   frame.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
   frame.setVisible(true);
   frame.repaint();
4

3 に答える 3

5

スケーリングする対象によって異なります。

  • Java2D を使用してグラフィックスを描画する場合は、どれだけ拡大する必要があるかを把握し、Graphics2D.scale() を使用して gfx を適切にスケーリングします。

  • Swing レイアウトを使用する場合は、レイアウト マネージャーを使用して適応レイアウトを作成します。

  • それ以外の場合は、問題について詳しく説明してください

于 2012-08-09T13:14:08.107 に答える
2

これが本当にやりたいことである場合(他の回答からの警告を参照)、それを行うのはそれほど難しくありません(ただし、理解するのに少し時間がかかります)。基本的に、メソッドを拡張JPanelしてから上書きする必要がありますpaint

これが私が思いついたサンプルです:

import java.awt.*;
import java.awt.image.BufferedImage;    
import javax.swing.*;


public class CustomPanel extends JPanel{ 

    Component myComponent;

    public CustomPanel(){
        super();
        setLayout(null);
    }

    /**
     * Only allows one component to be added
     */
    @Override
    public Component add(Component c){
        super.add(c);
        c.setLocation(0, 0);
        c.setSize(c.getPreferredSize());
        myComponent = c;
        return c;
    }

    @Override
    public void paint(final Graphics g){

        Dimension d = this.getSize();               
        Dimension p = myComponent.getPreferredSize();

        // Paints the child component to a image
        BufferedImage newImg = new BufferedImage(p.width, p.height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = newImg.createGraphics();
        super.paint(g2d);

        // Resizes the image if necessary
        Image img;
        if(d.height > p.height && d.width > p.width){
            System.out.println("Scaled");

            float changePercentage = 0;
            if(d.height/p.height > d.width/p.width){
                changePercentage = (float)d.width/(float)p.width;
            } else{
                changePercentage = (float)d.height/(float)p.height;
            }
            System.out.println(changePercentage);

            int newHeight = ((Float)(p.height * changePercentage)).intValue();
            int newWidth = ((Float)(p.width * changePercentage)).intValue();

            img = newImg.getScaledInstance(newWidth, newHeight, 0);             
        } else{
            System.out.println("Not Scaled");
            img = newImg;
        }

        // Paints the image of the child component to the screen.
        g.drawImage(img, 0, 0, null);
    }

    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        SwingUtilities.invokeLater(new Runnable(){public void run(){

            JFrame frame = new JFrame("Zoom Panel"); 
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.setSize(300, 200); 

            CustomPanel buffer = new CustomPanel();
            JPanel content = new JPanel();
            content.add(new JLabel("Bogus"));
            content.setBackground(Color.red);
            buffer.add(content);
            frame.setContentPane(buffer);

            frame.setVisible(true); 
            new CustomPanel();

        }}); 
    } 

} 
于 2012-08-09T20:40:42.550 に答える
1

数日前、私はJavaフルスクリーンアプリで作業しました。以下のリンクをご覧ください。それがあなたの要求であるなら、私はあなたをある程度助けることができます.

https://docs.google.com/open?id=0B9U-BwYu62ZaeDM3SWZhaTdSYzQ

于 2012-08-09T15:50:31.933 に答える