Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-05-2007, 06:38 PM
Member
 
Join Date: Aug 2007
Posts: 20
Rgfirefly24 is on a distinguished road
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=

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 07:02 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-06-2007, 09:40 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Drawing on aJPanel Djangolo AWT / Swing 1 02-17-2008 02:01 AM
New: Want to understand Drawing... diRisig New To Java 1 02-05-2008 09:13 AM
Help with drawing strings! JavaInLove AWT / Swing 1 02-05-2008 04:39 AM
drawing window BlitzA Advanced Java 0 12-30-2007 06:39 PM
Reading in data from file line by line bluekswing New To Java 1 10-02-2007 01:19 AM


All times are GMT +3. The time now is 02:55 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org