473,839 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
+ 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 8100
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

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

Similar topics

0
1072
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 an event happens (eg. button pressed). -- regards Maidoo.
2
8422
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 shape with a semi-transparent color i.e. we should still be able to see whatever is beneath the shape. Can anyone help me? Thanks. void OnPaint() { CPaintDC dc(this); CPen pPen(PS_SOLID, 1, RGB(0,0,0));
20
2410
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 used the autoshapes to create this diagram). I was wondering what the best way to incorporate this into the DB. I have read about how saving them as pictures in the DB bloats the size, which is unacceptable, but it needs to be easy to do (not...
1
3893
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 line across the screen? TIA, Mike Rodriguez
3
15980
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. Thanks, Steve
3
10335
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 shapeType Case 1 : g.DrawRectangle(New Pen(Color.Black), 0, 0, 50, 10) Case 2 'draw a circle Case 3 'draw a triangle Case 4 'draw other shape Case 5 'draw other shape
1
3440
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 label, so I want to draw a rectangle and lines on my form. I don't want them to have any user interaction whatever. They would be there just to look pretty. There's plenty of support for drawing rectangles from code. There seems to be support...
3
4711
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
1047
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 panel, so I cant see a part of the curve which goes though the panel. I want to see the whole curve. Any help? Thanx in advance.
0
9856
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10913
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10592
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10656
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10299
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7834
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4067
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.