Results 1 to 9 of 9

Thread: Java Graphics

  1. #1
    svaidya is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default Java Graphics

    Hi I am trying to get the Java Graphics to work but the paint function is messing up the GUI.
    following is my code:
    Java Code:
    /*
      Sample Program:Displaying a window
      File:Sample.java
      Author:Shweta Gupte
     */
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.lang.*;
      import javax.swing.border.*;
      import java.awt.geom.*;
      import java.applet.*;
      //import java.awt.Graphics;
      
      class Sample extends JFrame implements ActionListener {
    	  private static final int FRAME_WIDTH =700;
    	  private static final int FRAME_HEIGHT=700;
    	  private static final int FRAME_X_ORIGIN=0;
    	  private static final int FRAME_Y_ORIGIN=0;
              private JRadioButton [] radioButton;
    	  private final String CIRCLE="Circle";
    	  private JLabel image1,image2,image3,image4;
    	  private JButton circleBtn,RectBtn,SquareBtn,ovalBtn;
    	  private TitledBorder title;
    	
    	 private int oval=0,rect=0;
    	
    	public static void main ( String [] args){
    	 
    	  Sample frame = new Sample();
    	 frame.setVisible(true);
    	}
    	public Sample(){
    		
    		Container   contentPane;
    		JPanel drawPanel,manipulatePanel,buttonPanel,colorPanel,RadioPanel;
    		
    		String [] btnText= {"Translate","Resize"};
    		//Set Frame properties
    		setTitle("Shape Shifter");
    		setSize( FRAME_WIDTH, FRAME_HEIGHT);
    		setLocation( FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		//changeBkColor();
    		
    		
    		contentPane = getContentPane( );
    		
    		contentPane.setLayout(new BorderLayout());
    		
    		buttonPanel =new JPanel();
    		buttonPanel.setLayout(new FlowLayout());
    		buttonPanel.setBackground(Color.blue);
    		
    		drawPanel = new JPanel();
    		drawPanel.setBorder(BorderFactory.createTitledBorder("Draw"));
    		drawPanel.setBackground(Color.blue);
    		
    		//Fill and Coordinates Panel
    		colorPanel = new JPanel();
    		colorPanel.setBorder(BorderFactory.createRaisedBevelBorder());
    		colorPanel.setLayout(new GridLayout(0,1));
    		JCheckBox fillBtn =new JCheckBox("Fill");
    		fillBtn.setBackground(Color.blue);
    		JCheckBox coordBtn =new JCheckBox("Coordinates");
    		coordBtn.setBackground(Color.blue);
    		colorPanel.add(fillBtn);
    		colorPanel.add(coordBtn);
    		colorPanel.setBackground(Color.blue);
    		
    		
    		manipulatePanel = new JPanel();
    		manipulatePanel.setLayout(new FlowLayout());
    
    		title= BorderFactory.createTitledBorder("Manipulate");
    		title.setTitleJustification(TitledBorder.RIGHT);
    		manipulatePanel.setBorder(title);
    		manipulatePanel.setBackground(Color.blue);
    		RadioPanel =new JPanel();
    		RadioPanel.setLayout(new GridLayout(0,1));
    		RadioPanel.setBorder(BorderFactory.createLoweredBevelBorder());
    		
    		radioButton =new JRadioButton[btnText.length];
    		for (int i=0; i<radioButton.length;i++){
    			radioButton[i]= new JRadioButton(btnText[i]);
    			//radioButton[i].addItemListener(this);
    			radioButton[i].setBackground(Color.blue);
    			RadioPanel.add(radioButton[i]);
    		}
    		manipulatePanel.add(RadioPanel);
    		//Oval button
    		ovalBtn =new JButton();
    		//Setimage gif
    		image3=new JLabel (new ImageIcon( "myOval.gif"));
    		ovalBtn.add(image3);
    		drawPanel.add(ovalBtn);
    		ovalBtn.addActionListener(this);
    		//Circle button
    		circleBtn =new JButton();
    		//Setimage gif
    		image1=new JLabel (new ImageIcon( "myCircle.gif"));
    		circleBtn.add(image1);
    		drawPanel.add(circleBtn);
    		circleBtn.addActionListener(this);
    		
    		//rectangle Button
    		RectBtn =new JButton();
    		//setimage gif
    		image2=new JLabel (new ImageIcon( "myRectangle.gif"));
    		RectBtn.add(image2);
    		drawPanel.add(RectBtn);
    		RectBtn.addActionListener(this);
    		//Square button
    		SquareBtn =new JButton();
    		//Setimage gif
    		image4=new JLabel (new ImageIcon( "mySquare.gif"));
    		SquareBtn.add(image4);
    		drawPanel.add(SquareBtn);
    		SquareBtn.addActionListener(this);
    		
    		buttonPanel.add(drawPanel);
    		buttonPanel.add(colorPanel);
    		buttonPanel.add(manipulatePanel);
    		
    		
    		contentPane.add(buttonPanel,BorderLayout.NORTH);
    		
    		//setDefaultCloseOperation(EXIT_ON_CLOSE);
    		changeBkColor();
    		
      }
    		/*public void paint(Graphics g){
    		//g.setColor(Color.red);
    		if(oval==1){
    		g.drawOval(200,200,220,220);
    		//g.setColor(Color.green);
    		}
    		if(rect==1)
    		g.fillRect(230,230,250,250);
    		
    	}*/
    	
    	public void actionPerformed(ActionEvent event){
    		if(event.getSource() instanceof JButton){
    			
    		 	JButton clickedButton =(JButton) event.getSource();
    			repaint();
    			//System.out.println(clickedButton);
    			if(clickedButton== ovalBtn){
    				oval=1;
    				rect=0;
    				System.out.println("here");
    				
    				clickedButton=null;
    			}
    			if(clickedButton== circleBtn){
    				//draw Circle
    				
    			}
    			if(clickedButton ==RectBtn){
    				oval=0;
    				rect=1;
    				System.out.println("here1");
    				clickedButton=null;
    				//Draw Rect
    			}
    			if(clickedButton== SquareBtn){
    				//Draw Square
    			}
    		}
    		System.out.println("here2");
    		//repaint();
    		
    	}
    	
    	
    	private void changeBkColor(){
    		Container contentPane = getContentPane();
    		contentPane.setBackground(Color.BLACK);
    	}
    	
      }
    I apologize if the code is not very pretty.Also I can send the images for the labels if anyone wants it.
    I have commented out the paint function.but I need it to draw the circle ,oval etc.
    Any alternatives or suggestions appreciated.
    Thanks
    svaidya

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

    Default

    Any alternatives or suggestions appreciated.
    Yes, read the Sun graphics tutorials. They will show you how to do painting in Swing. For one, don't paint on a JFrame and in its paint method but rather on a JPanel in its paintComponent method.

  3. #3
    svaidya is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Made some changes.The following was added in the actionperformed method.I set the background color to the JPanel so the GUI not messing up any more.But the draw still not drawing things.I think I am missing something small.
    Java Code:
    if(clickedButton== ovalBtn){
    				
    				DrawBoard circle= new DrawBoard(200,100);
    				
    				oval=1;
    				rect=0;
    				System.out.println("here");
    				
    				clickedButton=null;
    			}
    My DrawBoard class:
    Java Code:
    class DrawBoard extends JPanel{
    	private int xPosition;
    	private int yPosition;
    	private int width;
    	private int height;
    	public DrawBoard (int x, int y){
    		xPosition = x;
    		yPosition = y;
    		width = 92;
    		height = 92;
    
    	}
    	
                   public void  draw (Graphics2D g2){
    		
    		Ellipse2D.Double ring = new Ellipse2D.Double (xPosition, yPosition, width, height);
                    g2.draw(ring);
    
    		
    		
    	}
    }

  4. #4
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Try to change

    Java Code:
    if(clickedButton== ovalBtn)
    to

    Java Code:
    if(clickedButton.equals("ovalBtn"))

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

    Default

    Quote Originally Posted by ocean View Post
    Try to change

    Java Code:
    if(clickedButton== ovalBtn)
    to

    Java Code:
    if(clickedButton.equals("ovalBtn"))

    Per his first posted code, clickedButton is a JButton, not a String, so I'm not sure if this is going to help.

    I still don't see where the OP is using a JComponent's (such as a JPanel's) paintComponent method anywhere.

  6. #6
    svaidya is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Hi
    I dont think thats the problem.The if statement has a System.out which does print when I click the Oval btn.For some reason the draw function is not getting accessed.i dont know how to draw.

    So about the JComponents PaintComponent where is it to be added in the DrawShapes?
    Thanks
    shwtea

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

    Default

    Quote Originally Posted by svaidya View Post
    So about the JComponents PaintComponent where is it to be added in the DrawShapes?
    shwtea
    Have you reviewed the Sun Swing and graphics tutorials on this subject? That will help you get up and running past this (and other) problems fastest.

  8. #8
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    I better be quiet. :o

  9. #9
    svaidya is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Yes I have been going through the tutorials but they confused me.
    So my basic question is I have my main in one class file and other class file has paintcomponent method etc.I want to call that class:

    Java Code:
    if(clickedButton== ovalBtn){
    				
    				//DrawBoard circle= new DrawBoard(200,100);
    				 circle= new Circle (3,red,30,50);
    				oval=1;
    				rect=0;
    				System.out.println("here");
    				
    				clickedButton=null;
    			}
    Just by typing circle = new Circle( x,color,30,50) if circle is instance of Circle class.The Circle.java code is what I am trying to use.Gives me errors like Circle class not found even if the files are in the same folder.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    public class Circle extends JPanel
    {
       private int diameter, x, y;
       private Color color;
    
       //-----------------------------------------------------------------
       //  Constructor: Sets up this circle with the specified values.
       //-----------------------------------------------------------------
       public Circle (int size, Color shade, int upperX, int upperY)
       {
          diameter = size;
          color = shade;
          x = upperX;
          y = upperY;
       }
    
       //-----------------------------------------------------------------
       //  Draws this circle in the specified graphics context.
       //-----------------------------------------------------------------
       public void draw (Graphics page)
       {
          page.setColor (color);
          page.fillOval (x, y, diameter, diameter);
       }
    
       //-----------------------------------------------------------------
       //  Diameter mutator.
       //-----------------------------------------------------------------
       public void setDiameter (int size)
       {
          diameter = size;
       }
    
       //-----------------------------------------------------------------
       //  Color mutator.
       //-----------------------------------------------------------------
       public void setColor (Color shade)
       {
          color = shade;
       }
    
       //-----------------------------------------------------------------
       //  X mutator.
       //-----------------------------------------------------------------
       public void setX (int upperX)
       {
          x = upperX;
       }
    
    
       //-----------------------------------------------------------------
       //  Y mutator.
       //-----------------------------------------------------------------
       public void setY (int upperY)
       {
          y = upperY;
       }
    
       //-----------------------------------------------------------------
       //  Diameter accessor.
       //-----------------------------------------------------------------
       public int getDiameter ()
       {
          return diameter;
       }
    
       //-----------------------------------------------------------------
       //  Color accessor.
       //-----------------------------------------------------------------
       public Color getColor ()
       {
          return color;
       }
    
       //-----------------------------------------------------------------
       //  X accessor.
       //-----------------------------------------------------------------
       public int getX ()
       {
          return x;
       }
    
       //-----------------------------------------------------------------
       //  Y accessor.
       //-----------------------------------------------------------------
       public int getY ()
       {
          return y;
       }
    }
    If I do circle. whatever method I should be able to draw correct? like circle.draw()?
    I am having some issues with basics

Similar Threads

  1. Java 2d Graphics Drag and Drop
    By shawleigh17 in forum AWT / Swing
    Replies: 9
    Last Post: 07-15-2009, 09:27 PM
  2. Help me with graphics
    By 7oclock in forum New To Java
    Replies: 12
    Last Post: 04-04-2009, 11:20 PM
  3. Want to learn Java Graphics
    By loggen in forum New To Java
    Replies: 7
    Last Post: 01-03-2009, 04:15 PM
  4. Replies: 4
    Last Post: 09-13-2008, 11:07 AM
  5. Problem in printing java graphics
    By Mahendra in forum Java 2D
    Replies: 0
    Last Post: 01-23-2008, 12:45 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
  •