Results 1 to 5 of 5

Thread: Repaint problem

  1. #1
    citizenXL is offline Member
    Join Date
    Oct 2009
    Location
    Mars
    Posts
    3
    Rep Power
    0

    Default Repaint problem

    Hi I'm kind of new to java and I have a problem with 2d graphics repaint method, I have a JFrame which is split in to 2 sections via a borderlayout. The bottom part of the JFrame consist of 4 JTextfields & 1 JButton.

    The Center section of the JFrame consist of a JPanel, the 4 JTextfields enable the user to enter the width,height,angle,angle end of an Arc2D.Double shape, the JButton is then pressed to draw the shape on the center JPanel, now I dont have a problem drawing the shape, but everytime I enter new values in to the JTextfields and press the button, instead of the old shape to be wiped clean and a new shape drawn again & again, hence could someone tell me what I can do to enable the old shape to dissapear if a new shape is drawn via repaint().

    Here is the code Please could someone help me:(
    p.s the code is divided in to 2 classes

    package com.rail.network.my;
    import javax.swing.*;

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Arc2D;

    public class ThreadTest2 extends JFrame{
    protected JTextField jtf1,jtf2,jtf3,jtf4;
    BorderLayout bdl = new BorderLayout();
    JPanel jpSouth,jpTop,jpCenter;

    public ThreadTest2(){
    jtf1 = new JTextField(2);
    jtf2 = new JTextField(2);
    jtf3 = new JTextField(2);
    jtf4 = new JTextField(2);
    JButton jbtn = new JButton("Send");
    jpSouth = new JPanel();
    final centerPaint cp = new centerPaint();

    Container content = this.getContentPane();
    content.setLayout(new BorderLayout());

    ActionListener listner = new ActionListener(){
    public void actionPerformed(ActionEvent ae){
    System.out.println("test tabbed pane");
    int awidth = Integer.parseInt(jtf1.getText());
    int aheight = Integer.parseInt(jtf2.getText());
    int aangleS = Integer.parseInt(jtf3.getText());
    int aangleEnd = Integer.parseInt(jtf4.getText());
    cp.initializeCords(awidth, aheight, aangleS, aangleEnd);
    }};

    jbtn.addActionListener(listner);

    jpSouth.add(jtf1);
    jpSouth.add(jtf2);
    jpSouth.add(jtf3);
    jpSouth.add(jtf4);
    jpSouth.add(jbtn);

    content.add(jpSouth,BorderLayout.SOUTH);
    content.add(cp,BorderLayout.CENTER);

    }



    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame windo = new ThreadTest2();
    windo.setVisible(true);
    }

    }


    package com.rail.network.my;

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Shape;
    import java.awt.geom.Arc2D;

    import javax.swing.JPanel;

    public class centerPaint extends JPanel{
    BasicStroke stroke = new BasicStroke(2.0f);
    int width=0;
    int height=0;
    int angleS=0;
    int angleEnd=0;

    public centerPaint(){}

    public void initializeCords(int width,int height,int angleS,int angleEnd){
    this.width=width;
    this.height=height;
    this.angleS=angleS;
    this.angleEnd=angleEnd;
    this.revalidate();
    this.repaint();

    }

    public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setBackground(Color.gray);
    g2.setPaint(Color.blue);
    g2.setStroke(stroke);
    Shape s = new Arc2D.Double(40,60,width,height,angleS,angleEnd,Ar c2D.OPEN);
    g2.fill(s);
    g2.draw(s);
    }
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    806
    Rep Power
    6

    Default

    in frame leve in ThreadTest2 u try to call repaint..

    cp.initializeCords(awidth, aheight, aangleS, aangleEnd);
    repaint();
    Ramya:cool:

  3. #3
    citizenXL is offline Member
    Join Date
    Oct 2009
    Location
    Mars
    Posts
    3
    Rep Power
    0

    Default repaint problem

    Yes i know that, but as I said, every time I enter new vlaues to draw another shape, instead of the old shape to dissapear when a new shape is created, the old shape still appears on the jpanel, is there something I'm doing wrong, did not quite understand your last reply

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    806
    Rep Power
    6

    Default

    instead of doing repaint in panel level ..do in frame level.iam asking u to put inside actionPerformed meth after calling initializeCords
    Ramya:cool:

  5. #5
    citizenXL is offline Member
    Join Date
    Oct 2009
    Location
    Mars
    Posts
    3
    Rep Power
    0

    Default repaint problem

    thanks RamyaSivakanth ,problem solved, I can't believe that was the problem staring me in the faceDD

Similar Threads

  1. Problem in mouse click n repaint
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 07-04-2008, 11:16 AM
  2. repaint problem
    By amith in forum Java 2D
    Replies: 2
    Last Post: 07-01-2008, 12:10 AM
  3. Problem in repaint
    By Preethi in forum AWT / Swing
    Replies: 16
    Last Post: 03-18-2008, 08:10 PM
  4. Repaint problem
    By swimberl in forum Java 2D
    Replies: 1
    Last Post: 02-16-2008, 09:12 PM
  5. Repaint problem
    By swimberl in forum Java 2D
    Replies: 0
    Last Post: 01-06-2008, 03:28 AM

Tags for this Thread

Posting Permissions

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