Results 1 to 2 of 2
  1. #1
    Rgfirefly24 is offline Member
    Join Date
    Aug 2007
    Posts
    20
    Rep Power
    0

    Default Help with Drawing a line

    I am trying to create an application that divides the client area of the screen into 4 equally sized canvases. EAch canvas should have a different color. I must create at least one of the colors. Also for every two mouse clicks in a particular canvas, that canvas should draw a line between the two mouse click points.

    here are my .java files.

    Lab6Draw.java=

    Java Code:
    import javax.swing.*;
    import java.lang.*;
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    
    public class Lab6Draw{
    	public static void main(String[] args) throws Exception{
    		Grid frame = new Grid();
    		frame.setTitle("Lab6 - Application #1");
    		frame.setSize(400,300);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    
    
    
    	}
    }
    
    Grid.java = 
    import javax.swing.*;
    import java.lang.*;
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.Color.*;
    
    public class Grid extends JFrame{
    	 public Grid(){
    		JPanel p1 = new JPanel(new FlowLayout());
    		Color color = new Color(50,100,75);
    		p1.setBackground(color);
    		p1.setForeground(color);
    		p1.add(new newPanel());
    
    
    		JPanel p2 = new JPanel(new FlowLayout());
    		p2.setBackground(Color.red);
    		p2.setForeground(Color.blue);
    
    		JPanel p3 = new JPanel(new FlowLayout());
    		p3.setBackground(Color.blue);
    		p3.setForeground(Color.red);
    
    		JPanel p4 = new JPanel(new FlowLayout());
    		p4.setBackground(Color.green);
    		p4.setForeground(Color.yellow);
    
    		JPanel p5 = new JPanel();
    		p5.setLayout(new GridLayout(2,2,1,1));
    		p5.add(p1);
    		p5.add(p2);
    		p5.add(p3);
    		p5.add(p4);
    
    		this.add(p5);
    
    
    
    	}
    }
    
    Drawing.java = 
    import javax.swing.*;
    import java.lang.*;
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.Color.*;
    
    class Drawing extends JFrame {
    	public Drawing(){
    		add(new newPanel());
    	}
    }
    
    newPanel.java = 
    
    import javax.swing.*;
    import java.lang.*;
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Graphics;
    
    class newPanel extends JPanel
    	implements MouseListener{
    		public int x1= -1;
    		public int y1= -1;
    		public int x2= -1;
    		public int y2= -1;
    
    		public newPanel(){
    		 // Register listener for the mouse event
    		    addMouseListener(this);
    		    }
    
    		  public void mouseClicked(MouseEvent e) {
    		  }
    
    		  public void mouseEntered(MouseEvent e) {
    		  }
    
    		  public void mouseExited(MouseEvent e) {
    		  }
    
    		  public void mouseReleased(MouseEvent e) {
    		  }
    
    		  public void mousePressed(MouseEvent e) {
    			if (x1 == -1){
    			   	x1 = e.getX();
    			   	y1 = e.getY();
    			}
    			else{
    				x2 = e.getX();
    				y2 = e.getY();
    			}
    		  }
    
      	protected void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.setColor(Color.black);
    		g.drawLine(x1,y1,x2,y2);
    	}
    }
    Last edited by levent; 08-05-2007 at 06:02 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class L6D{
    	public static void main(String[] args) throws Exception{
    		Grid frame = new Grid();
    		frame.setTitle("Lab6 - Application #1");
    		frame.setSize(400,300);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    	}
    }
    
    class Grid extends JFrame{
    	public Grid(){
    		Color[] bgColors = {
    			new Color(50,100,75), Color.red,
    			Color.green.darker(), Color.blue
    		};
    		Color[] fgColors = {
    			Color.orange, Color.blue,
    			Color.yellow, Color.red
    		};
    		JPanel panel = new JPanel();
    		panel.setLayout(new GridLayout(2,2,1,1));
    		for(int j = 0; j < bgColors.length; j++) {
    			panel.add(new NewPanel(bgColors[j], fgColors[j]));
    		}
    
    		this.add(panel);
    	}
    }
    
    class NewPanel extends JPanel implements MouseListener{
    	Color color;
    	public int x1= 0;
    	public int y1= 0;
    	public int x2= -1;
    	public int y2= -1;
    
    	public NewPanel(Color bg, Color fg){
    		setBackground(bg);
    		color = fg;
    		// Register listener for the mouse event
    		addMouseListener(this);
    	}
    
    	public void mouseClicked(MouseEvent e) {}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {}
    
    	public void mousePressed(MouseEvent e) {
    		if (x1 != x2 || y1 != y2){
    		   	x1 = e.getX();
    		   	y1 = e.getY();
    			x2 = x1;
    			y2 = y1;
    		} else {
    			x2 = e.getX();
    			y2 = e.getY();
    		}
    		repaint();
    	}
    
      	protected void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.setColor(color);
    		g.drawLine(x1,y1,x2,y2);
    	}
    }

Similar Threads

  1. Drawing on aJPanel
    By Djangolo in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 01:01 AM
  2. New: Want to understand Drawing...
    By diRisig in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 08:13 AM
  3. Help with drawing strings!
    By JavaInLove in forum AWT / Swing
    Replies: 1
    Last Post: 02-05-2008, 03:39 AM
  4. drawing window
    By BlitzA in forum Advanced Java
    Replies: 0
    Last Post: 12-30-2007, 05:39 PM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 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
  •