java - Paint and Buttons -


i'm trying program clear picture when button pushed , every time enter key pressed in text box new piece should display. doesn't seem after first action.

import javax.swing.jframe;  public class pushpicture { //----------------------------------------------------------------- //  creates main program frame. //----------------------------------------------------------------- public static void main(string[] args) {   jframe frame = new jframe("push paint");   frame.setdefaultcloseoperation(jframe.exit_on_close);    frame.getcontentpane().add(new pushtopaint());    frame.pack();   frame.setvisible(true); } } 

and meat of program...

import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class pushtopaint extends jpanel { //local constants final int max = 5;  //local variables private int count; private string temp; private jtextfield text; private jbutton clear; private jlabel title; private gridlayout main; buttonlistener listen;  /*********************** * program name  : push paint * author        : joshua james * date          : april 15, 2014 * description   : initialize elements , build panel *       be displayed. **********************/  public pushtopaint() {     listen = new buttonlistener();      //set jbutton     clear = new jbutton("clear");     clear.addactionlistener(listen);      //set textfield , label     text = new jtextfield(5);     text.addactionlistener(listen);      //initialize label     title = new jlabel ("enter text. see picture.");      //add textfield , label     add(title);     add(text);     add(clear);      //set window size , color     setpreferredsize(new dimension(400,400));     setbackground(color.blue); }  /*********************** * program name  : buttonlistener * author        : joshua james * date          : april 15, 2014 * description   : react actions made **********************/  private class buttonlistener implements actionlistener {     public void actionperformed(actionevent event)     {         if (event.getsource() == text)         {             text.settext("");             count ++;         }          else             count = 0;     } }  /*********************** * program name  : paintcomponent * author        : joshua james * date          : april 15, 2014 * description   : method paint in panel. **********************/  public void paintcomponent(graphics page) {     super.paintcomponent(page);      //set reset parameters     if (count == max)         count = 0;      switch(count)     {         //clear panel         case 0:             repaint();             break;//end case          case 1:             //draw snowman head             page.setcolor(color.white);             page.filloval(160,100,80,80);             break;          case 2:             //draw eyes             page.setcolor(color.red);             page.filloval(180,120,10,10);             page.filloval(210,120,10,10);             break;          case 3:             //draw torso             page.setcolor(color.white);             page.filloval(160,180,100,100);             break;          case 4:             //draw base             page.setcolor(color.white);             page.filloval(140,280,120,120);             break;          default:             break;     } } } 

don't call repaint in paintcomponent method, call repaint in actionperformed method

there no need paintcomponent method public, there should no reason why 1 else should ever want call it, saying


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -