Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-30-2009, 12:48 PM
Member
 
Join Date: Feb 2009
Posts: 9
Rep Power: 0
7oclock is on a distinguished road
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 07:12 AM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-30-2009, 01:48 PM
logicbug's Avatar
Member
 
Join Date: Jan 2009
Location: The Great White North, eh?
Posts: 76
Rep Power: 0
logicbug is on a distinguished road
Default
What code do you have so far? Are you getting any specific errors from it?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-30-2009, 02:36 PM
Member
 
Join Date: Feb 2009
Posts: 9
Rep Power: 0
7oclock is on a distinguished road
Default
The first line shows error, Inside button click method, I have inserted "Graphics2D myLine;" like this:

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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-30-2009, 07:45 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,465
Rep Power: 8
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-31-2009, 07:34 AM
Member
 
Join Date: Feb 2009
Posts: 9
Rep Power: 0
7oclock is on a distinguished road
Default
I figured it out myself. I need to initialize it to getGraphics(). Thanks anyway.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-31-2009, 07:42 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,465
Rep Power: 8
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-01-2009, 08:12 AM
Senior Member
 
Join Date: Dec 2008
Posts: 197
Rep Power: 2
Webuser is on a distinguished road
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 )
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 )))
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-01-2009, 07:18 PM
Member
 
Join Date: Feb 2009
Posts: 9
Rep Power: 0
7oclock is on a distinguished road
Default
Yeah, getGraphics's useless. lol Ok I have done as you said, I am using paint component and JPanel. Here's my code:

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 07:20 PM.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-01-2009, 07:24 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,465
Rep Power: 8
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-02-2009, 01:57 PM
Member
 
Join Date: Apr 2009
Posts: 1
Rep Power: 0
Normek is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-04-2009, 09:42 AM
Member
 
Join Date: Feb 2009
Posts: 9
Rep Power: 0
7oclock is on a distinguished road
Default
Thank you Fubarable and Webuser for helping.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-05-2009, 12:19 AM
Senior Member
 
Join Date: Dec 2008
Posts: 197
Rep Power: 2
Webuser is on a distinguished road
Default
Quote:
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
Code:
this.getContentPane().add(new MyDrawingP());
you get I mean?
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-05-2009, 12:20 AM
Senior Member
 
Join Date: Dec 2008
Posts: 197
Rep Power: 2
Webuser is on a distinguished road
Default
But to evoid the adding multi effect you may use removeAll() or so )
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SWT Graphics Example Java Tip SWT 0 06-28-2008 10:28 PM
Classes in graphics CyberFrog New To Java 0 04-02-2008 10:11 PM
Graphics Joe2003 Advanced Java 1 01-25-2008 07:24 PM
graphics Joe2003 Advanced Java 4 01-18-2008 08:44 PM
Graphics feniger New To Java 1 12-29-2007 05:22 PM


All times are GMT +2. The time now is 06:13 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org