Results 1 to 8 of 8
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Custom Painting - RoundRect2D JPanel (Only 1 corner curved)

    What I am trying to do is have a class that extends JPanel and does custom painting, so I can specify arcWidth & arcHeight and have a RoundRectangle2D placed instead of the default Rectangle that is used for JPanels.

    Java Code:
    public class WindowPanel extends JPanel{
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private int x, y, width, height, arcWidth, arcHeight;
    	private Color c;
    	
    	public WindowPanel(int x, int y, int width, int height, int arcWidth, int arcHeight, Color c){
    		this.x = x;
    		this.y = y;
    		this.width = width;
    		this.height = height;
    		this.arcWidth = arcWidth;
    		this.arcHeight = arcHeight;
    		this.c = c;
    	}
    	
    
    	public int getX() {
    		return x;
    	}
    
    	public void setX(int x) {
    		this.x = x;
    	}
    
    	public int getY() {
    		return y;
    	}
    
    	public void setY(int y) {
    		this.y = y;
    	}
    
    	public int getWidth() {
    		return width;
    	}
    
    	public void setWidth(int width) {
    		this.width = width;
    	}
    
    	public int getHeight() {
    		return height;
    	}
    
    	public void setHeight(int height) {
    		this.height = height;
    	}
    
    	public int getArcWidth() {
    		return arcWidth;
    	}
    
    	public void setArcWidth(int arcWidth) {
    		this.arcWidth = arcWidth;
    	}
    
    	public int getArcHeight() {
    		return arcHeight;
    	}
    
    	public void setArcHeight(int arcHeight) {
    		this.arcHeight = arcHeight;
    	}
    
    	public Color getBackground() {
    		return c;
    	}
    
    	public void setBackground(Color c) {
    		this.c = c;
    	}
    	
    	@Override
    	protected void paintComponent(Graphics g){
    		Graphics2D gg = (Graphics2D) g;
    		gg.setColor(getBackground());
    		gg.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
    		
    	}
    }
    And then to call I simply create a new WindowPanel object :
    Java Code:
    WindowPanel legend = new WindowPanel(leftPad, topPad, (int)((Starter.dim.getWidth()-(leftPad + divPad + rightPad))*.25), (int)(Starter.dim.getHeight()-(topPad + bottomPad)), 25, 25, black);
    add(legend);
    THE PROBLEM - For some reason, my JPanel is being created and placed in the appropriate place, but only the top left corner has a curve. The other 3 corners are right angles.


    EXPECTED - I expected that all 4 corners would use the arcWidth and arcHeight and have the appropriate curve.

    QUESTION - What in my code is causing this? I would imagine there is a way to maybe @Override something to define each corner, but I haven't done anything like that. I simply tried to build a separate "JPanel factory" class.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,768
    Rep Power
    5

    Default Re: Custom Painting - RoundRect2D JPanel (Only 1 corner curved)

    The problem is you are overriding methods in the JPanel that you shouldn't be overridden (unless you understand the ramifications).
    Don't override getX(), set or get background, or getHeight. Simply do a g.setColor(c) in your paintComponent(Graphics g) method. Also,
    your first call in paintComponent(Graphics g) should be super.paintComponent(g).

    I recommend you read the following tutorial. Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Re: Custom Painting - RoundRect2D JPanel (Only 1 corner curved)

    They were being overriden because RoundRectangle2D inherited those methods from Class RectangularShape already, correct?

    I did have the call to
    Java Code:
     super.paintComponent(g);
    at one point but evidently took it out for some reason.

    MAIN CLASS -
    Java Code:
    public Main(){
    		available = (int)(dim.getWidth()-(leftPad + divPad + rightPad));
    		
    		frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setMinimumSize(dim);
    		frame.setMaximumSize(dim);
    		frame.setPreferredSize(dim);
    		frame.setResizable(false);
    		frame.setUndecorated(true);
    		
    		addPanels();
    		frame.pack();
    		frame.setVisible(true);
    		
    		timer = new Timer(delay, new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// update
    				
    				// paint
    				
    			}
    		});
    		timer.start();
    	}
    	
    	private void addPanels(){
    		bg = new RectWindow(0, 0, (int)dim.getWidth(), (int)dim.getHeight(), 50, 50, dark);
    		frame.add(bg);
    		legend = new RectWindow(leftPad, 0, (int)(available*.25), (int)(dim.getHeight()-(topPad + bottomPad)), 50, 50, black);
    		bg.add(legend);
    		wBox = new RectWindow((leftPad + legend.getWidth() + divPad), topPad, (int)(available*.75), (int)(dim.getHeight()-(topPad + bottomPad)), 50, 50, black);
    		bg.add(wBox);
    	}
    	
    	public static void main(String[] args){
    		EventQueue.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				new Main();
    			}
    		});
    	}	
    }

    RECTWINDOW CLASS -
    Java Code:
    public class RectWindow extends JPanel{
    	
    	private int x, y, width, height, arcWidth, arcHeight;
    	private Color c;
    	
    	public RectWindow(int x, int y, int width, int height, int arcWidth, int arcHeight, Color c){
    		this.x = x;
    		this.y = y;
    		this.width = width;
    		this.height = height;
    		this.arcWidth = arcWidth;
    		this.arcHeight = arcHeight;
    		this.c = c;
    	}
    
    	@Override
    	protected void paintComponent(Graphics g){
    		super.paintComponent(g); 
    		g.setColor(c);
    		g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
    		g.dispose();
    	}
    }
    Last edited by rru96; 06-22-2014 at 12:35 AM. Reason: Code added

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,768
    Rep Power
    5

    Default Re: Custom Painting - RoundRect2D JPanel (Only 1 corner curved)

    You are making the problem too complicated. And you have other variables of which I do not know the values. Here is what I recommend you do.

    Simply create a class like this:

    Java Code:
    class WindowPanel extends JPanel {
       public WindowPanel() {
             setPreferredSize(new Dimension(500,500));
       }
     
       public void paintComponent(Graphics g) {
             super.paintComponent(g);
             Graphics2D gg = (Graphics2D) g;
             gg.setColor(Color.blue);
             gg.fillRoundRect(100,100,300,300,50,50);
    
       }
    }

    Now add an instance of that to a JFrame instance, call pack() on the JFrame and setVisible(true).
    Once you get the demo working you can modify it to use your variables. But don't override
    anything in JPanel except paintComponent().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Re: Custom Painting - RoundRect2D JPanel (Only 1 corner curved)

    Well I thank you for the help. I was able to get your version working fine. But started running into issues when trying to use multiple instances of the "WindowPanel" and replacing hardcoded values with variables.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,768
    Rep Power
    5

    Default Re: Custom Painting - RoundRect2D JPanel (Only 1 corner curved)

    I not certain how you are using multiple instances. One JPanel can hide another if you aren't using the proper layout manager. In the paintComponent method you should also print the values you are using as arguments to the fillRoundRect() method to ensure they make sense.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Re: Custom Painting - RoundRect2D JPanel (Only 1 corner curved)

    Here is my current code.
    MAIN -
    Java Code:
    public class Main{
    
    	private Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    	private JFrame frame;
    	private RectWindow legend, bg, wBox;
    	private int topPad = 50, bottomPad = 50, leftPad = 50, divPad = 50, rightPad = 50;
    	private int available;
    	private Color black = new Color(0,0,0);
    	private Color white = new Color(255,255,255);
    	private Color dark = new Color(50,50,50);
    	private Color light = new Color(205,205,205);
    	private Timer timer;
    	private int delay = 1000;
    	
    	
    	public Main(){
    		available = (int)(dim.getWidth()-(leftPad + divPad + rightPad));
    		
    		frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setMinimumSize(dim);
    		frame.setMaximumSize(dim);
    		frame.setPreferredSize(dim);
    		frame.setResizable(false);
    		frame.setUndecorated(true);
    		
    		addPanels();
    		
    		frame.pack();
    		frame.setVisible(true);
    		
    		timer = new Timer(delay, new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// update
    				// paint
    			}
    		});
    		timer.start();
    	}
    	
    	private void addPanels(){
    		bg = new RectWindow(0, 0, (int)dim.getWidth(), (int)dim.getHeight(), 0, 0, dark);
    		bg.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		Insets i = new Insets(0, 0, 0, 0);
    		c.gridx = 0;
    		c.gridy = 0;
    		c.insets = i;
    		legend = new RectWindow(leftPad, topPad, (int)(available*.25), (int)(dim.getHeight()-(topPad + bottomPad)),25,25, black);
    		bg.add(legend,c);
    		
    		c.gridx = 1;
    		c.gridy = 0;
    		wBox = new RectWindow((leftPad + legend.getWidth() + divPad), topPad, (int)(available*.75), (int)(dim.getHeight()-(topPad + bottomPad)), 25, 25, black);
    		bg.add(wBox,c);
    		
    		frame.add(bg);
    	}
    	
    	public static void main(String[] args){
    		EventQueue.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				new Main();
    			}
    		});
    	}	
    }

    RectWindow -
    Java Code:
    public class RectWindow extends JPanel {
    
    	private int x, y, width, height, arcWidth, arcHeight;
    	private Color c;
    	
    	
    	   public RectWindow(int x, int y, int width, int height, int arcWidth, int arcHeight, Color c) {
    		//   this.x = x;
    		//   this.y = y;
    		//   this.width = width;
    		//   this.height = height;
    		//   this.arcWidth = arcWidth;
    		//   this.arcHeight = arcHeight;
    		//   this.c = c;
    	
    		   setPreferredSize(new Dimension(width,height));
    		   setArcWidth(arcWidth);
    		   setArcHeight(arcHeight);
    		   setBackground(c);
    	   }
    	  	public void setArcWidth(int arcWidth) {
    			this.arcWidth = arcWidth;
    		}
    	
    	  	public void setArcHeight(int arcHeight) {
    			this.arcHeight = arcHeight;
    		}
    	  	
    	  	public int getArcWidth(){
    	  		return arcWidth;
    	  	}
    	  	
    	  	public int getArcHeight(){
    	  		return arcHeight;
    	  	}
    	   
    	   public void paintComponent(Graphics g) {
    		   System.out.println("X,Y - WxH - NAME = " + getX() + "," + getY() + " - " + getWidth() + " x " + getHeight());
    		   System.out.println("ARCWIDTH - ARCHEIGHT = " + arcWidth + " - " + arcHeight);
    	         super.paintComponent(g);
    	         Graphics2D gg = (Graphics2D) g;
    	         gg.setColor(c);
    	         gg.fillRoundRect(getX(),getY(),getWidth(),getHeight(),getArcWidth(),getArcHeight());
    	 
    	   }
    }
    As you can see... the result is all wonky.

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,768
    Rep Power
    5

    Default Re: Custom Painting - RoundRect2D JPanel (Only 1 corner curved)

    Quote Originally Posted by rru96 View Post
    As you can see... the result is all wonky.
    If you say so. But I can't tell what you are trying to do. First, just work with one RectWindow at a time. Get the first one displaying like you want. Then work with the next one. And make certain ALL of your variables are what you expect them to be when you paint the windows.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Painting a JComponent onto a JPanel
    By typedef in forum New To Java
    Replies: 1
    Last Post: 05-13-2014, 04:34 PM
  2. Problem with painting a JPanel
    By Navarrano in forum New To Java
    Replies: 12
    Last Post: 04-12-2012, 09:47 PM
  3. painting to a JPanel in a JFrame
    By Parkournerd in forum AWT / Swing
    Replies: 1
    Last Post: 01-16-2012, 09:24 PM
  4. Swing components vs. custom painting
    By Cruncher in forum New To Java
    Replies: 6
    Last Post: 05-06-2010, 07:16 AM
  5. JPanel not always painting everything
    By ekted in forum AWT / Swing
    Replies: 0
    Last Post: 11-26-2009, 11:24 AM

Posting Permissions

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