Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2011
    Posts
    24
    Rep Power
    0

    Default JPanel and Oval/Ellipse Problems

    Ok so I'm trying to draw a Oval or Ellipse, but I'm having issues, either the complier doesn't like Ellipses because its regular graphics or ovals won't work because the method where I add it to an ArrayList doesn't have a graphics modifier.

    Code 1: (JPanel class where all the drawings happen)
    Java Code:
    import javax.swing.JComboBox;
    import java.awt.geom.Ellipse2D;
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.BorderFactory;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics; 
    import java.awt.Graphics2D;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseAdapter;
    import java.util.ArrayList;
    
    public class Scribble extends JPanel 
    {
     
        public static void main(String[] args) 
        {
            SwingUtilities.invokeLater(new Runnable() 
            {
                public void run() 
                {
                    createAndShowGUI(); 
                }
            }
            );
        }
    
        private static void createAndShowGUI() 
        {
            ArrayList pointList = new ArrayList();
            ArrayList colorList = new ArrayList();
            ArrayList eraserList = new ArrayList();
        	MyPanel myPanel1 = new MyPanel(pointList, colorList, eraserList); 
            ColorBox f = new ColorBox(myPanel1); 
            f.add(myPanel1);
       
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            f.pack();
            f.setVisible(true);
        } 
        private ColorBox f;
        private JComboBox facenameCombo;
        private String facename;
        private ActionListener listener;
        private static final int FRAME_WIDTH = 600;
        private static final int FRAME_HEIGHT=800;
       
        private Graphics2D g2;
        private JPanel panel;
        public Color color;
    }
    
    class MyPanel extends JPanel {
    
        private int last_x;
        private int last_y;
        private int x;
        private int y;
        private Graphics g;
    	    
        public MyPanel(ArrayList pointList, ArrayList colorList, ArrayList eraserList) 
        {
        	
            setBorder(BorderFactory.createLineBorder(Color.black));
            this.pointList = pointList;
            this.colorList = colorList;
            this.eraserList = eraserList;
            defaultColor = new Color(238, 238, 238);
         
            
            addMouseListener(new MouseAdapter() 
            {
                public void mousePressed(MouseEvent e) 
                {
                    last_x = e.getX();
                    last_y = e.getY();
                   
                }
            }
            );
    
            addMouseMotionListener(new MouseAdapter() 
            {
                public void mouseDragged(MouseEvent e) 
                {
                    x = e.getX();
                    y = e.getY();
                    repaint();
                }
            }
            );
          
        }
       public void Clear()
       {
    	   pointList.clear();
    	   colorList.clear();
    	   ColorBox.setBackgroundDefault();
    	   setBackground(defaultColor);
    	   repaint();
       }
        public Dimension getPreferredSize() 
        {
            return new Dimension(250,200);
        }
        public Color setColor1()
        {
        	Color color = ColorBox.getColor();
        	return color;
        }
        public void setBackgroundColor()
        { 
        	Color a = ColorBox.getBackground1();
        	if (a==Color.darkGray)
        	{
        		setBackground(defaultColor);
        	}
        	else if (a!=Color.darkGray)
        	{
        		setBackground(a);
        	}
        }
        
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
        	int k = 0;
        	int j = 0;
        	String eraserString = ColorBox.setEraser();
        	if (eraserString.equals("Is Not Selected"))
        	{	
        		pointList.add(last_x);
        		pointList.add(last_y);	
            	pointList.add(x);	
            	pointList.add(y);
            	colorList.add(setColor1());
        	}
        	else if (eraserString.equals("Is Selected"))
        	{
        		Ellipse2D e = new Ellipse2D.Double(last_x,last_y,30,30);
        		eraserList.add(e);
        	}
            setBackgroundColor();
            g.drawString("This is my custom Panel!",10,20);
            int size = (int) pointList.size();
            while (k<size)
           {
                 int point1 = (pointList.get(k));
                 int point2 = ( pointList.get(k+1));
                 int point3 = (pointList.get(k+2));
                 int point4 = (pointList.get(k+3));
                 g.setColor(colorList.get(j));
                 g.drawLine(point1, point2, point3, point4);
                 g.setColor(defaultColor);
                 g.fill();
                 g.drawEllipse(eraserList.get(j));
                 last_x = x;
                 last_y = y;
                 k = k+4;
                 j = j+1;
            }
        }
        private ArrayList<Integer> pointList;
        private ArrayList<Color> colorList;
        private ArrayList<Ellipse2D> eraserList;
    	private Color defaultColor;
    	private ColorBox colorBox;
    }
    Code 2: (JFrame class where Buttons are created)
    Java Code:
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JComboBox;	
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.TitledBorder;
    
    import java.awt.Color;
    import java.awt.GridLayout;
    	
    public class ColorBox extends JFrame
    {
      
    	private MyPanel myPanel;
    	public ColorBox(MyPanel myPanel) 
          {
             this.myPanel = myPanel;  
             color = Color.black;
             background = Color.darkGray;
    
    		class ChoiceListener implements ActionListener 
    		{
    			public void actionPerformed (ActionEvent event) 
    			{
    				JComboBox cb = (JComboBox) event.getSource();
    				Object newItem = cb.getSelectedItem();
    				setColor((String) newItem);
    				
    				
    			}
    		
    		}
    		class ChoiceListener2 implements ActionListener
    		{
    			public void actionPerformed (ActionEvent event)
    			{
    				JComboBox cb = (JComboBox) event.getSource();
    				Object newItem = cb.getSelectedItem();
    				setBackground((String) newItem);
    			}
    		}	
    
    			listener = new ChoiceListener();
    			listener2 = new ChoiceListener2();
    			setLayout(new GridLayout(2,2));
    			createControlPanel();
    			setSize(FRAME_WIDTH, FRAME_HEIGHT);
    		}
    		
    		public void createControlPanel()
    		{
    			JPanel facenamePanel = createComboBox();
    			JPanel clearPanel = createClearBox();
    			JPanel facenamePanel1 = createComboBox2();
    			JPanel EraserPanel = createRadioButtons();
    			
    			JPanel controlPanel = new JPanel();
    			controlPanel.setLayout(new GridLayout(3,1));
    			controlPanel.add(facenamePanel);
    			controlPanel.add(clearPanel);
    			controlPanel.add(facenamePanel1);
    			controlPanel.add(EraserPanel);
    			add(controlPanel);		
    		}
    		
    		public JPanel createComboBox()
    		{
    			facenameCombo = new JComboBox();
    			facenameCombo.addItem("Black");
    			facenameCombo.addItem("Blue");
    			facenameCombo.addItem("Red");
    			facenameCombo.addItem("Green");
    			facenameCombo.addItem("Orange");
    			facenameCombo.addItem("Pink");
    			facenameCombo.addItem("Yellow");
    			facenameCombo.setEditable(true);
    			facenameCombo.addActionListener(listener);
    			
    			
    			JPanel panel = new JPanel();
    			panel.add(facenameCombo);
    			return panel;
    				
    		}
    		public JPanel createComboBox2()
    		{
    			facenameCombo1 = new JComboBox();
    			facenameCombo1.addItem("Default");
    			facenameCombo1.addItem("Black");
    			facenameCombo1.addItem("Blue");
    			facenameCombo1.addItem("Red");
    			facenameCombo1.addItem("Green");
    			facenameCombo1.addItem("Orange");
    			facenameCombo1.addItem("Pink");
    			facenameCombo1.addItem("Yellow");
    			facenameCombo1.setEditable(true);
    			facenameCombo1.addActionListener(listener2);
    			
    			JPanel panel = new JPanel();
    			panel.add(facenameCombo1);
    			return panel;
    		}
    		public JPanel createRadioButtons()
    		{
    			eraserButton = new JRadioButton("Eraser");
    			eraserButton.addActionListener(listener);
    			
    			noeraserButton = new JRadioButton("Blue");
    			noeraserButton.addActionListener(listener);
    			
    			ButtonGroup group = new ButtonGroup();
    			group.add(eraserButton);
    			group.add(noeraserButton);
    			
    			JPanel panel = new JPanel();
    			panel.add(eraserButton);
    			panel.add(noeraserButton);
    			panel.setBorder(new TitledBorder(new EtchedBorder(), "Color"));
    			
    			return panel;
    		}	
    		public void setColor(String newItem)
    		{		
    			if (newItem.equals("Blue")) color = (Color.BLUE);
    				
    			else if(newItem.equals("Black")) color = (Color.BLACK);
    				
    			else if (newItem.equals("Red")) color = (Color.RED);
    				
    			else if (newItem.equals("Green")) color = (Color.GREEN);
    				
    			else if (newItem.equals("Orange")) color = (Color.ORANGE);
    				
    			else if (newItem.equals("Pink")) color = (Color.PINK);
    				
    			else if (newItem.equals("Yellow")) color = (Color.YELLOW);
    	
    		
    			}	
    		public static Color getColor() 
    		{
    			
    			return color;
    		}
    		public void setBackground(String newItem)
    		{		
    			if (newItem.equals("Default")) background = (Color.darkGray);
    			
    			else if (newItem.equals("Blue")) background = (Color.BLUE);
    				
    			else if(newItem.equals("Black")) background = (Color.BLACK);
    				
    			else if (newItem.equals("Red")) background = (Color.RED);
    				
    			else if (newItem.equals("Green")) background = (Color.GREEN);
    				
    			else if (newItem.equals("Orange")) background = (Color.ORANGE);
    				
    			else if (newItem.equals("Pink")) background = (Color.PINK);
    				
    			else if (newItem.equals("Yellow")) background = (Color.YELLOW);
    			}	
    		public static  Color getBackground1() 
    		{
    			
    			return background;
    		}
    		
    	      public JPanel createClearBox()
    	      {
    	         clearButton = new JButton("Clear");
    	         clearButton.addActionListener(new ActionListener() {
    	         
    	                  public void actionPerformed(ActionEvent e)
    	                  {
    	                  myPanel.Clear();
    	                  
    	                  }
    	              });      
    	         JPanel panel = new JPanel();
    	         panel.add(clearButton);
    	         return panel;
    	         
    	      }
    	      public static void setBackgroundDefault()
    	      {
    	    	  facenameCombo1.setSelectedItem("Default");
    	      }
    	      public static String setEraser()
    	      {
    	    	  String eraser = "default";
    	    	  if (eraserButton.isSelected()) eraser = "Is Selected";
    	    	  
    	    	  else if (noeraserButton.isSelected()) eraser = "Is Not Selected";
    	    	  return eraser;
    	      }
    	   
    		private JComboBox facenameCombo;
    		private JButton clearButton;
    		private static JComboBox facenameCombo1;
    		private ActionListener listener;
    		private ActionListener listener2;
    		private static final int FRAME_WIDTH = 600;
    		private static final int FRAME_HEIGHT=800;
    		public static Color color;
    		public static Color background;
    		private MyPanel mypanel;
    		private static JRadioButton eraserButton;
    		private static JRadioButton noeraserButton;
    }
    Is there any way in the paintComponent method to draw Oval or Ellipses? I tried making the input for the method Graphics2D, but that didn't work either, any suggestions are appreciated.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Is there any way in the paintComponent method to draw Oval or Ellipses?
    Yes. 95% of the code you posted has nothing to do with your question. So create a simple frame with a JPanel. Override the paintComponent() method to draw the oval. This is called a Short, Self Contained, Correct Example. Then once you understand the basics you can start adding all the other buttons for colors and so on.

    If you can't get the SSCCE to work, then you have some simple code to post. We don't have time to read through 100's of lines of code.

Similar Threads

  1. Problems with JTable / JPanel
    By mbutler755 in forum AWT / Swing
    Replies: 19
    Last Post: 09-30-2010, 06:22 PM
  2. Clicking on an oval in JPanel
    By chielt in forum Java 2D
    Replies: 6
    Last Post: 03-17-2010, 12:08 AM
  3. problems with JPanel and JFrame
    By v1nsai in forum New To Java
    Replies: 13
    Last Post: 04-08-2009, 07:49 PM
  4. JPanel / layout problems
    By Warhorsei in forum AWT / Swing
    Replies: 4
    Last Post: 06-04-2008, 05:26 AM
  5. JPanel Problems
    By Riftwalker in forum AWT / Swing
    Replies: 6
    Last Post: 10-15-2007, 11:16 PM

Posting Permissions

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