Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    I need help on java graphics

    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()));
    frame.setUndecorated(true);
    frame.setVisible(true);
    }
    public static void main(String[] args){
    Gui gameGui = new Gui();
    boolean running = true;
    while(running){
    update();
    try {
    Thread.sleep(20);
    } 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");
    }
    g.drawImage(img,20*x,20*y,null);
    }
    public static void update(){
    boolean mainmenu = false;
    boolean playing = false;
    boolean exiting = false;
    if(mainmenu==false&&playing==false&&exiting==false){
    Action.openMenu();
    }else if(playing==true){
    Action.openGame();
    }else if(exiting==true){
    Action.closeGame();
    }
    }
    }
    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");
    Gui.draw(null,0,0,back);
    }
    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
    g.drawImage(img,20*x,20*y,null);
    instead of
    g.drawImage(img,x,y,null);
    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)

  • #2
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    if you want a more detailed explanation of the code i can supply

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    help? please

  • #4
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    or at least recognition

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    javarower (02-27-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •