Results 1 to 13 of 13
  1. #1
    7oclock is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default Help me with graphics

    Hi,
    I am trying to learn Graphics2D. I read all those tutorials about Graphics2D from Java website but I am not being able to understand it. I mean, I can draw a line and all but I am trying to create this simple program where there's a button and when user clicks that button a line is drawn somewhere in the JFrame but I am not being able to. Can anyone help me in this or give me a simple code so I can learn from it?
    Thank you!!!
    Last edited by 7oclock; 03-31-2009 at 06:12 AM.

  2. #2
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    What code do you have so far? Are you getting any specific errors from it?

  3. #3
    7oclock is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    The first line shows error, Inside button click method, I have inserted "Graphics2D myLine;" like this:

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            Graphics2D myLine;
            ...
    }
    And it says to initialize myLine variable. Initialize it to what?
    Sorry I am totally a beginner in this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You need to use the Graphics object provided by the JComponent in its paintComponent method. I'm sorry if this sounds Greek to you, but my other recommendation will help translate this: if you are really serious about doing graphics programming in Java, buy the book "Filthy Rich Clients" by Guy and Haase.. It contains a gold-mine of first-class information on how to do Graphics coding. You won't regret it.

  5. #5
    7oclock is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    I figured it out myself. I need to initialize it to getGraphics(). Thanks anyway.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by 7oclock View Post
    I figured it out myself. I need to initialize it to getGraphics(). Thanks anyway.
    No, this is wrong.

    Your image will not persist if the GUI ever has to be repainted (for example minimized then restored). Please read the tutorials and use the Graphics object returned by paintComponent as I suggested and as the Graphics tutorial will tell you as well.

  7. #7
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    to solve the problem you need to use JPanel and JFrame at the same time )
    The code is quite separated but it should work
    watch it )
    Java Code:
    public class LineMaker extends JFrame{
    
    public LineMaker(){
    
    this.setSize(200,200);
    this.setVisible(true);
    this.setDefaultCloseOperation(3);
    this.getContentPane().add(new DrawLine ());
    }
    
    }
    
    class DrawLine extends JPanel{
    
    JButton b=new JButton();
    public DrawLine (){
    
    this.add(b);
    }
    
    public void paintComponent(Graphics g){
    
    super.paintComponent(g);
    g.drawLine(x,y,x,y);
    
    }
    }
    you can figure out what next but it is the basic direction )))

  8. #8
    7oclock is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    Yeah, getGraphics's useless. lol Ok I have done as you said, I am using paint component and JPanel. Here's my code:

    Java Code:
    public class my2D extends javax.swing.JFrame {
    
    .
    .
    .
    
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            MyDrawingP DU = new MyDrawingP();
            DU.repaint();
            
      }  
    
    .
    .
    .
    
    public class MyDrawingP extends JPanel {
        
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawLine(100, 100, 200, 200);
        }
    }
    }
    But it won't work. When I click on the button, nothing happens. Isn't 'repaint' right way to call paintComponent?
    Last edited by 7oclock; 04-01-2009 at 06:20 PM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're creating a MyDrawingP object but where is it being placed so that it is visible on the JFrame or JDialog or JApplet? Just creating a JPanel even if you call setVisible(true) on it will not make it visible. You first need to eithr place it into a root container or in another container that eventually sits in a root container. To learn more about this, read the Sun Swing tutorials on creating and using Frames and Panels.

    Good luck.

  10. #10
    Normek is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default

    I can reproduce and modify the examples given in the Sun Swing tutorials, which deal with drawing in the main panel, but I cannot find how to anchor a drawing into an internal container within the main panel.
    Could anyone point me to where I could find clues on how to do that?

  11. #11
    7oclock is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    Thank you Fubarable and Webuser for helping. :)

  12. #12
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    But it won't work. When I click on the button, nothing happens. Isn't 'repaint' right way to call paintComponent?
    No... no... YOu should add the panel right to your frame like a
    Java Code:
    this.getContentPane().add(new MyDrawingP());
    you get I mean?

  13. #13
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    But to evoid the adding multi effect you may use removeAll() or so )

Similar Threads

  1. SWT Graphics Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:28 PM
  2. Classes in graphics
    By CyberFrog in forum New To Java
    Replies: 0
    Last Post: 04-02-2008, 09:11 PM
  3. Graphics
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2008, 06:24 PM
  4. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 07:44 PM
  5. Graphics
    By feniger in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 04:22 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
  •