Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Norm

Thread: the graphics won't show up when i use the methord paintComponent of JPanel class.HELP

  1. #1
    mcclain is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Unhappy the graphics won't show up when i use the methord paintComponent of JPanel class.HELP

    The code below can execute. I want to draw something on the right JPanel. But nothing appeared but the white background when pushed the button.
    I think there's something wrong with the methord paintComponent. How should I change the code? Someone help!!!

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;
    
    public class TestDemo2 extends JFrame{
    	public JTabbedPane jtp = new JTabbedPane();
    	public MyJPanel2 jp;
    	
    	public TestDemo2(){
    		jp = new MyJPanel2();
    		jtp.add("物资态势图", jp);
    		this.getContentPane().add(jtp);
    		
    		this.setVisible(true);
    		this.setSize(900, 600);
    		this.validate();
    	}
    	public static void main(String[] args) {
    		new TestDemo2();
    	}
    }
    class MyJPanel2 extends JPanel implements ActionListener{
    	public JLabel jl = new JLabel("plz input the id:");
    	public JTextField jtf = new JTextField(10);
    	public JButton jbtn = new JButton("query");
    	public JPanel j1;
    	public MyDrawPanel j2;
    	public MyJPanel2() {
    		this.setLayout(new BorderLayout());
    		this.add(getHeader(), BorderLayout.NORTH);
    
    		j1 = new JPanel();
    		j1.setBackground(Color.RED);
    		j2 = new MyDrawPanel();
    		j2.setBackground(Color.white);
    		this.add(getCenter(), BorderLayout.CENTER);
    	}
    	public JComponent getHeader() {
    		JPanel p1 = new JPanel();
    		p1.setLayout(new FlowLayout());
    		p1.add(jl);
    		p1.add(jtf);
    		jbtn.addActionListener(this);
    		p1.add(jbtn);
    		return p1;
    	}
    	public JComponent getCenter() {
    		JPanel p2 = new JPanel();
    		p2.setLayout(new GridLayout(1, 2));
    		p2.add(new JScrollPane(j1));
    		p2.add(j2);
    		return p2;
    	}
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == this.jbtn){
    			j2.draw();
    		}
    	}
    }
    class MyDrawPanel extends JPanel{
    	public MyDrawPanel(){
    		super();
    	}
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.setColor(Color.BLUE);
    		g.fillOval(this.getX(), this.getY()+50, this.getX()+150, this.getY()+150);
    		System.out.println(this.getSize());
    		System.out.println(this.getX() +"  "+this.getY());
    	}
    	public void draw(){
    		repaint();
    	}
    }
    Last edited by Fubarable; 10-01-2011 at 06:25 PM. Reason: code tags added

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

    Default Re: the graphics won't show up when i use the methord paintComponent of JPanel class.

    I am not near a computer with a Java compiler, but I'm concerned with your use of getX() and getY(). This will return the x and y for the position of the upper left corner of this component within its container and relative to the container's coordinate system, which is likely not what you want to do. Please tell us more about what actually is happening, if your buttons are working and any and all other details that may help us.

    Also, I added code tags to your post above to help allow your code to be readable. To do this yourself, add the tag [code] above your code block and [/code] below your code block.

  3. #3
    Join Date
    Aug 2011
    Location
    Pretoria, South Africa
    Posts
    27
    Blog Entries
    3
    Rep Power
    0

    Default Re: the graphics won't show up when i use the methord paintComponent of JPanel class.

    As far as I can see you are only trying to draw a oval shape on the right panel and fill it with the colour blue. If this is the case then I hope this code will help.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.RenderingHints;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;
    
    
    @SuppressWarnings("serial")
    public class TestDemo extends JFrame{
    	private MyDrawPanel panel2;
    	
    	TestDemo(){      
    		JTabbedPane jtp = new JTabbedPane();
    		jtp.add("Hallo", new MyPanel());
    		add(jtp);
    		
            setVisible(true);
            setSize(900, 600);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    	
    	public static void main(String[] args) {
            new TestDemo();
        }
    	
    	class MyPanel extends JPanel{
    		MyPanel(){
    			setLayout(new BorderLayout());
    			add(new getHeader(), BorderLayout.NORTH);
    			add(new getCenter(), BorderLayout.CENTER);
    		}
    	}
    	
    	class getHeader extends JPanel{
    		getHeader(){
    			setLayout(new FlowLayout());
    	        add(new JLabel("plz input the id"));
    	        add(new JTextField(10));
    	        
    	        JButton btn = new JButton("query");
    	        btn.addMouseListener(new MouseAdapter(){
    	        	public void mouseClicked(MouseEvent evt){
    	        		panel2.paintNow(panel2.getGraphics());
    	        	}
    	        });
    	        add(btn);
    		}
        }
    	
    	class getCenter extends JPanel{
    		getCenter(){
    			setLayout(new GridLayout(1,0));
    			
    			JPanel pnl = new JPanel();
    			pnl.setBackground(Color.RED);
    			add(new JScrollPane(pnl));
    			
    			panel2 = new MyDrawPanel();
    			panel2.setBackground(Color.WHITE);
    			add(panel2);
    		}
        }
    	
    	class MyDrawPanel extends JPanel{
    		public MyDrawPanel(){
    	    }
    	    public void paintNow(Graphics g){
    	    	Graphics2D g2d=(Graphics2D)g;
    	    	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    	    	g2d.setColor(Color.BLACK);
    	    	g2d.drawOval(0,50,150,150);
    	    	g2d.setPaint(Color.BLUE);
    	    	g2d.fillOval(0,50,150,150);
    	    }
    	}
    }

  4. #4
    mcclain is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Question Re: the graphics won't show up when i use the methord paintComponent of JPanel class.

    thanks, first. I'm not just drawing an oval, that's just an example to explain my problem. And i tried the code after you changed. It surely can draw a circle. But when you minimized the window, then maximum , the circle disappeared.
    I am trying to draw something on the right panel. I don't want it disappear when refresh the whole frame.

    And i'm very confused about how to use paintComponent() correctly. Someone help me please.

  5. #5
    Join Date
    Aug 2011
    Location
    Pretoria, South Africa
    Posts
    27
    Blog Entries
    3
    Rep Power
    0

    Default Re: the graphics won't show up when i use the methord paintComponent of JPanel class.

    No problem man, always happy to help. First things first, as soon as you minimize, maximize, move, setVisible to false or true the components inside the container will repaint itself to the original state... which in this case is without the the oval. if you want the oval there permanent you will have extend the JPanel with a paintComponent method where the oval shape is permanent. I cant give you a one set rule for paint, paintComponent or drawing methods, because ever situation is different from one another. If you can go into more detail on what you want to achieve I will be more than willing to help you.
    Last edited by DuvanSlabbert; 10-02-2011 at 03:03 AM.

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

    Default Re: the graphics won't show up when i use the methord paintComponent of JPanel class.

    For debugging try adding this to the end of your paintComponent method to find what the x,y values are:
    Java Code:
            for(int i=10; i < 400; i = i + 10) {
              g.drawString ("i=" + i, i ,i);
            }
    Fubarable likes this.

Similar Threads

  1. JPanel and graphics
    By nevermiind in forum AWT / Swing
    Replies: 5
    Last Post: 03-04-2011, 01:21 PM
  2. Graphics issue, pic won't show up.
    By AndroidAppNewbie in forum New To Java
    Replies: 1
    Last Post: 03-02-2011, 08:44 PM
  3. Graphics wont show up(paintComponent)
    By TheBreadCat in forum New To Java
    Replies: 3
    Last Post: 02-13-2011, 06:00 PM
  4. JPanel PaintComponent
    By capiono in forum AWT / Swing
    Replies: 5
    Last Post: 10-31-2010, 03:36 AM
  5. paint vs paintComponent for a JPanel
    By lightstream in forum AWT / Swing
    Replies: 4
    Last Post: 01-29-2009, 02:26 AM

Tags for this Thread

Posting Permissions

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