Reputation: 11
I am trying to make a 2d game in java swing. When I resize the JFrame, the edge which has been resized is not transparent and the 2d object cannot be seen.
JFrame Class Code :
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 Class Code:
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);
}
Upvotes: 0
Views: 43
Reputation: 285415
You're overriding getPreferredSize
on the drawing JPanel, effectively setting its size to hard numbers, and then adding it into a container that uses **FlowLayout. So the JPanel will not increase in size if its container, the JFrame's contentPane increases, and the drawings remain clipped. Solution: avoid overly simplistic layouts like FlowLayout for purposes such as these. Why not use BorderLayout instead?
As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.
Aside number 2: you're grossly and inappropriately using the static modifier. I would avoid using it unless you're dealing with a constant, or you have a definite explicit need for it, and note that "the compiler is telling me to use it" doesn't count, since that just means that the code needs to be fixed so that the compiler will allow the variable to remain an instance variable.
Aside number 3: Whatever you think this: G_FRAME.isResizable();
is doing, understand that it's not doing it. This call does nothing at all other than to return a boolean value -- a boolean that you are discarding.
Upvotes: 1