472,110 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

get input and draw the shape

xpun
39
Im trying to use data form input boxes ( integers) and draw a triangle with the numbers received. I can get the numbers fine but I can seem to grab them form the static class.
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.applet.*;
  3. import javax.swing.*;
  4.  
  5. public  class Triangle 
  6.  {
  7.  int a;
  8.  int b;
  9.  int c;
  10.  
  11.      public static int main (String[] args)
  12.         {
  13.  
  14.              int perimeter;
  15.          int sa;
  16.         String SA;
  17.         SA = JOptionPane.showInputDialog("Enther the legenth of side A :");
  18.         sa =Integer.parseInt(SA);
  19.  
  20.  
  21.         int sb;
  22.         String SB;
  23.         SB = JOptionPane.showInputDialog("Enter the legenth of side B: ");
  24.         sb = Integer.parseInt(SB);
  25.  
  26.         int sc;
  27.         String SC;
  28.         SC = JOptionPane.showInputDialog("Enter the legenth of side C: ");
  29.         sc = Integer.parseInt(SC);
  30.  
  31.         perimeter = sa + sb + sc ;
  32.  
  33.         JOptionPane.showMessageDialog( null,"The Permeter of you triangle is " + perimeter);
  34.  
  35.         //a= sc;
  36.         //b= sb;
  37.         //c=sc;
  38.         Triangle da = new Triangle();
  39.         System.exit(0);
  40.  
  41.  
  42.  
  43.         }
  44.  
  45.  
  46.  
  47.  
  48.     public static int paint(Graphics canvas)
  49.         {
  50.  
  51.         canvas.drawLine( 100,100, 150,sa);
  52.         canvas.drawLine(100,100,150,sb);
  53.         canvas.drawLine(100,100,50,sc);
  54.  
  55.         }
  56.  
  57.         }
  58.  
what am I doing wrong ...
Feb 7 '09 #1
11 7819
JosAH
11,448 Expert 8TB
@xpun
Who, or what, is supposed to call your static method paint() in your Triangle class? There is no magic involved so your static method isn't called at all. Read a Swing tutorial and make your triangle class extend (or delegate to) a JComponent with a non-static paintComponent() method. The AWT event dispatch thread calls those methods.

kind regards,

Jos
Feb 7 '09 #2
xpun
39
Indeed... but I can't seem to call it. Unless I place null inside ie. Draw.paint(null);
Expand|Select|Wrap|Line Numbers
  1. here is my revised code...
  2. import java.awt.Graphics;
  3. import javax.swing.*;
  4.  
  5.  
  6.  
  7. public  class Triangle
  8.  {
  9.  
  10.  
  11.      public static void main (String[] args)
  12.         {
  13.         Triangle Draw = new Triangle();
  14.          Draw.paint();
  15.         }
  16.  
  17.                public int inputA()
  18.              {
  19.               int sa;
  20.               String SA;
  21.             SA = JOptionPane.showInputDialog("Enther the legenth of side A :");
  22.             sa =Integer.parseInt(SA);
  23.             return sa;
  24.             }
  25.  
  26.             public int inputB()
  27.             {
  28.             int sb;
  29.              String SB;
  30.             SB = JOptionPane.showInputDialog("Enter the legenth of side B: ");
  31.             sb = Integer.parseInt(SB);
  32.             return sb;
  33.             }      
  34.  
  35.  
  36.             public int inputC()
  37.             {
  38.             int sc;
  39.             String SC;
  40.             SC = JOptionPane.showInputDialog("Enter the legenth of side C: ");
  41.             sc = Integer.parseInt(SC);
  42.             return sc;
  43.             }
  44.  
  45.     public void paint(Graphics can) {
  46.         int sa;
  47.         int sb;
  48.         int sc;
  49.         int perimeter;
  50.  
  51.         Triangle LegenthA = new Triangle();
  52.         sa = LegenthA.inputA();
  53.  
  54.         Triangle LegenthB = new Triangle();
  55.         sb = LegenthB.inputB();
  56.  
  57.         Triangle LegenthC = new Triangle();
  58.         sc = LegenthC.inputC() ;
  59.  
  60.         perimeter =  sa + sb + sc ;
  61.  
  62.         JOptionPane.showMessageDialog( null,"The Permeter of you triangle is " + perimeter);
  63.  
  64.           can.drawLine( 100,100, 150,sa);
  65.           can.drawLine(100,100,150,sb);
  66.           can.drawLine(100,100,50,sc);
  67.  
  68.  
  69.      }
  70.  
  71.     } 
  72.  
Feb 9 '09 #3
JosAH
11,448 Expert 8TB
@xpun
That just keeps the compiler's mouth shut but most certainly doesn't work; as I wrote: read a Swing tutorial an see how the drawing mechanism works, i.e. the (small) parts you have to implement and the parts the Swing framework does for you. Just guessing that some paint() method should do the job doesn't work.

kind regards,

Jos
Feb 9 '09 #4
xpun
39
Thanks for the help Jos. I managed to find a tutorial I could understand, and got this working quite nicely. It still uses the regular paint() method.

Thanks again.

John Schintone
Feb 9 '09 #5
JosAH
11,448 Expert 8TB
@xpun
You must be using AWT components then; better use the Swing JComponents.

kind regards,

Jos
Feb 9 '09 #6
xpun
39
hmm yeah i'm guessing that's why this doesn't run in windows.

Expand|Select|Wrap|Line Numbers
  1. //  Triangle.java
  2. //  Triangle
  3. //
  4. //  Created by John Schintone on 2/6/09.
  5. //  Copyright (c) 2009 __MyCompanyName__. All rights reserved.
  6.  
  7.  
  8. import java.awt.Graphics;
  9. import javax.swing.*;
  10. import javax.swing.JApplet;
  11. import javax.swing.JPanel;
  12. import javax.swing.BorderFactory;
  13. import java.awt.Color;
  14. import java.awt.Dimension;
  15.  
  16.  
  17.  
  18.  
  19. public  class Triangle 
  20.  {
  21.  
  22.      public static void main (String[] args)
  23.         {
  24.         Triangle Draw = new Triangle();
  25.  
  26.          SwingUtilities.invokeLater(new Runnable() {
  27.             public void run() {
  28.                 createAndShowGUI();
  29.             }
  30.         });
  31.  
  32.         }
  33.  
  34.         private static void createAndShowGUI() {
  35.         System.out.println("Created GUI on EDT? "+
  36.                 SwingUtilities.isEventDispatchThread());
  37.         JFrame f = new JFrame("Triangle Visualizer");
  38.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.         f.add(new Panel());
  40.         f.pack();
  41.         f.setSize(600,600);
  42.         f.setVisible(true);
  43.          }        
  44.  
  45.  
  46.  
  47.                public int inputA()
  48.              {
  49.               int sa;
  50.               String SA;
  51.             SA = JOptionPane.showInputDialog("Enter the legenth of side A :");
  52.             sa = Integer.parseInt(SA);
  53.             return sa;
  54.             }
  55.  
  56.             public int inputB()
  57.             {
  58.             int sb;
  59.              String SB;
  60.             SB = JOptionPane.showInputDialog("Enter the legenth of side B: ");
  61.             sb = Integer.parseInt(SB);
  62.             return sb;
  63.             }      
  64.  
  65.  
  66.             public int inputC()
  67.             {
  68.             int sc;
  69.             String SC;
  70.             SC = JOptionPane.showInputDialog("Enter the legenth of side C: ");
  71.             sc = Integer.parseInt(SC);
  72.             return sc;
  73.  
  74.  
  75.             }
  76.  
  77.             }                            
  78.  
  79.      class Panel extends JPanel 
  80.      {
  81.  
  82.       public  Panel() {
  83.         setBorder(BorderFactory.createLineBorder(Color.black));
  84.             }
  85.  
  86.          public Dimension getPreferredSize() {
  87.                return new Dimension(600,600);
  88.             }
  89.  
  90.            public void paint(Graphics can) {
  91.              // super.paintComponent(can);  
  92.               int sa;
  93.         int sb;
  94.         int sc;
  95.         int perimeter;
  96.  
  97.         Triangle LegenthA = new Triangle();
  98.         sa = LegenthA.inputA();
  99.  
  100.         Triangle LegenthB = new Triangle();
  101.         sb = LegenthB.inputB();
  102.  
  103.         Triangle LegenthC = new Triangle();
  104.         sc = LegenthC.inputC() ;
  105.  
  106.         perimeter =  sa + sb + sc ;
  107.  
  108.         JOptionPane.showMessageDialog( null,"The Permeter of you triangle is " + perimeter);
  109.  
  110.  
  111.         can.setColor(Color.blue);
  112.         can.drawString("Side A:"+sa,10,40);
  113.                 can.drawLine(190,(sa+100),(190+(sc/2)),100); //side a
  114.         can.setColor(Color.red);
  115.                 can.drawLine((190+sc),(sb+100),(190+(sc/2)),100); //side b
  116.                 can.drawString("Side B:"+sb,10,60);    
  117.         can.setColor(Color.green);
  118.                 can.drawLine(190, (sa+100),sc+190,sb+100);
  119.                 can.drawString("Side C:"+sc,10,80);
  120.         Color p =new Color (155,0,250);
  121.         can.setColor(p);        
  122.                 can.drawString("Perimeter:" +perimeter,450,550);
  123.  
  124.  
  125.            }  
  126.  
  127.                 }
  128.  
Feb 10 '09 #7
xpun
39
Can't seem to figure out why, but on OS X this runs fine. when I run it in windows (xp) the windows don't have a background, I'm just getting the outline of the input boxes and the drawing area. ie it is all transparent. Any ideas?
Feb 10 '09 #8
JosAH
11,448 Expert 8TB
@xpun
You are supposed to override the paintComponent() method, not the paint() method (also see my reply #2).

kind regards,

Jos

ps. and please read a Swing tutorial; it explains how the actual painting mechanism works. Don't guess.
Feb 11 '09 #9
xpun
39
Sorry about that Jos...

That was the wrong batch of code. I did override the paintComponent() method...

I'll read a swing tutorial before I post again ... I've learned my lesson. Tutorials tend to piss me off some times because they take to long to get to the point.
Feb 11 '09 #10
JosAH
11,448 Expert 8TB
@xpun
No need to apologize; check the very first article in this section; the "Read This First" article has a couple of useful links and check Sun's Java site. btw, the Swing tutorials has quite some fun and interesting sections; read them, they're a good read (not boring and they get to the point quite fast).

kind regards,

Jos
Feb 11 '09 #11
xpun
39
OK I finally got it working. I still wounder why os x let it run..

Thanks for all the help Jos.
I'll post the final code when I'm done with class.


John Schintone
Feb 11 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by maidoo | last post: by
2 posts views Thread by Starlite | last post: by
1 post views Thread by Michael Rodriguez | last post: by
3 posts views Thread by Steve | last post: by
1 post views Thread by Rob Richardson | last post: by
3 posts views Thread by VB Programmer | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.