J'essaie de faire un jeu 2d en java swing. Lorsque je redimensionne le JFrame, le bord qui a été redimensionné n'est pas transparent et l'objet 2d ne peut pas être vu.Un objet 2D est bloqué par le bord du JFrame lorsqu'il est redimensionné
JFrame Code de classe:
public class GameFrame
extends JFrame
{
public static JFrame G_FRAME = new JFrame();
private ArrayList<Integer> KL_DOWN;
private static GameDrawPlayer GD_PLAYER = new GameDrawPlayer();
public static int GF_WIDTH = G_FRAME.getWidth();
public static int GF_HEIGHT = G_FRAME.getHeight();
public static int H = 0;
public GameFrame()
{
KL_DOWN = new ArrayList<Integer>();
G_FRAME.setFocusable(true);
G_FRAME.getContentPane().add(GD_PLAYER);
GameKey keyListener = new GameKey(GD_PLAYER);
G_FRAME.addKeyListener(keyListener);
System.out.println(GF_WIDTH);
CR_REPEAT.start();
}
public static void GL_FRAME(String TITLE, int WIDTH, int HEIGHT)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
G_FRAME.setLayout(new FlowLayout());
G_FRAME.setTitle(TITLE);
G_FRAME.setSize(WIDTH, HEIGHT);
//G_FRAME.setResizable(false);
G_FRAME.isResizable();
G_FRAME.setMinimumSize(new Dimension(WIDTH, HEIGHT));
G_FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
G_FRAME.pack();
G_FRAME.setVisible(true);
}
}
);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException
| InstantiationException
| IllegalAccessException
| UnsupportedLookAndFeelException e1)
{
e1.printStackTrace();
}
}
private ActionListener LT_ACTION = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
GF_WIDTH = G_FRAME.getWidth();
GF_HEIGHT = G_FRAME.getHeight();
}
};
private int speed = 1;
private Timer CR_REPEAT = new Timer(speed, LT_ACTION);
}
Graphics2D Code de classe:
public class GameDrawPlayer
extends JComponent
{
private static final int GP_W = 100;
private static final int GP_H = 100;
private static int GP_X = 0;
private static int GP_Y = 0;
private static GameClass GC = new GameClass();
private static int GM_PIXEL = 1;
private static Rectangle rect = new Rectangle(GP_X, GP_Y, GP_W, GP_H);
private static BufferedImage G_BI;
private static int GF_getWidth = GC.G_FRAME.G_FRAME.getWidth();
private static int SD = (int) (GC.G_FRAME.G_FRAME.getWidth());
public GameDrawPlayer()
{
try
{
G_BI = ImageIO.read(getClass().getResourceAsStream("/Images/Redox.png"));
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e);
}
GF_REPEAT.start();
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(GC.PREF_W, GC.PREF_H);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(G_BI, GP_X, GP_Y, GP_W, GP_H, null);
}
public void getImgLocation()
{
System.out.print(" <-> [" + GP_X + ", " + GP_Y + "]");
}
public void refresh(){repaint();};
public void GPK_UP()
{
GP_Y = GP_Y - GM_PIXEL;
repaint();
}
public void GPK_DOWN()
{
GP_Y = GP_Y + GM_PIXEL;
repaint();
}
public void GPK_LEFT()
{
GP_X = GP_X - GM_PIXEL;
repaint();
}
public void GPK_RIGHT()
{
GP_X = GP_X + GM_PIXEL;
repaint();
}
private static int s = 0;
public void GP_COLLISION()
{
if (GP_X + GM_PIXEL > s - GP_W - (GP_W/5) - 1)
GP_X = s - GP_W - (GP_W/5) - 1;
}
private ActionListener LT_ACTION = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s = GC.G_FRAME.GF_WIDTH;
}
};
private int speed = 1;
private Timer GF_REPEAT = new Timer(speed, LT_ACTION);
}