Results 1 to 9 of 9
  1. #1
    howdil is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default Problem med att få diagram och visa sig!

    Hejhej! jag har som skoluppgift fått uppgiften att göra ett program där man ska kunna skriva in höjderna på 3 olika diagram och sedan infoga de diagrammen.

    Problemet jag har är att min generera diagrammen knappen inte funkar och jag tror jag vet ungefär vad som behövs men har inte kunskapen för att göra det . Har försökt googla runt lite men har inte hittat en bra lösning på problemet så jag vore jätteglad om något kunde hjälpa mig!

    Här kommer koden! :

    Första dokumentet
    Java Code:
    import javax.swing.*;
     import java.awt.*;
     import java.awt.event.*;
     
     public class stapel extends JPanel implements ActionListener 
     {
     JTextField tf1;
     JTextField tf2;
     JTextField tf3;
     JButton b;
     JLabel lbl1;
     JLabel lbl2;
     JLabel lbl3;
     int summa1,summa2,summa3;
     private int bredd = 40, diagram1, diagram2, diagram3;
     public stapel ()
     {
     
     this.setBackground(Color.white);
     lbl1 = new JLabel("Skriv in höjd för de olika staplarna.                                             ");
     tf1 = new JTextField(5);
     tf2 = new JTextField(5);
     tf3 = new JTextField(5);
     lbl2 = new JLabel ("                         ");
     lbl3 = new JLabel ("            ");
     b = new JButton("Skapa tabell");
     add(lbl1);
     add(tf1);
     add(tf2);
     add(tf3);
     add(lbl2);
     add(b);
     tf1.addActionListener(this);
     tf2.addActionListener(this);
     tf3.addActionListener(this);
     b.addActionListener(this);
     }
     
     
     public void actionPerformed(ActionEvent e)
     { 
     summa1 = Integer.parseInt(tf1.getText() );
     summa2 = Integer.parseInt(tf2.getText() );
     summa3 = Integer.parseInt(tf2.getText() );
     
     summa1 = diagram1; 
     summa2 = diagram2;
     summa3 = diagram3;
     
     }
     
     public void paintComponent(Graphics g)
     {
     super.paintComponent(g);
     // GRAFIKEN FÖR STAPLARNA
     g.setColor(Color.blue);
     g.fillRect(100,300-diagram1,bredd,diagram1);
     g.setColor(Color.red);
     g.fillRect(150,300-diagram2,bredd,diagram2);
     g.setColor(Color.green);
     g.fillRect(200,300-diagram3,bredd,diagram3);
     }
     
     
     }
    Java Code:
     import javax.swing.*;
     
     
     
     public class Visastapel extends JPanel {
     
     
     
     
     
     public static void main(String[] args)
     	{
     	JFrame f = new JFrame();
     	f.setSize(400, 400);
     	f.setLocation(100,100);
     	f.setTitle("Stapeldiagram");
     	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     	stapel p = new stapel ();
     	f.add(p);
     	f.setVisible(true);
     	}
     }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Now in English please; Babelfish can be of help if needed.

    kind regards,

    Jos

  3. #3
    howdil is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Oh sorry :p
    Translation :
    Hey! i've got a problem with getting the button in the projekt to work. The program is built so you can type in different numbers in the three fields and when you press the button the program will paint a bar graph with 3 lines. The numbers you type into the textfields is the heigth of the lines.

    When i press the button nothing happens! i'm pretty new to java and i would get really happy if someone could help me :) (i wrote the code myself, if you have any tip i could use please reply!)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here:
    Java Code:
     summa1 = Integer.parseInt(tf1.getText() );
     summa2 = Integer.parseInt(tf2.getText() );
     summa3 = Integer.parseInt(tf2.getText() );
     
     summa1 = diagram1; 
     summa2 = diagram2;
     summa3 = diagram3;
    You're getting the numbers from the user and placing them into summa1, summa2, and summa3, but then you erase those values by making summa1 = diagram1. You need to reverse this:
    Java Code:
     summa1 = Integer.parseInt(tf1.getText() );
     summa2 = Integer.parseInt(tf2.getText() );
     summa3 = Integer.parseInt(tf2.getText() );
     
     diagram1 = summa1;
     diagram2 = summa2;
     diagram3 = summa3;
    
    // then call repaint on your JPanel
      repaint();
    And you also need to call repaint() on your JPanel.

    You'll do even better getting rid of summa1 - summa3 altogether and just setting diagram1 - diagram3 directly:

    Java Code:
     diagram1 = Integer.parseInt(tf1.getText() );
     //...

  5. #5
    howdil is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Thaaankkss <3 you saved my weekend :D

  6. #6
    howdil is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    hmm just a quick question, whats the command for clearing it upp ? (i allready know how to do the button for it) :)

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    If you're asking about clearing a JTextField, just set its text to the empty String:
    Java Code:
    tf1.setText("");
    db

  8. #8
    howdil is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    Naah i'm more looking for clearing the fillRect's :o

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    super.paintComponent(g) (which you already have) will clear any previously painted image on your JComponent. to get rid of the drawing, you could use a boolean that tells the paintComponent to paint the drawing or not.

Similar Threads

  1. XML ER diagram
    By palrakesh in forum XML
    Replies: 0
    Last Post: 06-07-2010, 07:48 AM
  2. plz i need help ((ER diagram))
    By sweet angle in forum JDBC
    Replies: 9
    Last Post: 02-01-2009, 10:28 PM
  3. Replies: 0
    Last Post: 06-28-2008, 04:54 AM
  4. Replies: 0
    Last Post: 06-10-2008, 07:37 AM
  5. Java Lead with B1 Visa
    By Strategic-One in forum Jobs Offered
    Replies: 0
    Last Post: 04-08-2008, 10:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •