Results 1 to 9 of 9
  1. #1
    Steelsmasher is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Using Graphics2D to write text in a different class

    I am trying to write a program that has a black background with "hello" text written in white on it, but I want to do it using a class without the paintComponent. So I try this..

    This is the Frame class that makes the window:

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class Frame extends JFrame {
    	
    	public Frame(){
            	this.setTitle("Test");
    			this.setSize(800, 600);
    			this.setLocationRelativeTo(null);
    			this.setResizable(false);
    			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			this.setContentPane(new Canvas());
    	        this.setVisible(true);
    	        this.setUndecorated(true);
    	}
    	
    
    	public static void main(String[] args){
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    new Frame();
                }
            });
        }
    
    }
    This is the Canvas class that contains the paintComponent which will pass on the g2d to the Draw method.
    The draw method then distributes the g2d to each class method.

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JPanel;
    
    public class Canvas extends JPanel {
    	
    	private Screen Screen;
    	
    	public Canvas(){
    		this.setDoubleBuffered(true);
            this.setFocusable(true);
            this.setBackground(Color.black);
    	}
    	
    	public void Draw(Graphics2D g2d){
    		Screen.Draw(g2d);
    	}
    	
    	public void paintComponent(Graphics g){
            Graphics2D g2d = (Graphics2D)g;        
            super.paintComponent(g2d);        
            Draw(g2d);
        }
    
    
    }
    And this is the Screen Class which draws the text on the black screen

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class Screen {
    	
    	public Screen(){
    		
    	}
    	
    	public void Draw(Graphics g2d){
    		g2d.setColor(Color.white);
    		g2d.drawString("Hello", 400, 300);
    	}
    
    }
    The problem is the window is transparent and seems frozen, however the program works perfectly if I tell the paintComponent to do this instead..

    Java Code:
    	public void paintComponent(Graphics g){
            Graphics2D g2d = (Graphics2D)g;        
            super.paintComponent(g2d);        
            g2d.setColor(Color.white);
            g2d.drawString("Hello", 400, 300);
        }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Using Graphics2D to write text in a different class

    Comment on coding style: Don't have a variable named the same as the class. It makes for confusion.
    Standards say variable and method names should start with lower case letter.

    the window is transparent and seems frozen,
    What error messages do you get when you execute the code? Please copy and post the full text.
    I expect a NPE.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Steelsmasher is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Using Graphics2D to write text in a different class

    Not sure what NPE is sorry. And I'm also not sure what you mean by variable the same as the class (are you talking of a constructor?)

    Anyway these are the errors I get from the Console in Eclipse

    Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable.
    at java.awt.Frame.setUndecorated(Unknown Source)
    at Frame.<init>(Frame.java:14)
    at Frame$1.run(Frame.java:21)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Canvas.Draw(Canvas.java:17)
    at Canvas.paintComponent(Canvas.java:23)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.BufferStrategyPaintManager.paint(Unkno wn Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Un known Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run( Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Using Graphics2D to write text in a different class

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Canvas.Draw(Canvas.java:17)
    In the middle of the stack trace is what I was expecting.

    What variable on line 17 has a null(default) value? It needs to be given a value.

    what you mean by variable the same as the class
    Java Code:
       private Screen Screen;   //<<<< variable name same as class name
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Steelsmasher is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Using Graphics2D to write text in a different class

    Quote Originally Posted by Norm View Post
    In the middle of the stack trace is what I was expecting.

    What variable on line 17 has a null(default) value? It needs to be given a value.
    Sorry I'm still not sure what you mean by stack trace, only really just starting java.

    Java Code:
    public void Draw(Graphics2D g2d){
            Screen.Draw(g2d);
        }
    Line 17 in the Canvas class is the Screen.Draw(g2d) part.
    What I thought should happen was.. the g2d would be passed on to the Draw method in the Screen class which would then write the text on the black background.

    Screen class

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
     
    public class Screen {
         
        public Screen(){
             
        }
         
        public void Draw(Graphics g2d){
            g2d.setColor(Color.white);
            g2d.drawString("Hello", 400, 300);
        }
     
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Using Graphics2D to write text in a different class

    Most of what is in post #3 is called a stack trace. When one method calls another and that method calls yet another and on and on, a record of those calls is saved. The print out in post#3 shows the history of those saved calls. You read the history from the bottom up. For the trace below: the paint()method called paintComponent() in Canvas, then there was a call to the Draw method where the NPE happened.
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Canvas.Draw(Canvas.java:17)
    at Canvas.paintComponent(Canvas.java:23)
    at javax.swing.JComponent.paint(Unknown Source)
    If this is line 17
    Java Code:
            Screen.Draw(g2d);
    where does the Screen (should be screen) variable get assigned a value?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Steelsmasher is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Using Graphics2D to write text in a different class

    I can't see why screen should be assigned a value at all. I didn't even know its a variable.

    When I did this..

    Java Code:
    private Screen screen;
    I assumed 'screen' will be used as a reference to the Screen class. So then I told it to pass the g2d to the Draw method of the Screen class.

    Java Code:
    public void Draw(Graphics2D g2d){
            screen.Draw(g2d);
        }
    I can't imagine how or why I would need to give a value to a class, if that is what you mean.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default Re: Using Graphics2D to write text in a different class

    I can't imagine how or why I would need to give a value to a class
    screen is a variable that can refer to an instance of the Screen class(when it is created with a new statement).
    Java Code:
      private Screen screen;  // define a variable(will have null value)
    Try this code to see what happens when an object reference variable has a null value:
    Java Code:
    String str = null;
    int len = str.length();
    There is no class object with a draw() method unless you create one with the new statement. The value of screen will be null and you will get a NPE as the above code does.
    Last edited by Norm; 08-12-2012 at 02:46 AM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Steelsmasher is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Using Graphics2D to write text in a different class

    Quote Originally Posted by Norm View Post
    There is no class object with a draw() method unless you create one with the new statement.
    Ah thanks works as it should now.

    I changed the draw method in the Canvas class to this

    Java Code:
    public void Draw(Graphics2D g2d){
        	Screen screen = new Screen();
            screen.Draw(g2d);
        }
    and removed the

    Java Code:
    private Screen screen;
    Strange though because I based this off another program but it managed to get away with doing that. Thanks.

Similar Threads

  1. How to write bytes to text file?
    By fatabass in forum New To Java
    Replies: 10
    Last Post: 09-18-2012, 08:46 PM
  2. Replies: 3
    Last Post: 04-27-2012, 03:41 PM
  3. write in a text file at the end
    By ahmakki in forum New To Java
    Replies: 0
    Last Post: 02-06-2011, 05:56 PM
  4. How do I write or read data text?
    By off99555 in forum Advanced Java
    Replies: 1
    Last Post: 12-08-2010, 11:37 AM
  5. how to write text on an existing image
    By jeshmal4u in forum Java 2D
    Replies: 6
    Last Post: 05-07-2010, 07:14 PM

Posting Permissions

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