5 Replies - 518 Views - Last Post: 30 November 2017 - 12:59 PM Rate Topic: -----

#1 martzo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 02-February 17

Problem with Panel drawing

Posted 30 November 2017 - 11:40 AM

Hey,
First of all, sorry for my english, i'm french :)/>
So i've a question, I created a jFrame in which the users can create a point. I used Netbeans to create this frame. I added a "ok" button. When the user press this button, all the informations a collected and added to a text files with bufferedwriter with this code :
 private void Validation(java.awt.event.ActionEvent evt) {                            
        // TODO add your handling code here:
        System.out.println("Clic sur OK");
        String chaine ;
        String idn = id.getText() ;
        String s1 = x.getText();
        float absciss = Float.parseFloat(s1) ;
        String s2 = y.getText();
        float ordonne = Float.parseFloat(s2);
        System.out.println("Point : " + idn + " Abscisse " + absciss + " Ordonnee " + ordonne);
        chaine = "Point : " + idn + " Abscisse " + absciss + " Ordonnee " + ordonne ;
        try
        {
            BufferedWriter fichierFigures=new BufferedWriter(new FileWriter ("FicherFigures.txt",true)) ;
             fichierFigures.write(chaine);
             fichierFigures.newLine() ;
             fichierFigures.close();
        }
       catch (IOException err)
       {
        System.out.println("Erreur : \n" + err) ;
       }
        Fenetre f = new Fenetre();
        dispose();
    } 

abscisse is the x axe and ordonne is the y axe.

And i'd like to create this point on a Panel. I managed to create a Panel in a Frame in which I can draw point or line or anything.
But i can't manage to create a point with the information given by the user .
My Frame is coded with :
public class Fenetre extends JFrame {
 
    private JPanel dessin;
    private Container contenu;
 
    public Fenetre() {

        contenu=getContentPane();
        dessin=new Dessin();
        contenu.add(dessin);
        setTitle("Dessin");
        setSize(400,400);
        setVisible(true);
    }
}



And my panel is :
public class Dessin extends JPanel {
     
    public Dessin() {
        setBackground(Color.lightGray);
    }
     
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.drawOval(1,1,1,1);
    }
 
}

If someone can help me :)/>

Thanks !

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Panel drawing

#2 martzo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 02-February 17

Re: Problem with Panel drawing

Posted 30 November 2017 - 12:16 PM

Maybe i'm not clear enought. I don't want to draw the point (1,1,1,1) but the point (s1,s2,1,1)


Thanks !!
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14096
  • View blog
  • Posts: 56,492
  • Joined: 12-June 08

Re: Problem with Panel drawing

Posted 30 November 2017 - 12:28 PM

Wouldn't you create two publicly accessible variables for the class, reference those in the 'oval draw', and set them outside of the object as needed?
Was This Post Helpful? 0
  • +
  • -

#4 martzo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 02-February 17

Re: Problem with Panel drawing

Posted 30 November 2017 - 12:53 PM

I've never used public variable i had two of them in my pannel class as :

public class Dessin extends JPanel {
    public int a,b ;
    
    public Dessin() {
        setBackground(Color.lightGray);
    }
     
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.drawOval(a,b,1,1);
    }
 
}


But how do I set them ?

thanks
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14096
  • View blog
  • Posts: 56,492
  • Joined: 12-June 08

Re: Problem with Panel drawing

Posted 30 November 2017 - 12:56 PM

Hmm. You should bone up on object orientated programming.. but in the mean time you set them like anything else.

Declare your instance of the class.
Set that instance's properties/variables that are public.

Example:
Dessin foo = new Dessin ();
foo.a = 100;
foo.b = 100;

Was This Post Helpful? 0
  • +
  • -

#6 martzo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 02-February 17

Re: Problem with Panel drawing

Posted 30 November 2017 - 12:59 PM

Ok thank you so much !
Yep i'll read about this subject, thanks !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1