Astir
Legendary Skial King
I have been struggling on this issue for quite some time, and I can't seem to find the answer on the internet or stack overflow.
I am meant to, for an assignment at university, create an interactive GUI that allows the user to select one of the basic forms, draw it with the desired dimensions and get the drawing and information in response.
The figures I have to do are ellipse, circle, square, rectangle and triangle. This is quite an easy assignment, since most of the forms can be drawn using standard methods of the graphical class. However, a triangle should be drawn using 3 lines.
This is where the problem kicks in. Every form gets drawn without problem, except for the triangle. No matter whether I use predefined or user-chosen dimensions, it doesn't appear on the drawing pane.
Have been searching on this for quite some time and don't have any compiling errors, so I really wonder how to solve this. I know some of you are into java as well, so it would be nice if you'd take a look.
So this is not a request to make my university assignment, this is a request for people who know java to take a look into it and help me out with this last issue I can't seem to get fixed.
I am meant to, for an assignment at university, create an interactive GUI that allows the user to select one of the basic forms, draw it with the desired dimensions and get the drawing and information in response.
The figures I have to do are ellipse, circle, square, rectangle and triangle. This is quite an easy assignment, since most of the forms can be drawn using standard methods of the graphical class. However, a triangle should be drawn using 3 lines.
This is where the problem kicks in. Every form gets drawn without problem, except for the triangle. No matter whether I use predefined or user-chosen dimensions, it doesn't appear on the drawing pane.
Have been searching on this for quite some time and don't have any compiling errors, so I really wonder how to solve this. I know some of you are into java as well, so it would be nice if you'd take a look.
- .zip-file with the .java and .class-files: http://www.mediafire.com/download/5zdfkb4wyjfl1i9/Figure.zip
- .jar-file, which is the execution of the current applet. This is not a virus, nor has it broken down my computer, but it is at your own risk to open it if you don't understand java. http://www.mediafire.com/download/vga5r655cvjrbch/Figure.jar
Launcher
Selection
In here is the code for the drawing. There are 2 lines with // in front, those are the ones that should be drawing the triangle. However, none of them work (without // of course).
Drawing
Calculation
Figure
Ellipse
Circle
Rectangle
Square
Triangle
Code:
import java.awt.*;
import javax.swing.*;
public class Launcher extends JFrame
{
private Drawing drawing;
private Calculation calculation;
private Selection selection;
public Launcher(){
drawing = new Drawing();
calculation = new Calculation();
selection = new Selection(drawing, calculation);
JPanel panel = new JPanel( new BorderLayout() );
panel.add( drawing, BorderLayout.CENTER );
panel.add( selection, BorderLayout.NORTH );
panel.add( calculation, BorderLayout.SOUTH );
setContentPane(panel);
}
public static void main(String args[]){
JFrame frame = new Launcher();
frame.setSize( 400, 400 );
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
}
}
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Selection extends JPanel
{
private Drawing drawing;
private Calculation calculation;
private JPanel panel1, cards;
private JTextField cradius, tbase, dheight, emaj, emin, rbase, rheight, sside;
public Selection( Drawing tp, Calculation bp ){
drawing = tp;
calculation = bp;
setLayout( new GridLayout( 2, 1 ) );
String[] shapes = { "Circle", "Triangle", "Ellipse", "Rectangle", "Square" };
panel1 = new JPanel();
JLabel heading = new JLabel( "Choose a shape:" );
JComboBox<String> cb = new JComboBox<String>(shapes);
cb.setEditable(false);
cb.addActionListener( new CBHandler() );
panel1.add(heading);
panel1.add(cb);
cards = new JPanel( new CardLayout());
JPanel card1 = new JPanel();
card1.add( new JLabel( "Radius: " ));
cradius = new JTextField(3);
card1.add(cradius);
JButton drawc = new JButton("Draw");
card1.add(drawc);
drawc.addActionListener( new DrawHandler("Circle") );
JPanel card2 = new JPanel();
card2.add( new JLabel( "Base: "));
tbase = new JTextField(3);
card2.add(tbase);
card2.add( new JLabel( ", height: " ));
dheight = new JTextField(3);
card2.add(dheight);
JButton drawd = new JButton("Draw");
card2.add(drawd);
drawd.addActionListener( new DrawHandler("Triangle") );
JPanel card3 = new JPanel();
card3.add( new JLabel( "Minor axis: " ));
emin = new JTextField(3);
card3.add(emin);
card3.add( new JLabel( ", major axis: " ));
emaj = new JTextField(3);
card3.add(emaj);
JButton drawe = new JButton("Draw");
card3.add(drawe);
drawe.addActionListener( new DrawHandler("Ellipse") );
JPanel card4 = new JPanel();
card4.add( new JLabel( "Width: " ));
rbase = new JTextField(3);
card4.add(rbase);
card4.add( new JLabel( ", height: " ));
rheight = new JTextField(3);
card4.add(rheight);
JButton drawr = new JButton("Draw");
card4.add(drawr);
drawr.addActionListener( new DrawHandler("Rectangle") );
JPanel card5 = new JPanel();
card5.add( new JLabel( "Side: " ));
sside = new JTextField(3);
card5.add(sside);
JButton drawv = new JButton("Draw");
card5.add(drawv);
drawv.addActionListener( new DrawHandler("Square") );
cards.add( card1, "Circle" );
cards.add( card2, "Triangle" );
cards.add( card3, "Ellipse" );
cards.add( card4, "Rectangle" );
cards.add( card5, "Square" );
add( panel1 );
add( cards );
}
public class CBHandler implements ActionListener
{
public void actionPerformed( ActionEvent e ){
JComboBox jcb = (JComboBox) e.getSource();
String selected = (String) jcb.getSelectedItem();
CardLayout cl = (CardLayout) cards.getLayout();
cl.show( cards, selected );
}
}
public class DrawHandler implements ActionListener
{
String shape;
public DrawHandler( String shape ){
this.shape = shape;
}
public void actionPerformed( ActionEvent e ){
if( shape.equals("Circle") ){
int radius = Integer.parseInt( cradius.getText() );
drawing.toggleEllipse( radius, radius );
calculation.calcEllipse( radius, radius );
}
if( shape.equals("Ellipse") ){
int majax = Integer.parseInt( emaj.getText() );
int minax = Integer.parseInt( emin.getText() );
drawing.toggleEllipse( majax, minax );
calculation.calcEllipse( majax, minax );
}
if( shape.equals("Square") ){
int side = Integer.parseInt( sside.getText() );
drawing.toggleRectangle( side, side );
calculation.calcRectangle( side, side );
}
if( shape.equals("Rectangle") ){
int width = Integer.parseInt( rbase.getText() );
int height = Integer.parseInt( rheight.getText() );
drawing.toggleRectangle( width, height );
calculation.calcRectangle( width, height );
}
if( shape.equals("Triangle") ){
int base = Integer.parseInt( tbase.getText() );
int height = Integer.parseInt( dheight.getText() );
drawing.toggleTriangle( base, height );
calculation.calcTriangle( base, height );
}
}
}
}
Drawing
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Drawing extends JPanel
{
private boolean drawEllipse, drawRectangle, drawTriangle;
int x, y, e1, e2, e3, e4, e5, e6;
public void paintComponent( Graphics g ){
super.paintComponent(g);
setBackground( Color.WHITE );
if(drawEllipse){ g.drawOval( x, y, e1, e2 ); }
if(drawRectangle){ g.drawRect( x, y, e1, e2 ); }
// if(drawTriangle){ g.drawLine( e1, e2, e3, e4 ); g.drawLine( e1, e2, e5, e6 ); g.drawLine( e3, e4, e5, e6 ); }
// if(drawTriangle){ g.drawLine( 0, 150, 200, 150 ); g.drawLine( 200, 150, 100, 0 ); g.drawLine( 100, 0, 0, 150 ); }
}
public void toggleEllipse( int majax, int minax ){
int horDim = getWidth();
int vertDim = getHeight();
x = (int)( 0.5*( horDim-majax ));
y = (int)( 0.5*( vertDim-minax ));
e1 = majax;
e2 = minax;
drawEllipse = true;
drawRectangle = false;
drawTriangle = false;
repaint();
}
public void toggleRectangle( int base, int height ){
int horDim = getWidth();
int vertDim = getHeight();
x = (int)( 0.5*( horDim-base ));
y = (int)( 0.5*( vertDim-height ));
e1 = base;
e2 = height;
drawRectangle = true;
drawEllipse = false;
drawTriangle = false;
repaint();
}
public void toggleTriangle( int basis, int height ){
int horDim = getWidth();
int vertDim = getHeight();
e1 = (int)( 0.5*( horDim-basis ));
e2 = (int)( 0.5*( vertDim+height ));
e3 = (int)( 0.5*( horDim+basis ));;
e4 = e1;
e5 = (int)(horDim*0.5);
e6 = (int)( 0.5*( vertDim-height ));
drawTriangle = true;
drawEllipse = false;
drawTriangle = false;
repaint();
}
}
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static java.lang.Math.*;
public class Calculation extends JPanel
{
private JTextField surface, circumference;
public Calculation(){
setLayout( new GridLayout( 1, 2 ));
JPanel surfacep = new JPanel();
surfacep.add( new JLabel( "surface: " ));
surface = new JTextField(6);
surfacep.add(surface);
JPanel circumferencep = new JPanel();
circumferencep.add( new JLabel( "circumference: " ));
circumference = new JTextField(6);
circumferencep.add(circumference);
add(surfacep);
add(circumferencep);
}
public void calcEllipse( int majax, int minax ){
double surf, circ;
if( majax == minax ){
Circle c = new Circle( 0, 0, majax );
surf = c.surface();
circ = c.circumference();
} else {
Ellipse e = new Ellipse( 0, 0, majax, minax );
surf = e.surface();
circ = e.circumference();
}
surface.setText( ""+surf );
circumference.setText( ""+circ );
}
public void calcRectangle( int base, int height ){
double surf, circ;
if( base == height ){
Square v = new Square( 0, 0, base );
surf = v.surface();
circ = v.circumference();
} else {
Rectangle r = new Rectangle( 0, 0, base, height );
surf = r.surface();
circ = r.circumference();
}
surface.setText( ""+surf );
circumference.setText( ""+circ );
}
public void calcTriangle( int base, int height ){
Triangle d = new Triangle( 0, 0, base, height );
surface.setText( ""+d.surface() );
circumference.setText( ""+d.circumference() );
}
}
Code:
public class Figure
{
protected int xCenter, yCenter;
public Figure( int x, int y ){
this.xCenter = x; this.yCenter = y;
}
public double surface(){
return -1;
}
public double circumference(){
return -1;
}
}
Code:
import static java.lang.Math.*;
public class Ellipse extends Figure
{
protected int minorAxis, majorAxis;
public Ellipse( int x, int y, int majax, int minax ){
super( x, y );
this.minorAxis = majax; this.majorAxis = minax;
}
public double surface(){
double surf = PI * majorAxis * minorAxis;
surf *= 10000; surf = round(surf); surf /= 10000;
return surf;
}
public double circumference(){
double circ = PI*( majorAxis + minorAxis );
circ *= 10000; circ = round(circ); circ /= 10000;
return circ;
}
}
Code:
public class Circle extends Ellipse
{
public Circle( int x, int y, int r ){
super( x, y, r, r );
}
public double surface(){
return super.surface();
}
public double circumference(){
return super.circumference();
}
}
Code:
import static java.lang.Math.*;
public class Rectangle extends Figure
{
protected int base, height;
public Rectangle( int x, int y, int s1, int s2 ){
super( x, y );
this.base = s1; this.height = s2;
}
public double surface(){
double surf = base*height;
surf *= 10000; surf = round(surf); surf /= 10000;
return surf;
}
public double circumference(){
double circ = 2*base + 2*height;
circ *= 10000; circ = round(circ); circ /= 10000;
return circ;
}
}
Code:
public class Square extends Rectangle
{
public Square( int x, int y, int s ){
super( x, y, s, s );
}
public double surface(){
return super.surface();
}
public double circumference(){
return super.circumference();
}
}
Code:
import static java.lang.Math.*;
public class Triangle extends Figure
{
private int base, height;
public Triangle( int x, int y, int b, int h ){
super( x, y );
this.base = b; this.height = h;
}
public double surface(){
double surf = base*height/2;
surf *= 10000; surf = round(surf); surf /= 10000;
return surf;
}
public double circumference(){
double circ = base + height + sqrt( pow( base, 2 ) + pow( height, 2 ));
circ *= 10000; circ = round(circ); circ /= 10000;
return circ;
}
}
So this is not a request to make my university assignment, this is a request for people who know java to take a look into it and help me out with this last issue I can't seem to get fixed.