Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default 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?
    Java 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);
            }
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    What happens if you replace JApplet with JPanel and remove the init() method?

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

    Default

    Java 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);
            }
        }
    }

Similar Threads

  1. Replies: 4
    Last Post: 08-28-2009, 06:50 PM
  2. How to draw a bar graph using Jfree
    By sacr83 in forum Java 2D
    Replies: 0
    Last Post: 04-30-2008, 08:50 AM
  3. How to draw a VERTICAL BAR GRAPH in JAVA
    By sacr83 in forum Java 2D
    Replies: 3
    Last Post: 04-30-2008, 08:09 AM
  4. Bar Graph
    By Zosden in forum Advanced Java
    Replies: 2
    Last Post: 04-28-2008, 06:52 AM
  5. Draw on JPanel, Help
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 06:56 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
  •