View Full Version : I need help on java graphics

02-26-2012, 12:16 AM
Hello I am trying to make a program that will create an undecorated full screen gui and then put a picture in the gui. The ultimate goal is to have a game writing different game aspects to the picture and then rendering them to the screen. For now i am just trying to get one picture on it. Here is my code:

import javax.swing.*;
import java.text.DecimalFormat;
import java.awt.*;
import javax.swing.JFrame;

public class Gui extends JFrame{
public Gui(){
JFrame frame = new JFrame();
Toolkit tk = Toolkit.getDefaultToolkit();
frame.setBounds(new Rectangle(new Point(0, 0), tk.getScreenSize()));
public static void main(String[] args){
Gui gameGui = new Gui();
boolean running = true;
try {
} catch (InterruptedException ex) {


public static void draw(Graphics g,int x, int y, Image img){
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
double screenHeight=dim.getHeight();
double numOQ=(screenHeight)/20;
int numOfQuads = (int)numOQ;
if(x>numOfQuads || y>numOfQuads){
System.out.println("Invalid Custom Coords");
public static void update(){
boolean mainmenu = false;
boolean playing = false;
boolean exiting = false;
}else if(playing==true){
}else if(exiting==true){
New Class(I am using Eclipse)

import java.awt.Image;
import java.awt.Toolkit;

public class Action {
public static void openMenu(){
Image exitgame = Toolkit.getDefaultToolkit().getImage("exitgame.xcf");
Image loadgame = Toolkit.getDefaultToolkit().getImage("loadgame.xcf");
Image newgame = Toolkit.getDefaultToolkit().getImage("newgame.xcf");
Image options = Toolkit.getDefaultToolkit().getImage("options.xcf");
Image back = Toolkit.getDefaultToolkit().getImage("back.jpg");
public static void openGame(){

public static void closeGame(){


I have some errors when ever i run it and i do not know the problem. In case you are wondering the reason i use
instead of
is that i want to make it easier for me down the line in making this program to put things on the screen in preset boxes(every 20 pixels)

02-26-2012, 12:21 AM
if you want a more detailed explanation of the code i can supply

02-26-2012, 02:21 AM
help? please

02-26-2012, 02:21 AM
or at least recognition

02-26-2012, 07:20 PM
Bumping every couple of hours on a Saturday night doesn't really help if nobody's here to answer.
This code is a mess. You are instantiating a JFrame within your constructor that cannot be tracked. Gui is a typeof JFrame, there is no reason to invoke another one. These methods are all static, but you don't provide access to the given Gui in order to draw on the graphics itself. Graphics in draw will be null, so you have nothing to write to (and will throw a null pointer exception).

You have to go back to the drawing board and change these into proper object instance access. Retaining static methods will force you to provide an instance of the Gui in several locations, but can be done so long as you pull the proper object access when necessary and pull the getGraphics out of the Gui. I would rewrite Action to return the Image or Graphics when called and do all of this in a repaint instead.