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. -
import java.awt.*;
-
import java.applet.*;
-
import javax.swing.*;
-
-
public class Triangle
-
{
-
int a;
-
int b;
-
int c;
-
-
public static int main (String[] args)
-
{
-
-
int perimeter;
-
int sa;
-
String SA;
-
SA = JOptionPane.showInputDialog("Enther the legenth of side A :");
-
sa =Integer.parseInt(SA);
-
-
-
int sb;
-
String SB;
-
SB = JOptionPane.showInputDialog("Enter the legenth of side B: ");
-
sb = Integer.parseInt(SB);
-
-
int sc;
-
String SC;
-
SC = JOptionPane.showInputDialog("Enter the legenth of side C: ");
-
sc = Integer.parseInt(SC);
-
-
perimeter = sa + sb + sc ;
-
-
JOptionPane.showMessageDialog( null,"The Permeter of you triangle is " + perimeter);
-
-
//a= sc;
-
//b= sb;
-
//c=sc;
-
Triangle da = new Triangle();
-
System.exit(0);
-
-
-
-
}
-
-
-
-
-
public static int paint(Graphics canvas)
-
{
-
-
canvas.drawLine( 100,100, 150,sa);
-
canvas.drawLine(100,100,150,sb);
-
canvas.drawLine(100,100,50,sc);
-
-
}
-
-
}
-
what am I doing wrong ...
11 7819 @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
Indeed... but I can't seem to call it. Unless I place null inside ie. Draw.paint(null); -
here is my revised code...
-
import java.awt.Graphics;
-
import javax.swing.*;
-
-
-
-
public class Triangle
-
{
-
-
-
public static void main (String[] args)
-
{
-
Triangle Draw = new Triangle();
-
Draw.paint();
-
}
-
-
public int inputA()
-
{
-
int sa;
-
String SA;
-
SA = JOptionPane.showInputDialog("Enther the legenth of side A :");
-
sa =Integer.parseInt(SA);
-
return sa;
-
}
-
-
public int inputB()
-
{
-
int sb;
-
String SB;
-
SB = JOptionPane.showInputDialog("Enter the legenth of side B: ");
-
sb = Integer.parseInt(SB);
-
return sb;
-
}
-
-
-
public int inputC()
-
{
-
int sc;
-
String SC;
-
SC = JOptionPane.showInputDialog("Enter the legenth of side C: ");
-
sc = Integer.parseInt(SC);
-
return sc;
-
}
-
-
public void paint(Graphics can) {
-
int sa;
-
int sb;
-
int sc;
-
int perimeter;
-
-
Triangle LegenthA = new Triangle();
-
sa = LegenthA.inputA();
-
-
Triangle LegenthB = new Triangle();
-
sb = LegenthB.inputB();
-
-
Triangle LegenthC = new Triangle();
-
sc = LegenthC.inputC() ;
-
-
perimeter = sa + sb + sc ;
-
-
JOptionPane.showMessageDialog( null,"The Permeter of you triangle is " + perimeter);
-
-
can.drawLine( 100,100, 150,sa);
-
can.drawLine(100,100,150,sb);
-
can.drawLine(100,100,50,sc);
-
-
-
}
-
-
}
-
@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
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
@xpun
You must be using AWT components then; better use the Swing JComponents.
kind regards,
Jos
hmm yeah i'm guessing that's why this doesn't run in windows. -
// Triangle.java
-
// Triangle
-
//
-
// Created by John Schintone on 2/6/09.
-
// Copyright (c) 2009 __MyCompanyName__. All rights reserved.
-
-
-
import java.awt.Graphics;
-
import javax.swing.*;
-
import javax.swing.JApplet;
-
import javax.swing.JPanel;
-
import javax.swing.BorderFactory;
-
import java.awt.Color;
-
import java.awt.Dimension;
-
-
-
-
-
public class Triangle
-
{
-
-
public static void main (String[] args)
-
{
-
Triangle Draw = new Triangle();
-
-
SwingUtilities.invokeLater(new Runnable() {
-
public void run() {
-
createAndShowGUI();
-
}
-
});
-
-
}
-
-
private static void createAndShowGUI() {
-
System.out.println("Created GUI on EDT? "+
-
SwingUtilities.isEventDispatchThread());
-
JFrame f = new JFrame("Triangle Visualizer");
-
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
f.add(new Panel());
-
f.pack();
-
f.setSize(600,600);
-
f.setVisible(true);
-
}
-
-
-
-
public int inputA()
-
{
-
int sa;
-
String SA;
-
SA = JOptionPane.showInputDialog("Enter the legenth of side A :");
-
sa = Integer.parseInt(SA);
-
return sa;
-
}
-
-
public int inputB()
-
{
-
int sb;
-
String SB;
-
SB = JOptionPane.showInputDialog("Enter the legenth of side B: ");
-
sb = Integer.parseInt(SB);
-
return sb;
-
}
-
-
-
public int inputC()
-
{
-
int sc;
-
String SC;
-
SC = JOptionPane.showInputDialog("Enter the legenth of side C: ");
-
sc = Integer.parseInt(SC);
-
return sc;
-
-
-
}
-
-
}
-
-
class Panel extends JPanel
-
{
-
-
public Panel() {
-
setBorder(BorderFactory.createLineBorder(Color.black));
-
}
-
-
public Dimension getPreferredSize() {
-
return new Dimension(600,600);
-
}
-
-
public void paint(Graphics can) {
-
// super.paintComponent(can);
-
int sa;
-
int sb;
-
int sc;
-
int perimeter;
-
-
Triangle LegenthA = new Triangle();
-
sa = LegenthA.inputA();
-
-
Triangle LegenthB = new Triangle();
-
sb = LegenthB.inputB();
-
-
Triangle LegenthC = new Triangle();
-
sc = LegenthC.inputC() ;
-
-
perimeter = sa + sb + sc ;
-
-
JOptionPane.showMessageDialog( null,"The Permeter of you triangle is " + perimeter);
-
-
-
can.setColor(Color.blue);
-
can.drawString("Side A:"+sa,10,40);
-
can.drawLine(190,(sa+100),(190+(sc/2)),100); //side a
-
can.setColor(Color.red);
-
can.drawLine((190+sc),(sb+100),(190+(sc/2)),100); //side b
-
can.drawString("Side B:"+sb,10,60);
-
can.setColor(Color.green);
-
can.drawLine(190, (sa+100),sc+190,sb+100);
-
can.drawString("Side C:"+sc,10,80);
-
Color p =new Color (155,0,250);
-
can.setColor(p);
-
can.drawString("Perimeter:" +perimeter,450,550);
-
-
-
}
-
-
}
-
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?
@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.
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.
@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
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
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
|
20 posts
views
Thread by Greg |
last post: by
|
1 post
views
Thread by Michael Rodriguez |
last post: by
|
3 posts
views
Thread by Steve |
last post: by
|
3 posts
views
Thread by Colin McGuire |
last post: by
|
1 post
views
Thread by Rob Richardson |
last post: by
|
3 posts
views
Thread by VB Programmer |
last post: by
| | | | | | | | | | | |