Results 1 to 5 of 5

Thread: drawing in java

  1. #1
    liamo is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default drawing in java

    im trying to draw a random line or swiggle in the white space.i have tryed loads of code but it fails to draw.any ideas?

    Java Code:
    public class mirrordraw extends JFrame implements ActionListener{
    	
    	
    	private Color color=(Color.WHITE);
    
    	
    
    	private JButton hjbutton;
    	private JButton vjbutton;
    	private JButton djbutton;
    	private JButton cjbutton;
    	private JCheckBox cbutton;
    	private JButton rjbutton;
    	private JButton gjbutton;
    	private JButton bjbutton;
    	//JLabel idLabel = new JLabel("Status bar:", JLabel.LEFT);
    	
    	
    	
    
    	JFrame f=new JFrame("mirrordraw");
    	JPanel y=new JPanel();
    	JPanel x=new JPanel();
    	JPanel b=new JPanel();
    	JPanel c=new JPanel();//f
    	//private static canvas Canvas = new canvas();
    	
    	
    		
    	public mirrordraw(){
    		
    		
    		
    		f.getContentPane().setLayout(new GridLayout(1,2));
    		x.setLayout(new GridLayout(6,1));
    		b.setLayout(new GridLayout(2,2));
    		
    		
    		
    		JPanel drawing=new JPanel ();
    		 drawing.setBackground(Color.white);
    		  f.add(drawing, BorderLayout.EAST);
    		   drawing.setPreferredSize(new Dimension(400, 0));
    		    drawing.setBorder(BorderFactory.createLineBorder (Color.blue, 2));
    		    drawing.setBackground(Color.white);
    		    f.add(drawing, BorderLayout.EAST);
    		    f.getGraphics();
    		   
    		   
    
    		 
    		
    		hjbutton=new JButton ("Horizontal ");
    	x.add(hjbutton,BorderLayout.EAST);
    	
    		vjbutton=new JButton ("Vertical ");
    		x.add(vjbutton,BorderLayout.EAST); 
    		
    		djbutton=new JButton ("Diagonal");
    		x.add(djbutton,BorderLayout.EAST);
    		
    		cbutton=new JCheckBox ("crosshairs ");
    		cbutton.setBackground(Color.GRAY);
    		cbutton.setBorder(null);
    		
    		x.add(cbutton,BorderLayout.EAST);
    		
    		rjbutton=new JButton ();
    		rjbutton.setBackground(Color.red);
    		rjbutton.setBorder(null);
    
    		b.add(rjbutton,BorderLayout.WEST);
    		rjbutton.setBorder(null);
    
    		b.add(rjbutton,BorderLayout.WEST);
    			
    	
    	rjbutton.setBorder(null);
    
    	b.add(rjbutton,BorderLayout.WEST);
    		gjbutton=new JButton ();
    		gjbutton.setBackground(Color.green);
    		gjbutton.setBorder(null);
    
    		b.add(gjbutton,BorderLayout.EAST);
    		
    		bjbutton=new JButton ();
    		bjbutton.setBackground(Color.blue);
    		bjbutton.setBorder(null);
    
    		b.add(bjbutton,BorderLayout.WEST);
    		
    		bjbutton=new JButton ();
    		bjbutton.setBackground(Color.black);
    		bjbutton.setBorder(null);
    
    		b.add(bjbutton,BorderLayout.EAST);
    		
    		
    		
    		
    		x.add(b);
    		
    		
    		
    		
    		
    		cjbutton=new JButton ("clear");
    		x.add(cjbutton,BorderLayout.EAST);
    	
    		
    		f.getContentPane().add(x);
    		
    	
    	
    		
    		
    		
    	
    		
    		
    		
    		
    		f.setVisible(true);}
    	
    		
    	
    		
    		 
    		public void actionPerformed(ActionEvent e) {
    			if(e.getSource()==cbutton) getContentPane().getGraphics();
    				getGraphics();
    				cbutton.setVisible(true);
    			
    				
    		
    	}
    		  
    		
    
    		
    		
    		
    		
    				
    		
    	
    	public static void main (String [] args){
    		
    JFrame frame = new mirrordraw();
    	
    	frame.setSize(400,400);
    	frame.setLocationRelativeTo(null); // Center  frame
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	
    	}
    
    
    	public static class canvas extends JPanel{	private int pointCount=0;
    	private Point[] points=new Point[20000];
    	public int x,y;
    	{
    		
    		
    		
    			 
    		
    	addMouseMotionListener(
    			new MouseMotionAdapter()
    			{ public void mouseDragged(MouseEvent event)
    			{
    				if (pointCount<points.length);
    				{points[pointCount]=event.getPoint();
    				++pointCount;
    				repaint();
    				
    				
    			}
    			}
    			}
    			);}
    
    
    	public void paintComponent(Graphics g)
    	{
    	super.paint(g); 
    	for (int i=0;i<pointCount;i++)
    	g.fillOval(points[i].x,points[i].y,4,4);
    
    	}
    	public void mouseMoved(MouseEvent e) 
    	{ 
    	  x=e.getX();
    	  y=e.getY();
    	     repaint();
    
    	} 
    
    	public void mouseDragged(MouseEvent e) 
    	{ x=e.getX();
    	y=e.getY();
    	} 
    
    
    	}
    		
    		
    
    
    	
    
    	
    
    
    	
    		
    	
    	
    
    
    
    		
    		
    	}

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,527
    Blog Entries
    7
    Rep Power
    20

    Default Re: drawing in java

    Don't call the super.paint( ... ) method in line #187 of your code; call super.paintComponent( ... ) instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    liamo is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: drawing in java

    Quote Originally Posted by JosAH View Post
    Don't call the super.paint( ... ) method in line #187 of your code; call super.paintComponent( ... ) instead.

    kind regards,

    Jos
    yep i changed it but still wont draw on the white space when i try it

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: drawing in java

    Maybe you need to revalidate the layout after you've done your painting? I haven't tried your code but that would be something I'd check out

  5. #5
    liamo is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: drawing in java

    Quote Originally Posted by ozzyman View Post
    Maybe you need to revalidate the layout after you've done your painting? I haven't tried your code but that would be something I'd check out
    tryed mate,i have tryed loads of changing of code and stuff it still wont work.my head is done in with it

Similar Threads

  1. drawing in java
    By Ike in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 06:39 PM
  2. Drawing coordinates in Java GUI
    By javausr in forum Java 2D
    Replies: 5
    Last Post: 12-29-2010, 10:49 AM
  3. Drawing strings in Java
    By LyraM in forum Java 2D
    Replies: 4
    Last Post: 05-11-2010, 11:15 PM
  4. drawing shapes in java help
    By alphajoseph in forum Java 2D
    Replies: 2
    Last Post: 09-29-2009, 06:35 PM
  5. Java Drawing PUZZLE
    By Cyorxamp in forum AWT / Swing
    Replies: 3
    Last Post: 06-09-2008, 10:35 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
  •