Thread: Graphics

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2

    Graphics


    Hello . I want to write checkers as my 1st project in java. But i have a problem with drawing of a simple cell. What am i doing wrong ?
    Code:
    public class view extends JFrame{
    
    	static JToolBar tb1  = new JToolBar();
    	static JButton newgb = new JButton ("New Game");
    	static JButton optb = new JButton ("Save");
    	static JButton flvlb = new JButton ("Load");
    	static JButton aboutb = new JButton ("About");
    	static Canvas field;
    	static Graphics gr;
    
    	
    	public view(String s){	
    		super(s);
    		tb1.setLayout(new GridLayout());
    		tb1.setFloatable(false);
    		tb1.add(newgb);
    		tb1.add(optb);
    		tb1.add(flvlb);
    		tb1.add(aboutb);
    		setLayout(new BorderLayout());
    		add(tb1, BorderLayout.PAGE_START);
    		setSize(400, 400);
    		setResizable(false);
    		setVisible(true);
    		
    	}
    	
    	public void drawfield(){
    				
    				gr = getGraphics();
    				gr.setColor(Color.red);
    				gr.drawRect(0, 0, 50, 50);
    				field.paint(gr);
    				add(field);
    
    		
    	}
    	
    }
    and this is my main where i'm calling drawfied .
    Code:
    class root {
    
    	static view form = new view("Checkers");
    	
    	public static void main(String[] args){
    	
    		form.drawfield();
    		
    	}
    	
    	
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    i have a problem with drawing of a simple cell
    Can you explain what the problem is and describe what you want the program to do?

    See the tutorial for how to draw:http://docs.oracle.com/javase/tutori...ing/index.html
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0
    Hi
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    88
    Rep Power
    2
    The program must draw a red rectangle 50x50.
    The main and the nly problem is that nothing were drawn . I think it's in my adding of canvas on form or in it's usage . Please guide me .
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    The tutorial at the link I posted shows how to do custom drawing.
    Basically, define a class that extends a JPanel, override its paintComponent() method and do the drawing there. Add an instance of the new class to a GUI container like a JFrame that will show it.

IMN logo majestic logo threadwatch logo seochat tools logo