I am currently having a problem with JFrame and Images. This program uses an Applet then adds it to a JFrame in a different class, so it can be ran as either an Applet or an Application. Currently, the frame holds only a handful of images, and no components. I recently attempted to add a JTextField using absolute positioning (LayoutManger is null) and it works fine, except all the images are removed, leaving me with just a JTextField. Why is this happening? How can I fix it? My code is posted below. Thanks!
The Main Class (Creates the Applet and Images):
package net.xenix.src;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.Timer;
public class XenixMain extends JApplet implements ActionListener
{
private static final long serialVersionUID = 1L;
private static int pixelSize = 2;
public static Dimension size = new Dimension(1600, 900);
public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize);
public static String name = "Xenix";
public static final int WIDTH = 1600;
public static final int HEIGHT = 900;
//Finding player Windows name
static String username = System.getProperty("user.name");
public static int secondsCount = 0;
//Shortcut to image directory
public static String imagePath = "C:\\Users\\" + username + "\\Desktop\\Xenix Dev\\Xenix\\resources\\graphics\\";
private static PaintSurface canvas;
public static ImageIcon XenixBackgroundIcon = new ImageIcon(imagePath + "XenixBackground.png");
public static Image XenixBackground = XenixBackgroundIcon.getImage();
public static ImageIcon XenixLogoIcon = new ImageIcon(imagePath + "XenixLogo.png");
public static Image XenixLogo = XenixLogoIcon.getImage();
public static ImageIcon HeartContainerFullIcon = new ImageIcon(imagePath + "HeartContainerFull.png");
public static Image HeartContainerFull = HeartContainerFullIcon.getImage();
public static ImageIcon HeartContainer9Icon = new ImageIcon(imagePath + "HeartContainer9.png");
public static Image HeartContainer9 = HeartContainer9Icon.getImage();
public static ImageIcon HeartContainer8Icon = new ImageIcon(imagePath + "HeartContainer8.png");
public static Image HeartContainer8 = HeartContainer8Icon.getImage();
public static ImageIcon HeartContainer7Icon = new ImageIcon(imagePath + "HeartContainer7.png");
public static Image HeartContainer7 = HeartContainer7Icon.getImage();
public static ImageIcon HeartContainer6Icon = new ImageIcon(imagePath + "HeartContainer6.png");
public static Image HeartContainer6 = HeartContainer6Icon.getImage();
public static ImageIcon HeartContainer5Icon = new ImageIcon(imagePath + "HeartContainer5.png");
public static Image HeartContainer5 = HeartContainer5Icon.getImage();
public static ImageIcon HeartContainer4Icon = new ImageIcon(imagePath + "HeartContainer4.png");
public static Image HeartContainer4 = HeartContainer4Icon.getImage();
public static ImageIcon HeartContainer3Icon = new ImageIcon(imagePath + "HeartContainer3.png");
public static Image HeartContainer3 = HeartContainer3Icon.getImage();
public static ImageIcon HeartContainer2Icon = new ImageIcon(imagePath + "HeartContainer2.png");
public static Image HeartContainer2 = HeartContainer2Icon.getImage();
public static ImageIcon HeartContainer1Icon = new ImageIcon(imagePath + "HeartContainer1.png");
public static Image HeartContainer1 = HeartContainer1Icon.getImage();
public static ImageIcon HeartContainerDepletedIcon = new ImageIcon(imagePath + "HeartContainerDepleted.png");
public static Image HeartContainerDepleted = HeartContainerDepletedIcon.getImage();
public static ImageIcon HealthTextIcon = new ImageIcon(imagePath + "HealthText.png");
public static Image HealthText = HealthTextIcon.getImage();
public static ImageIcon ForwardSlashIcon = new ImageIcon(imagePath + "ForwardSlash.png");
public static Image ForwardSlash = ForwardSlashIcon.getImage();
public Timer timer = new Timer(1000, this);
public void start()
{
timer.setInitialDelay(0);
timer.start();
}
public void stop()
{
timer.stop();
}
public void actionPerformed(ActionEvent e)
{
secondsCount++;
System.out.println(secondsCount);
}
public void init()
{
this.setSize(WIDTH, HEIGHT);
canvas = new PaintSurface();
this.add(canvas, BorderLayout.CENTER);
ScheduledThreadPoolExecutor executor =
new ScheduledThreadPoolExecutor(3);
executor.scheduleAtFixedRate(
new AnimationThread(this),
0L, 20L, TimeUnit.MILLISECONDS);
}
}
class AnimationThread implements Runnable
{
JApplet c;
public AnimationThread(JApplet c)
{
this.c = c;
}
public void run()
{
c.repaint();
}
}
class PaintSurface extends JComponent
{
private static final long serialVersionUID = 1L;
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Shape screenDisplay = new Rectangle2D.Float(
450, 175, 700, 500);
g2.setColor(Color.DARK_GRAY);
g2.fill(screenDisplay);
/*
* START TITLE SCREEN CREATION
*/
if(XenixMain.secondsCount > 0)
{
g.drawImage(XenixMain.XenixLogo, 500, 500, this);
}
g.drawImage(XenixMain.XenixBackground, 0, 0, this);
/*
* END TITLE SCREEN CREATION
*/
}
}
JFrame Class (Creates JFrame and adds the Applet):
package net.xenix.src ;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class XenixApplicationWindow extends JFrame
{
private static final long serialVersionUID = 1L;
static XenixMain xenix = new XenixMain();
static ImageIcon xenixIcon = new ImageIcon(XenixMain.imagePath + "XenixIcon.png");
public static void main(String[] args)
{
new XenixApplicationWindow();
}
public XenixApplicationWindow()
{
JFrame frame = new JFrame();
JTextField userInput = new JTextField(15);
JPanel panel1 = new JPanel(null);
frame.add(xenix);
frame.setSize(1600, 900);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setResizable(true);
frame.setIconImage(xenixIcon.getImage());
userInput.setBounds(0, 0, 120, 10);
panel1.add(userInput);
frame.add(panel1);
frame.setContentPane(panel1);
frame.setLocationRelativeTo(null);
frame.setTitle("Xenix");
xenix.init();
xenix.start();
frame.setVisible(true);
}
}
Absolute Positioning
, if you really wanted to move inside the world ofSwing
. If you still so eager to put components at given location have a look at GroupLayout. Moreover, do not overridepaint
instead override paintComponent() Solution to the first problem answers this part :-) – Iatrogenicstatic
thingy so randomly in yourView
code. – Iatrogenic