So, I've been working to redo my code so that the painting is all done in a JPanel instead of a JFrame so I can do some very much needed image buffering.
I've scoured StackOverflow and I've googled my fingers raw and I thought I had it set up right, but it's not working. I just get a blank white screen and some error script in the terminal. Any help is appreciated. Here is the code
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
public class RacerDoom extends JFrame {
private JPanel panel;
final int WIDTH = 900, HEIGHT = 640;
int counter = 0;
Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
Rectangle right = new Rectangle((WIDTH/9)*8,0,WIDTH/9,HEIGHT);
Rectangle top = new Rectangle(0,0,WIDTH,HEIGHT/9);
Rectangle bottom = new Rectangle(0,(HEIGHT/9)*8,WIDTH,HEIGHT);
Rectangle center = new Rectangle((int)((WIDTH/9)*2.5),(int)((HEIGHT/9)*2.5),(int)((WIDTH/9)*4),(HEIGHT/9)*4);
Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2,WIDTH/30,WIDTH/30);
Rectangle finishtop = new Rectangle(WIDTH/9,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5),HEIGHT/70);
//Starting lines
Rectangle startO = new Rectangle(WIDTH/9,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
public RacerDoom() {
//create JFrame
super("Racer Doom Squared");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
getContentPane().add(panel);
panel = new MainPanel();
panel.setBounds(0,0,WIDTH,HEIGHT);
this.getContentPane().add(panel);
//set up Game countdown timer
final Timer timer=new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(counter>=10) {
((Timer)e.getSource()).stop();
}
else{
counter++;
}
System.out.println(counter);
}
});
//start timer
timer.start();
}
private class MainPanel extends JPanel {
public MainPanel() {
super();
}
//draw graphics
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0,0,WIDTH,HEIGHT);
//boundaries
g.setColor(Color.LIGHT_GRAY);
g.fillRect(left.x,left.y,left.width,left.height);
g.fillRect(right.x,right.y,right.width,right.height);
g.fillRect(top.x,top.y,top.width,top.height);
g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
g.fillRect(center.x,center.y,center.width,center.height);
//start line
g.setColor(Color.WHITE);
g.fillRect(startO.x,startO.y,startO.width,startO.height);
//finish line
g.setColor(Color.CYAN);
g.fillRect(finishtop.x,finishtop.y,finishtop.width,finishtop.height);
//p1
g.setColor(Color.BLUE);
g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);
//HUD
g.setColor(Color.WHITE);
Font f = new Font("Monospaced", Font.BOLD, 24);
g.setFont(f);
g.drawString("Boosts: "+p1Boost,(WIDTH-(WIDTH/6)),(HEIGHT-(int)(HEIGHT/1.1)));
g.drawString("Time: "+(10-counter),540,100);
}
}
public static void main (String [] args) {
new RacerDoom();
}
}
The error script:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at RacerDoom.(RacerDoom.java:46)
at RacerDoom.main(RacerDoom.java:232)
I'm quite sure I'm just an idiot and the answer is probably giving me the middle finger in the error message, but it's still Greek to me. But even (especially?) idiots need help.
Your error is pretty simple:
getContentPane().add(panel);
panel = new MainPanel();
panel.setBounds(0,0,WIDTH,HEIGHT);
You're adding the panel to the widow before the panel is actually created. Try rearranging it like this:
panel = new MainPanel();
panel.setBounds(0,0,WIDTH,HEIGHT);
getContentPane().add(panel);
I believe line 46 is:
getContentPane().add(panel);
At that point, you haven't created the panel yet, so it is null. You can't add a null component to a container. Remove that line; your later this.getContentPane().add(panel); is all you need.
The NullPointerException occurs because you try to add not initialized variable panel:
getContentPane().add(panel);
You have to first initialize this variable, and than add it to getContentPane()
Related
The goal here is to use jlabels with an image icon that contains a BufferedImage. Those jlabels can then be easily moved around with the mouse without having to go searching a ton of different BufferedImages on the screen to find out which one is being clicked on.
This is easy to do in a standard JFrame. I've been searching around here for an hour trying to figure out how to implement this in a game loop where a paintComponent is overridden.
import javax.swing.*;
import java.awt.*;
public class Main {
public Main() {
}
public static void main(String[] args) {
JFrame window = new JFrame();
GamePanel gamePanel = new GamePanel();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
window.setLayout(new FlowLayout());
window.setTitle("FX Test");
window.add(gamePanel);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
gamePanel.startGameThread();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
public class GamePanel extends JPanel implements ActionListener {
private Timer gameLoopTimer;
public int screenWidthPixels = 640;
public int screenHeightPixels = 480;
private int counter;
private int x = 1;
private float alpha = 1;
private final int DELAY = 15;
private final int INITIAL_DELAY = 200;
public GamePanel() {
this.setPreferredSize(new Dimension(screenWidthPixels, screenHeightPixels));
this.setBackground(Color.black);
this.setDoubleBuffered(true);
this.setFocusable(true);
this.requestFocus();
counter = 0;
JButton testButton = new JButton("Button Test");
this.add(testButton);
JLabel label = new JLabel(new String("Label test"));
label.setVisible(true); // Doesn't seem to be needed.
this.add(label);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.WHITE);
g2.drawString("Game Panel Testing: " + counter,128,129);
g2.dispose();
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
update();
}
void startGameThread() {
gameLoopTimer = new Timer(DELAY,this);
gameLoopTimer.setInitialDelay(INITIAL_DELAY);
gameLoopTimer.start();
}
}
That code draws "Game Panel Testing: " and the incrementing counter, but no button and no label.
If I comment out the entire paintComponent I'm overriding, the button and label appear as expected.
What I can't wrap my head around is how to get the label and button to appear again once paintComponent is overridden. I thought the super.paintComponent(g) would take care of that automatically, but clearly I'm missing something here. How on earth can I add a bunch of JLabels to this game loop instead of having to manually handle moving of g2 drawn BufferedImages on mouse drag?
The jlabels are not drawn since you have overridden the paintComponent method.
The call to super is on the super class, so you have misunderstood how that call works.
If you put your in a class that inherits from your class with jlabels it will work.
I have a gameloop with a triangle that moves around the screen randomly. I want to add a button that when clicked will add another randomly moving triangle. I'm using event listeners and no matter where I place it I'm still getting an exception
Here's my code
import javax.swing.*;
import drawing.Canvas;
import tools.Utils;
import triangle.DynamicTriangle;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class TriangleProgram {
private JFrame frame;
private Canvas canvas;
private JPanel lowerPanel;
private JButton addTriangleButton;
private ArrayList<DynamicTriangle> triangles;
public void gameLoop() {
int deltaTime = 20;
triangles.add(new RandomTriangleA(canvas, 150, 150));
for (DynamicTriangle dynamicTriangle : triangles) {
dynamicTriangle.drawTriangle();
}
while (true)
{
for (DynamicTriangle dynamicTriangle : triangles) {
dynamicTriangle.undrawTriangle();
}
for (DynamicTriangle dynamicTriangle : triangles) {
dynamicTriangle.update(100);
dynamicTriangle.wrapPosition();
}
for (DynamicTriangle dynamicTriangle : triangles) {
dynamicTriangle.drawTriangle();
}
Utils.pause(deltaTime);
canvas.repaint();
}
}
public TriangleProgram() {
frame = new JFrame();
frame.setTitle("Canvas");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
canvas = new Canvas();
frame.add(canvas, BorderLayout.CENTER);
lowerPanel = new JPanel();
lowerPanel.setLayout(new FlowLayout());
frame.add(lowerPanel, BorderLayout.SOUTH);
triangles = new ArrayList<DynamicTriangle>();
addTriangleButton = new JButton("Add Triangle");
lowerPanel.add(addTriangleButton);
frame.revalidate();
addTriangleButton.addActionListener(new ButtonListener());
gameLoop();
}
public static void main(String[] args) {
System.out.println("Running TriangleProgram...");
new TriangleProgram();
}
class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println(triangles.size());
triangles.add(new RandomTriangleA(canvas, 150, 150));
}
}
}
I expect every time I press the button that a new triangle is added to the canvas, but instead I get the stack trace:
Running TriangleProgram...
1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out-of-bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.remove(ArrayList.java:517)
at java.base/java.util.Collections$SynchronizedList.remove(Collections.java:2424)
at drawing.Canvas.removeMostRecentLine(Canvas.java:162)
at triangle.Triangle.undrawTriangle(Triangle.java:131)
at TriangleProgram.gameLoop(TriangleProgram.java:29)
at TriangleProgram.<init>(TriangleProgram.java:69)
at TriangleProgram.main(TriangleProgram.java:75)
triangle.add(new Triangle(canvas, 150, 150));
Well here you never even declare triangle so not sure how this code even runs to give you an exception. I'm assuming you meant triangles
Also you can probably(?) condense your loops.
Edit: Also when you declare the list triangles, why is it a list of DynamicTriangle but when you initialize and add to list it's Triangle.
Not sure if DynamicTriangle is subclass of Triangle or just typo but if that's the case it shouldn't compile.
If it's a typo then make your ArrayList<DynamicTriangle> triangles use Triangle instead.
If DyanmicTriangle is a subclass of Triangle then you want to declare and initialize triangles as ArrayList<Triangle>() and when you add you do .add(new DynamicTriangle(...
Why doesn't JPanel (panel) get drawn on the green background (the jpanel)? I want to be able to do this without extending j panel to...
Furthermore, for java games should i use keybindings or keylistener in java.
import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class Game {
JFrame window;
JPanel panel;
int charPosX = 0;
int charPosY = 0;
public Boolean createGui() {
window = new JFrame("Game");
window.setSize(1000,500);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
panel = new JPanel();
panel.setVisible(true);
panel.setLayout(null);;
panel.setBackground(new Color(65,130,92));
window.add(panel);
return true; //returns true if ran and will be ran by check status in Main.
}
public void paintComponent(Graphics g) {
panel.paintComponents(g);
g.setColor(Color.RED);
g.drawRect(100,10,30,40);
g.fillRect(10, 10, 20, 10);
}
}
Let's take your code for a second and add #Override to your paintComponent method...
public class Game {
//...
#Override
public void paintComponent(Graphics g) {
panel.paintComponents(g);
g.setColor(Color.RED);
g.drawRect(100, 10, 30, 40);
g.fillRect(10, 10, 20, 10);
}
}
And now we have a compiler error! This is because Game extends Object and does not have a paintComponent method. This means that there is no way that the method could be called by any part of the existing painting system, so, it never gets called.
Components make poor "game" entities, they have a lot of "plumbing" which doesn't make them very efficient for this kind of work, you're generally better off heading down a complete custom painting route
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
public class Game {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Game().createGui();
}
});
}
JFrame window;
GamePanel panel;
int charPosX = 0;
int charPosY = 0;
public Boolean createGui() {
window = new JFrame("Game");
window.setSize(1000, 500);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new GamePanel();
panel.setBackground(new Color(65, 130, 92));
window.add(panel);
window.setVisible(true);
return true; //returns true if ran and will be ran by check status in Main.
}
public class GamePanel extends JPanel {
private Rectangle entity = new Rectangle(100, 10, 30, 40);
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
g2d.draw(entity);
g2d.setColor(Color.BLUE);
g2d.fill(entity);
g2d.dispose();
}
}
}
Also note, I called window.setVisible(true); only after I had added the panel to the window, this is because Swing is lazy when it comes to adding/removing components. If you want to add/remove components after the UI has been realized on the screen, you'll need to call revalidate and repaint on the container to trigger a layout and paint pass
Also, beware, there is a difference between paintComponent and paintComponents ;)
I would highly recommend having a look at Painting in AWT Swing and Performing Custom Painting to gain a better understanding of how painting works in Swing and how you can take advantage of it
Here is my code:
package trialruns;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TransparentFrame extends JFrame
{
JButton b1;
public TransparentFrame()
{
setTitle("Transparent Frame Demo");
setSize(400,400);
setLayout(new GridBagLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setUndecorated(true);
setVisible(true);
setResizable(true);
setOpacity(0.4f);
}
public static void main(String args[])
{
new TransparentFrame();
}
}
The problem is if I setOpacity<1.0 i get an error :
The frame is decorated at java.awt.Frame.setOpacity(Frame.java:960)
And if I make do setUndecorated(true) then I cant resize the Jframe
I need to be able to resize a transparent JFrame
I also need to be able to access the folders under the transparent frame
I mean if the transparent window is sitting on the desktop and I want to open a particular folder placed under the window then I should be able to do so without the jframe getting minimized.
Is there any way to do this??
I searched online but couldn't find a suitable solution.
Resizing of the frame is handled by the frame itself. When you remove the Border decorations you lose the resizing functionality.
So, you need to manage the resizing of the frame yourself. Check out Component Resizer for a class that will allow you to resize any component.
The change to your code would be:
//setResizable(true); // not needed as this is the default anyway
setOpacity(0.4f);
new ComponentResizer( this );
But is it possible to keep the border opaque
Yes, but you will only get the Swing decorated Border, not the platform Border and decorations:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TransparentFrame2 extends JFrame
{
public TransparentFrame2()
{
setTitle("Transparent Frame Demo");
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
setBackground( new Color(0, 0, 0, 0) );
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args[])
{
new TransparentFrame2();
}
}
Also it is still not possible to access the content behind the frame
Yes, but you need full transparency. If you don't use full transparency then the mouse event is passed to the frame, not the component underneath the frame.
If you what semi transparency then theoretically you could add a MouseListener to the frame to intercept the MouseEvent. Then you can make your frame invisible. Then you could use a Robot to generate a new MouseEvent which will now be dispatched to the screen. You would next to use the frames locationOnScreen(...) method to convert the mouse point from the frames coordinates. I have never tried this approach.
try this.. working for me..
import java.awt.*;
import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT;
import javax.swing.*;
class TransparentFrame extends JFrame {
JButton b1;
public TransparentFrame() {
setTitle("Transparent Frame Demo");
setSize(400, 400);
setAlwaysOnTop(true);
setLayout(new GridBagLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setResizable(true);
JPanel panel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
if (g instanceof Graphics2D) {
final int R = 255;
final int G = 255;
final int B = 255;
Paint p =
new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),
0.0f, getHeight(), new Color(R, G, B, 0), true);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
}
};
setContentPane(panel);
JButton button = new JButton("Button");
setBackground(new Color(0,0,0,0));
panel.add(button);
}
public static void main(String args[]) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
//If translucent windows aren't supported, exit.
if (!isPerPixelTranslucencySupported) {
System.err.println("PerPixel Translucency is not supported");
System.exit(0);
}
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TransparentFrame tw = new TransparentFrame();
tw.setVisible(true);
}
});
}
}
referenced from this
I've seen a few examples of this, and I've tried with the following code. I'm trying to change the content pane when portraitB is selected and then run the other class file.
//imported java libraries
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.UIManager;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.Dimension;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class birthdayCardGUI implements ActionListener
{
//Welcome Screen
JPanel welcomeP, welcomeImageP, portraitP, landscapeP, backP;
JLabel welcomeImageL;
JButton portraitB, landscapeB, backB;
//Portrait Screen
JTabbedPane tabbedPane;
JPanel portraitOne;
JLabel test;
public JFrame frame;
//Colours
int colourOne = Integer.parseInt( "c1c7f9", 16);
Color Blue = new Color( colourOne );
public birthdayCardGUI() throws Exception
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
JFrame frame = new JFrame("birthday Card Maker!");
frame.setExtendedState(frame.NORMAL);
frame.getContentPane().add(create_Content_Pane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 700); //Size of main window
frame.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
//sets frame location
int fw = frame.getSize().width;
int fh = frame.getSize().height;
int fx = (dim.width-fw)/2;
int fy = (dim.height-fh)/2;
//moves the frame
frame.setLocation(fx, fy);
}
public JPanel create_Content_Pane() throws Exception
{
JPanel TotalGUI = new JPanel();
//TotalGUI.setBackground(Blue);
TotalGUI.setLayout(null);
//Welcome Panel
welcomeP = new JPanel();
Border etched = BorderFactory.createBevelBorder(10);
Border titled = BorderFactory.createTitledBorder(etched, "Welcome");
welcomeP.setBorder(titled);
welcomeP.setLayout(null);
welcomeP.setLocation(0,0);
welcomeP.setSize(485, 680);
welcomeP.setBackground(Blue);
TotalGUI.add(welcomeP);
welcomeImageP = new JPanel();
welcomeImageP.setLayout(null);
welcomeImageP.setLocation(88,20);
welcomeImageP.setSize(324, 225);
welcomeP.add(welcomeImageP);
String welcomeG = "Welcome Image.png";
ImageIcon WelcomeG = new ImageIcon(welcomeG);
welcomeImageL = new JLabel( WelcomeG, JLabel.CENTER);
welcomeImageL.setSize(324, 225);
welcomeImageL.setLocation(0,0);
welcomeImageP.add(welcomeImageL);
portraitB = new JButton("Portrait");
portraitB.setSize(100, 30);
portraitB.setLocation(200, 295);
portraitB.addActionListener(this);
welcomeP.add(portraitB);
landscapeB = new JButton("Landscape");
landscapeB.setSize(100, 30);
landscapeB.setLocation(200, 335);
landscapeB.addActionListener(this);
welcomeP.add(landscapeB);
TotalGUI.setOpaque(true);
return TotalGUI;
}
public void create_Portrait_Pane()
{
PortraitGUI portrait = new PortraitGUI();
getContentPane().removeAll();
getContentPane().add(portrait.PortraitGUI);
getContentPane().doLayout();
update(getGraphics());
}
#Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == portraitB)
{
create_Portrait_Pane();
}
}
//MAIN METHOD
public static void main(String[] args) throws Exception
{
birthdayCardGUI CGUI = new birthdayCardGUI();
}
}
And this is the PortraitGUI file that creates the new content pane.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class PortraitGUI extends JPanel implements ActionListener
{
JPanel frontPageP;
JLabel frontPageL;
//Color White;
int intValue = Integer.parseInt( "FFFFFF", 16);
Color White = new Color(intValue);
public JPanel PortraitGUI() throws Exception
{
JPanel PortraitGUI = new JPanel();
PortraitGUI.setLayout(null);
frontPageP = new JPanel();
frontPageP.setBackground(White);
frontPageP.setSize(350, 400);
frontPageP.setLocation(20, 70);
PortraitGUI.add(frontPageP);
frontPageL = new JLabel("Front Page");
frontPageL.setLocation(10, 5);
frontPageL.setSize(70, 30);
frontPageL.setHorizontalAlignment(JTextField.CENTER);
PortraitGUI.add(frontPageL);
PortraitGUI.setOpaque(true);
return PortraitGUI;
}
public void actionPerformed(ActionEvent e)
{
}
}
There are several problems in your code, but one of your main problems comes from your shadowing of the JFrame class field in your constructor leaving the class field null and non-usable. To fix this, don't redeclare this variable. Thus change this:
JFrame frame = new JFrame("birthday Card Maker!");
to this:
// this uses the JFrame variable declared in the class.
frame = new JFrame("birthday Card Maker!");
Then you can use this variable later on in a method where you swap contents of the contentPane:
public void create_Portrait_Pane() throws Exception {
PortraitGUI portrait = new PortraitGUI();
frame.getContentPane().removeAll(); // now you can use the frame variable
frame.getContentPane().add(portrait);
//!! getContentPane().doLayout();
//!! update(getGraphics()); // WTF?
((JPanel)frame.getContentPane()).revalidate();
frame.repaint();
}
Having said this, myself, I'd probably use a JPanel that uses CardLayout as my Container for swapping views (other JPanels).
Also, you appear to have a "pseudo" constructor here:
public JPanel PortraitGUI() throws Exception {
Why not just use a real constructor?:
public PortraitGUI() throws Exception {
setLayout(null);
frontPageP = new JPanel();
frontPageP.setBackground(White);
frontPageP.setSize(350, 400);
frontPageP.setLocation(20, 70);
add(frontPageP);
frontPageL = new JLabel("Front Page");
frontPageL.setLocation(10, 5);
frontPageL.setSize(70, 30);
frontPageL.setHorizontalAlignment(JTextField.CENTER);
add(frontPageL);
setOpaque(true);
}
Also for good programming practice you'll want to avoid using a plain-vanilla Exception class and instead throw or catch specific exceptions.
Next, you're going to want to get out of the habit of using absolute size and position and instead using the layout managers for doing what they do best.
Edit:
replies to your recent comments
The reason I used public "JPanel" PortraitGUI is because it was throwing the error or return type required,
This was fixing the wrong thing though as the better solution was to make it a true constructor, not to give it a return type.
and I coded the class the same as create_Content_Pane(); with returning a Panel. Also the return type required error came up a few times.
Again, it's important to know why the error is occurring rather than fixing the wrong thing.
The update(getGraphics()); was also a method I tried from code examples I found with the same problem.
Surely that didn't come from a Swing example but more likely an older AWT example. You don't do that sort of coding with Swing.