a little java help

removing a picture in a frame after button is clicked

Page 1 of 1

5 Replies - 703 Views - Last Post: 13 April 2010 - 05:37 AM Rate Topic: -----

#1 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

a little java help

Posted 12 April 2010 - 07:29 AM

hello, i have got most of my code working but when i click a diffrent button the shape is just written over it and i want the old to be removed... how do i fix that,, here is my code.

import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.event.*;
import java.awt.*;

public class RAC11_01 implements ActionListener {

    JButton button1;
    JButton button2;
    JButton button3;
    JButton button4;
    
    JPanel panel5 = new JPanel();  
    JFrame frame = new JFrame("Test Frame");
    JPanel buttonPanel = new JPanel();

    public static void main(String[] args) {
        RAC11_01 gui = new RAC11_01();
        gui.go();
    }

    public void go() {

        Dimension frameDimension = new Dimension(900, 400);
        frame.setMinimumSize(frameDimension);

        JPanel RectPanel = new JPanel();
        JPanel OvalPanel = new JPanel();
        JPanel ArcPanel = new JPanel();
        JPanel PolyPanel = new JPanel();

//----------------------------------------------------------------------------- FRAME FOR ....

        panel5.setBackground(Color.PINK);
        panel5.setSize(200, 300); 
        Container contentPane = frame.getContentPane();
        contentPane.add(panel5);

//----------------------------------------------------------------------	   panel1
        RectPanel.setBackground(Color.BLACK);

        buttonPanel.add(RectPanel);



        button1 = new JButton("Show Rectangle");

        button1.addActionListener(this);
        RectPanel.add(button1);


//---------------------------------------------------------------------------	panel2

        OvalPanel.setBackground(Color.red);

        buttonPanel.add(OvalPanel);

        button2 = new JButton("Show Oval");
        button2.addActionListener(this);
        OvalPanel.add(button2);




//--------------------------------------------------------------------------	     panel3
        ArcPanel.setBackground(Color.GREEN);

        buttonPanel.add(ArcPanel);
        JButton button3 = new JButton("Show Arc");
        button3.addActionListener(this);
        ArcPanel.add(button3);

//--------------------------------------------------------------------------------------panel4
        PolyPanel.setBackground(Color.blue);

        buttonPanel.add(PolyPanel);

        contentPane.add(buttonPanel);


        JButton button4 = new JButton("Show Poly");
        button1.addActionListener(this);
        PolyPanel.add(button4);

        frame.pack();  
        frame.setVisible(true);


        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

//----------------------------------------------------------
    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
      
        if (evt.getActionCommand().equalsIgnoreCase(button1.getText())) { 
            panel5.setName("RectPane");  
            drawRectangle();
        }
        if (evt.getActionCommand().equalsIgnoreCase(button2.getText())) { 

            panel5.setName("OvalPanel");  
            drawOval();
        }
        if (source == button3) {
            panel5.setName("ArcPanel");  
        } else if (source == button4) {
            panel5.setName("PolyPanel"); 
        }

    }

    private void drawRectangle() {
        panel5.removeAll();  
        panel5.validate();
        Graphics g = panel5.getGraphics();
        JComponent myRect = new MyRectangle();
        myRect.paint(g);
        panel5.add(myRect);
    }

    private void drawOval() {
        panel5.removeAll();  
        panel5.validate();
        Graphics g = panel5.getGraphics();
        JComponent myOval = new MyOval();
        myOval.paint(g);
        panel5.add(myOval);
    }

    class MyRectangle extends JComponent {

        public void paint(Graphics g) {
            g.setColor(Color.blue);
            g.drawRect(30, 20, 100, 200);
        }
    }

    class MyOval extends JComponent {

        public void paint(Graphics g) {
            g.setColor(Color.black);
            g.drawOval(50, 100, 100, 100);
        }
    }

}

 


Is This A Good Question/Topic? 0
  • +

Replies To: a little java help

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: a little java help

Posted 12 April 2010 - 08:01 AM

you can set up something like

g.setColor( Color.WHITE )
g.clearRect(0,0,width,height);



basically, this would clear what is already there
Was This Post Helpful? 0
  • +
  • -

#3 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4099
  • View blog
  • Posts: 18,184
  • Joined: 24-September 08

Re: a little java help

Posted 12 April 2010 - 08:41 AM

Oh this program again. If you're just going to reset the panels, just delete the instance of the panel you already have and add a panel of the shape you want.
Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: a little java help

Posted 12 April 2010 - 12:46 PM

that too... :D
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: a little java help

Posted 12 April 2010 - 10:08 PM

Graphics g = component.getGraphics();
is a very VERY bad coding practice
Your actionListener() should just update data of what you want to paint() or not and then calls repaint()
In your paint() method, act accordingly on what should be paint or not using the Graphics hangle received as parameter

public void actionPerformed(ActionEvent e) {
   if(e.getSource() == drawCircleButton) {
     drawCircle = true;
     repaint();
}

public void paint(Graphics g) {
   super.paint(g);  // to draw the background and the JComponent you do not want to draw
   if(drawCircle) {
     g.drawOvale(.......
   }
}


Was This Post Helpful? 0
  • +
  • -

#6 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: a little java help

Posted 13 April 2010 - 05:37 AM

after it repaints, should the boolean also be set back to false so it won't redraw the circle AND the next shape that is clicked too?
(that is a fantastic way to do it, btw. pbl, you are legend :bigsmile:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1