CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    161

    Draw Thick line inside a specific panel

    Hello everyone,

    I am trying to draw lines inside panles or other components but I don't understand how this works.
    I mean lets say I have a JPanel dArea= new JPanel(); where I have some pictures and other writing inside, how would I draw lines inside that panel instead of drawing lines on the main frame

    I have the code to draw thick line but its now drawing inside components that I want.
    Code:
    public void thickLine(Graphics g) 
    	{
    		
    		
    		 g.drawLine(0,0,100,20);    //default
    		 Graphics2D g2 = (Graphics2D) g;
    	     g2.setStroke(new BasicStroke(3));
    	     g2.drawLine(0,100,20,0);   //thick
    	     
    		
    	}

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Draw Thick line inside a specific panel

    What you need to look at is "where" you are calling that method from. If your main class is extending JFrame and that method resides in that class, then you will be drawing inside of the main panel. If you want to write code that draws inside of a custom pane then you need to write a separate class for that.

    Here is an example:

    Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Line2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class DrawingLines extends JFrame {
    
        public DrawingLines() {
            initComponents();
        }
    
        private void initComponents() {
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            this.setSize(500, 500);
            InnerFrame frame = new InnerFrame();
            this.add(frame);
            pack();
        }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new DrawingLines().setVisible(true);
                }
            });
        }
        
        class InnerFrame extends JPanel {
            public void paint(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                Line2D lin = new Line2D.Float(100, 100, 250, 260);
                g2.draw(lin);
            }
        }
    }
    Here I have an inner class that overrides the paint method and draws a line, and I add this panel to the main frame.

  3. #3
    Join Date
    Jan 2010
    Posts
    161

    Re: Draw Thick line inside a specific panel

    Quote Originally Posted by ProgramThis View Post
    What you need to look at is "where" you are calling that method from. If your main class is extending JFrame and that method resides in that class, then you will be drawing inside of the main panel. If you want to write code that draws inside of a custom pane then you need to write a separate class for that.

    Here is an example:

    Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Line2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class DrawingLines extends JFrame {
    
        public DrawingLines() {
            initComponents();
        }
    
        private void initComponents() {
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            this.setSize(500, 500);
            InnerFrame frame = new InnerFrame();
            this.add(frame);
            pack();
        }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new DrawingLines().setVisible(true);
                }
            });
        }
        
        class InnerFrame extends JPanel {
            public void paint(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                Line2D lin = new Line2D.Float(100, 100, 250, 260);
                g2.draw(lin);
            }
        }
    }
    Here I have an inner class that overrides the paint method and draws a line, and I add this panel to the main frame.
    thank you,
    but how i understand that a new class is needed to achieve what I want .
    but in your code its drawing a line only.
    how would i like make a jpanel inside a jframe ,with background yello and on the left size of the screen and draw lines inside it.

    i know how to set size etc, but the reason im asking it its because when i run your code i can see only one frame and wanted to know, if is possible for you, how would i make a jpanel smaller then the fram with a different color(in order to distinguish it) and then draw lines inside that jpanel?

    one more thing, you said that if i extend the jframe in the main class ,lines that i will draw will go inside the main jframe and you're right.
    so make it work in a panel ill have to extend the jpanel but what if I have several panels, by using extends jpanel how will java understand whichi specific jpanel I want to draw lines in?

  4. #4
    Join Date
    Jan 2010
    Posts
    161

    Re: Draw Thick line inside a specific panel

    I have manged to make it work by setting to null the layout of the main frame,then i could set the size and location of the jpanel
    but when i set the color it doesn't change the color of the background of the jpanel

  5. #5
    Join Date
    Feb 2008
    Posts
    966

    Re: Draw Thick line inside a specific panel

    Quote Originally Posted by cpu2007 View Post
    thank you,
    but how i understand that a new class is needed to achieve what I want .
    Through experience. You have to first understand how the Java API works for AWT and Swing components. You really should read some tutorials.
    Quote Originally Posted by cpu2007 View Post
    but in your code its drawing a line only.
    how would i like make a jpanel inside a jframe ,with background yello and on the left size of the screen and draw lines inside it.
    I gave you an example of how to do the basic task of adding separate panels to the main frame. I don't want to do your entire assignment, so I won't show you how to add multiple panels and position them. Please read up on some tutorials on how to do this and come back here with specific questions when you get stuck. I would love to help you then, but you have to make a little more effort please.
    Quote Originally Posted by cpu2007 View Post
    i know how to set size etc, but the reason im asking it its because when i run your code i can see only one frame and wanted to know, if is possible for you, how would i make a jpanel smaller then the fram with a different color(in order to distinguish it) and then draw lines inside that jpanel?
    It is indeed possible to make the jpanel smaller than the frame and to even have multiple panels. Start by reading this tutorial from Sun on how to add panels to frames.
    Quote Originally Posted by cpu2007 View Post
    one more thing, you said that if i extend the jframe in the main class ,lines that i will draw will go inside the main jframe and you're right.
    so make it work in a panel ill have to extend the jpanel but what if I have several panels, by using extends jpanel how will java understand whichi specific jpanel I want to draw lines in?
    No, you don't extend jpanel in the main class. Your main class is the Frame. Inside of the frame we want to add Panels (other classes). Each separate class that extends jpanel will be something added to the main canvas (jFrame, your main class).

  6. #6
    Join Date
    Jan 2010
    Posts
    161

    Re: Draw Thick line inside a specific panel

    Thank you for the reply.
    Yeah i know I should be reading more about it, the problem is this is all about research and we are supposed to study and at the same time make something...if I start reading about the ways Java swing and components work I will not have time to make my project reason why I am studying specifig things which ofcourse are making me struggle as I am skipping some level.

    I have been playing around and I have managed to understand how all this classes work and how the extends work...now I can add panels or other components set the size and other properties, everything seem to have more sense in the way it works now

    one of the problem I am having is that I set my JPanel to a specific color and size which is working fine, but when i use the paint method to draw grids on it ,it somehow overrides the background color and i only see the grid lines
    here is the code, I cant figure out where I am making the mistake

    Code:
    public class dArea extends JPanel
     {
    	public dArea()
    	 {
    	   this.setLayout(null);
    	   this.setBounds(10, 70, 680, 680);
    	    this.setBackground(Color.blue);
    	 }
    	public void drawArea()
    		{
    			//super.paintComponents(g);
    			//asphalt.setBounds(0, 0, 620, 620);
    		}	
    	
    	public void paint(Graphics g) {
           Graphics2D g2 = (Graphics2D) g;
         
           this.setBackground(Color.black);
            for (int i=0;i<200;i+=20)
            {
            	g.drawLine(0, i, 600, i);
            	
            }
            for (int i=0;i<200;i+=20)
            {
            	g.drawLine(i, 0, i, 180);
            	
            }
          // this.setBackground(Color.blue); I have set the backgound here too but still the    //background its not there when i draw the lines 
        }

  7. #7
    Join Date
    Feb 2008
    Posts
    966

    Re: Draw Thick line inside a specific panel

    In the method / place where you create an instance of your sub class, you call that object's "setBackground()" method at that time. Also, in the paint() method of the sub class, you need to add the line:

    super.paintComponent(g);

    Code:
        class InnerFrame extends JPanel {
            public InnerFrame() {
                this.setBackground(Color.RED);
                setPreferredSize(new Dimension(400,400));
            }
            public void paint(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                Graphics2D g2 = (Graphics2D) g;
                Line2D lin = new Line2D.Float(100, 100, 250, 260);
                g2.draw(lin);
                
            }
        }

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured