Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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-23-2008, 03:30 PM
Member
 
Join Date: Aug 2008
Posts: 4
vincent2001@gmail.com is on a distinguished road
how to draw x-y graph in Jpanel.--not in APPLET.
Hi,

I am writing a Destop simple UI --(extend from JFrame) on this JFrame ,there are two JPanel..

on Left JPanel ,there is standard JLabel,JTextField..Where User input x( 30)
on right Jpanel, I wanna draw a X-Y graph based on Y=sin(30)

I never use Graphic2D or Graphic before..it is not hard..what confuses me is that from all the exampls I saw, it seems like the Drawing can only do in APPLET??

The following exampl uses JPanel ( I can understand this part),but eventually the main class extends APPLET? How should i change to fit into my Destop UI which extends JFrame?
Code:
import javax.swing.*; import javax.swing.border.Border; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class RectangleDemo extends JApplet { JLabel label; //Called only when this is run as an applet. public void init() { buildUI(getContentPane()); } void buildUI(Container container) { container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); RectangleArea rectangleArea = new RectangleArea(this); container.add(rectangleArea); label = new JLabel("Click within the framed area."); container.add(label); //Align the left edges of the components. rectangleArea.setAlignmentX(LEFT_ALIGNMENT); label.setAlignmentX(LEFT_ALIGNMENT); //unnecessary, but doesn't hurt } public void updateLabel(Point point) { label.setText("Click occurred at coordinate (" + point.x + ", " + point.y + ")."); } public static void main(String s[]) { JFrame f = new JFrame("RectangleDemo"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); RectangleDemo controller = new RectangleDemo(); controller.buildUI(f.getContentPane()); f.pack(); f.setVisible(true); } } class RectangleArea extends JPanel { Point point = null; RectangleDemo controller; Dimension preferredSize = new Dimension(300,100); int rectWidth = 50; int rectHeight = 50; public RectangleArea(RectangleDemo controller) { this.controller = controller; Border raisedBevel = BorderFactory.createRaisedBevelBorder(); Border loweredBevel = BorderFactory.createLoweredBevelBorder(); Border compound = BorderFactory.createCompoundBorder (raisedBevel, loweredBevel); setBorder(compound); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); if (point == null) { point = new Point(x, y); } else { point.x = x; point.y = y; } repaint(); } }); } public Dimension getPreferredSize() { return preferredSize; } public void paintComponent(Graphics g) { super.paintComponent(g); //paint background //Paint a filled rectangle at user's chosen point. if (point != null) { g.drawRect(point.x, point.y, rectWidth - 1, rectHeight - 1); g.setColor(Color.yellow); g.fillRect(point.x + 1, point.y + 1, rectWidth - 2, rectHeight - 2); controller.updateLabel(point); } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-23-2008, 04:15 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
What happens if you replace JApplet with JPanel and remove the init() method?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-24-2008, 07:01 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import javax.swing.*; import javax.swing.border.Border; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class RectangleDemoRx extends JFrame { JLabel label; public RectangleDemoRx() { buildUI(getContentPane()); } void buildUI(Container container) { container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); RectangleArea rectangleArea = new RectangleArea(this); container.add(rectangleArea); label = new JLabel("Click within the framed area."); container.add(label); //Align the left edges of the components. rectangleArea.setAlignmentX(LEFT_ALIGNMENT); label.setAlignmentX(LEFT_ALIGNMENT); //unnecessary, but doesn't hurt } public void updateLabel(Point point) { label.setText("Click occurred at coordinate (" + point.x + ", " + point.y + ")."); } public static void main(String s[]) { JFrame f = new JFrame("RectangleDemo"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); RectangleDemoRx controller = new RectangleDemoRx(); controller.buildUI(f.getContentPane()); f.pack(); f.setVisible(true); } } class RectangleArea extends JPanel { Point point = null; RectangleDemoRx controller; Dimension preferredSize = new Dimension(300,100); int rectWidth = 50; int rectHeight = 50; public RectangleArea(RectangleDemoRx controller) { this.controller = controller; Border raisedBevel = BorderFactory.createRaisedBevelBorder(); Border loweredBevel = BorderFactory.createLoweredBevelBorder(); Border compound = BorderFactory.createCompoundBorder (raisedBevel, loweredBevel); setBorder(compound); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); if (point == null) { point = new Point(x, y); } else { point.x = x; point.y = y; } repaint(); } }); } public Dimension getPreferredSize() { return preferredSize; } public void paintComponent(Graphics g) { super.paintComponent(g); //paint background //Paint a filled rectangle at user's chosen point. if (point != null) { g.drawRect(point.x, point.y, rectWidth - 1, rectHeight - 1); g.setColor(Color.yellow); g.fillRect(point.x + 1, point.y + 1, rectWidth - 2, rectHeight - 2); controller.updateLabel(point); } } }
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
How to draw in a JPanel using Netbeans 6.1 GUI maker Gatts79 New To Java 2 08-09-2008 05:55 AM
How to draw a bar graph using Jfree sacr83 Java 2D 0 04-30-2008 10:50 AM
How to draw a VERTICAL BAR GRAPH in JAVA sacr83 Java 2D 3 04-30-2008 10:09 AM
Bar Graph Zosden Advanced Java 2 04-28-2008 08:52 AM
Draw on JPanel, Help carl Java 2D 1 07-31-2007 08:56 AM


All times are GMT +3. The time now is 12:19 AM.


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