473,466 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

get input and draw the shape

xpun
39 New Member
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 8060
JosAH
11,448 Recognized Expert MVP
@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 New Member
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 Recognized Expert MVP
@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 New Member
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 Recognized Expert MVP
@xpun
You must be using AWT components then; better use the Swing JComponents.

kind regards,

Jos
Feb 9 '09 #6
xpun
39 New Member
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 New Member
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 Recognized Expert MVP
@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 New Member
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 Recognized Expert MVP
@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 New Member
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

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

Similar topics

0
by: maidoo | last post by:
here is the situation im developing an asp.net application using vb.net through vs2003 i want to draw a shape of connected circles upon a database retreival result. or i want to draw a circle when...
2
by: Starlite | last post by:
I am using Microsoft visual c++ 6, and I want to draw a shape (say a rectangle) on a view. The code that I have used is below, but this draws a solid -filled rectangle, what I want is a displayed...
20
by: Greg | last post by:
I'm fairly new to access (using 2002) and am setting up a DB for work. along with each record the user also needs to make a flow diagram (previously, these reports were composed in word and they...
1
by: Michael Rodriguez | last post by:
In Delphi there was a control called a Bevel. It would let you draw straight lines, top, left, right, or bottom, anywhere on your screen. What is the equivalent in C#? How can I draw a straight...
3
by: Steve | last post by:
Hi All, I am sure this is a simple question, but how do I draw a line onto a windows form in VB.NET, or any shape for that matter. VB6 had a line and shape control that you could use. ...
3
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case...
1
by: Rob Richardson | last post by:
Greetings! I am creating a form that will contain information that will eventually be on a label. The label has a 2-column table with lines separating the cells. I want my form to resemble the...
3
by: VB Programmer | last post by:
VB.NET 2005 WinForm App: How do I draw a black box or another shape on the form I'm designing? I want to do it manually, not thru code. Is there a way or a 3rd party add-on?
0
by: daves | last post by:
How to draw a shape from one panel to an other in GDI+?? I want to draw a curve, where the beginning point is in the form and the end point is in a panel. By default the curve is drawn below the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.