Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Help about Graphics

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help about Graphics

    Hello everyone

    I have a problem as I built a drag-drop gui appilcation using netbeans, and I wand to add some drawings (using filloval or so) but I have no Idea about how to do so with the generated code of the netbeans.

    also I know how to do it in normal hand build gui, but I am asking about the drag-drop gui of netbeans.

    Thanks in advance


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Help about Graphics

    Last edited by Darryl.Burke; September 7th, 2010 at 01:30 AM. Reason: Added underline for link

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Location
    Netherlands
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help about Graphics

    I guess you should just:
    import java.awt.Graphics;
    After you've imported the graphics, you can make an method which draws your Oval.
    Like this:
    public void DrawtheOval(Graphics g) {
    g.fillOval(x,y,width,height);
    }

    Well sometimes you might have to use the method in another class/method, to make the Oval to actually get drawn.

    Hope this helped a bit

    PS: what is cross posting? ;x
    Last edited by Varial; September 7th, 2010 at 03:21 AM.

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Help about Graphics

    Quote Originally Posted by Varial View Post
    I guess you should just:
    import java.awt.Graphics;
    After you've imported the graphics, you can make an method which draws your Oval.
    Like this:
    public void DrawtheOval(Graphics g) {
    g.fillOval(x,y,width,height);
    }

    Well sometimes you might have to use the method in another class/method, to make the Oval to actually get drawn.

    Hope this helped a bit

    PS: what is cross posting? ;x
    It lets the forums know that the same person has asked the question at multiple forums.

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help about Graphics

    I tried what you have said, as follows:

    class draw{
    public void DrawtheOval(Graphics g) {
    g.setColor(Color.BLACK);
    g.fillOval(30,30,30,30);
    }
    }

    then at the beginning of netbeans generated code:
    draw d = new draw();


    The question now is about the next to step. to call the method I have to get it from object "d" but I have to pass some Graphics object to it. What will be the next step?

    also I want to add this Oval in a panel (panel generated by netbeans gui designer, not by hand code). How about this?

    I have tried something but it has not worked (although it seems logic):
    jPanel.getGraphics().drawOval(30, 30, 30, 30);


    Thanks for your help

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help about Graphics

    Override the paintComponent method for the panel and put your drawing code there.

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help about Graphics

    another way would depend on what you're drawing too. If it's a JPanel I would do something like this:
    JPanel myPanel = new DrawMyPanel();

    and below I would have a the DrawMyPanel class

    public class DrawMyPanel extends JPanel {
    @Override
    paintComponent(Graphics g) {
    Graphics2d 2dg;

    if (g instanceOf Graphics2D) {
    2dg = (Graphics2D)g;
    }

    // now you can draw your graphics
    g.setColor(Color.blue);

    g.drawOval(x, y, width, height);
    }

  8. #8
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: Help about Graphics

    look at this approach for drawing forms. first of all you need a frame that holds the canvas for your paintings. here is the code of the DrawableFrame.java

    public class DrawableFrame extends java.awt.Frame {
     
    	public DrawableFrame(String title) {
    		// set the title of the frame
    		super(title);
    		// create a drawable canvas for the frame
    		DrawableCanvas drawableCanvas = new DrawableCanvas();
    		// add the canvas to the frame
    		add(drawableCanvas);
    		// set the desired size
    		setSize(400, 400);
    		// set the desired position
    		setLocation(300, 200);
    		// set the frame visible
    		setVisible(true);
     
    		// for closing the window
    		addWindowListener(new java.awt.event.WindowAdapter() {
    			public void windowClosing(java.awt.event.WindowEvent we) {
    				System.exit(0);
    			}
    		});
    	}
    }

    look at the comments in the code. next, you will need the canvas where you instantiate your forms. here is the code of DrawableCanvas.java

    import java.awt.Color;
     
    public class DrawableCanvas extends java.awt.Canvas {
     
    	public void paint(java.awt.Graphics g) {
     
    		// create instances of class DrawableClasses and send message draw()
     
    			ColoredRectangle rec1 = new ColoredRectangle(22, 22, 150, 150, Color.blue); 
    			// Note, each class share the same Graphics g
    			rec1.draw(g);
    			ColoredOval oval1 = new ColoredOval(44, 44, 150, 150, Color.blue);
    			oval1.draw(g);
     
    	}
    }

    inside the canvas the two forms Rectangle and Oval are instantiate and with the call of draw(g) also painted on the canvas. if you need further forms copy/paste the code an adapt it to the form you need. here's is the code of the ColoredRectangle.java

    import java.awt.Color;
     
    public class ColoredRectangle {
     
    	private int x;
    	private int y;
    	private int width;
    	private int height;
    	private java.awt.Color color;
    	private java.awt.Color line_color = Color.WHITE;
     
    	public ColoredRectangle(int x, int y, int width, int height, java.awt.Color c ) {
    		this.x = x;
    		this.y = y;
    		this.width = width;
    		this.height = height;
    		this.color = c;
    	}
    	public void draw(java.awt.Graphics g) {
    		g.setColor(line_color);
    		g.drawRect(x, y, width, height);
    		g.setColor(color);
    		g.fillRect(x + 1, y + 1, width - 1, height - 1);
    	}	
    }

    and here the code for the ColoredOval.java

    import java.awt.Color;
     
    public class ColoredOval {
    	private int x;
    	private int y;
    	private int width;
    	private int height;
    	private java.awt.Color color;
    	private java.awt.Color line_color = Color.WHITE;
     
    	public ColoredOval(int x, int y, int width, int height, java.awt.Color c ) {
    		this.x = x;
    		this.y = y;
    		this.width = width;
    		this.height = height;
    		this.color = c;
    	}
    	public void draw(java.awt.Graphics g) {
    		g.setColor(line_color);
    		g.drawOval(x, y, width, height);
    		g.setColor(color);
    		g.fillOval(x + 1, y + 1, width - 1, height - 1);
    	}
    }

    note that two calls are made inside the draw()-method. one for drawing the form and the other for drawing the borders of the form, which is helpful when the forms are overlapping.

    and last but not least the class with the main method(). inside the main() only the DrawableFrame is instantiated.

    public class DrawableExercise {
     
        /* methods */
        public static void main(String[] args) {
            new DrawableFrame("Some drawables...");
        }
    }

    hope you got some ideas with this code. you can copy/paste and then compile and run the code.

  9. #9
    Junior Member
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help about Graphics

    Thank you very much for your help.

    The Problem is that I want to do this by Gui builder of Netbeans, so the question now is: How can I override the paint() method in Canvas or in Panel?

    Or Is there another methods that I can add drawings to Canvas or Panel in runtime?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help about Graphics

    How can I override the paint() method in Canvas or in Panel?
    Do you know how to use an anonymous class? That's one of the easiest ways to override an method.
    Panel pnl = new Panel() {
      @Override
      public void paint(Graphics g) {
        // do your painting here
      }
    };

  11. #11
    Junior Member
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help about Graphics

    Yes, But In Gui builder I do not create neither panel nor canvas with something like : Panel pnl = new Panel()
    but it is a matter of drag and drop.

    Your solution works only if I made the code by hand form the beginning

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help about Graphics

    Sorry, I don't know anything about using your IDE. I thought you were writing a java program, but I see its the IDE that is writing the program.
    Last edited by Norm; September 9th, 2010 at 02:40 PM.

  13. #13
    Junior Member
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help about Graphics

    I thought that netbeans is a famous IDE and I thought that I can get help concerning dealing with some helpful tools in it as Gui builder

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help about Graphics

    Perhaps you should have named this thread: Need help using Netbeans.
    Your title says you want help using Grahpics.

Similar Threads

  1. Graphics mayhem
    By javapenguin in forum AWT / Java Swing
    Replies: 2
    Last Post: August 9th, 2010, 05:21 PM
  2. graphics in job?
    By SweetyStacey in forum The Cafe
    Replies: 10
    Last Post: May 3rd, 2010, 03:29 PM
  3. How do i use graphics with Java?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 4
    Last Post: December 27th, 2009, 05:16 PM
  4. Increasing performance of my graphics routines
    By willberg in forum AWT / Java Swing
    Replies: 6
    Last Post: November 16th, 2009, 01:41 PM
  5. Simple graphics practice on previous Java code
    By amrawad_85 in forum AWT / Java Swing
    Replies: 5
    Last Post: June 19th, 2009, 10:30 AM