java - Buttons and paint method -


i'm having trouble buttons. know they're working because i've tested them out exiting problem through system.exit. here output looks like:

http://imgur.com/ks7mifa

when click close button, handle on switch should redraw other side , close button should change open. when click open button, should opposite. however, buttons aren't doing anything. doing wrong?

public class programmingassignment2 {  public static void main(string[] args) {      boolean ison = false;      // objects     circuit circuitobject = new circuit();     controller controllerobject = new controller();     draw drawobject = new draw();     alllisteners listenerobject = new alllisteners();     drawobject.window();      circuitobject.buttons(drawobject, ison);     controllerobject.openfile("programming assignment 2 data.txt", drawobject); } } 

class circuit creates buttons

import javax.swing.jbutton;  public class circuit {  public void buttons(draw drawobject, boolean ison) {     alllisteners listenerobject = new alllisteners();      if (ison == true) {          jbutton openbutton = new jbutton("close");         openbutton.addactionlistener(listenerobject);         openbutton.setbounds(200, 100, 50, 20);         drawobject.add(openbutton);     } else if (ison == false) {          jbutton closebutton = new jbutton("open");         closebutton.addactionlistener(listenerobject);         closebutton.setbounds(50, 100, 50, 20);         drawobject.add(closebutton);     } } } 

draw class of work. creates graphics , reads in text file has titles of each object(like switch , lightbulb).

import java.io.bufferedreader; import java.io.filereader; import java.io.filenotfoundexception; import java.io.ioexception;  import javax.swing.jframe; import java.awt.graphics;  public class draw extends jframe {  private string[] line = new string[5]; private int counter = 0; private boolean ison;  public void window() {      setsize(500, 500);     settitle("programming assignment 2");     setdefaultcloseoperation(jframe.exit_on_close);     setvisible(true); }  public void readfile(string filename) {      counter = 1;     bufferedreader br = null;      try {         br = new bufferedreader(new filereader(filename));           (int = 0; < 4; i++) {             line[i] = br.readline();         }      } catch (filenotfoundexception e) {         string error = "file not found";         system.out.println(error.tostring());         system.out.println("or not opened.");     } catch (ioexception e) {         system.out.println("error reading file.");     } {         try {             br.close();         } catch (ioexception e) {             system.out.println("error closing file.");         }     } }  public void paint(graphics g) {     circuit circuitobject = new circuit();     super.paint(g);      if (ison == true) {         turnon(g);         circuitobject.buttons(this, ison);     } else {         turnoff(g);     } }  public void setison() {     ison = true; }  public void setisoff() {     ison = false; }  public void turnoff(graphics g) {     // title     g.drawstring(line[0], 150, 40);      //switch     g.drawstring(line[2], 130, 190);     g.drawrect(100, 150, 100, 20);     g.drawoval(115, 155, 10, 10);     g.drawoval(175, 155, 10, 10);     g.drawarc(140, 140, 20, 20, 180, -180);       //off switch     g.drawline(160, 150, 182, 133);     g.drawline(157, 142, 173, 128);     g.drawline(173, 128, 182, 133);       //power supply     g.drawstring(line[1], 50, 420);     g.drawrect(50, 320, 50, 80);     g.drawline(50, 320, 70, 290);     g.drawline(100, 320, 120, 290);     g.drawline(70, 290, 120, 290);     g.drawline(120, 370, 120, 290);     g.drawline(120, 370, 100, 400);     //plus     g.drawline(94, 310, 100, 310);     g.drawline(97, 307, 97, 313);     // minus     g.drawline(100, 300, 107, 300);     // pliers     g.drawrect(70, 305, 5, 10);     g.drawrect(90, 288, 5, 10);      //lightbulb     g.drawstring(line[3], 400, 250);     g.drawrect(400, 200, 20, 20);     g.drawoval(395, 170, 30, 30);     // pliers     g.drawrect(400, 220, 5, 10);     g.drawrect(415, 220, 5, 10);      // plus wire switch     g.drawline(72, 305, 120, 160);     //bulb switch     g.drawline(180, 160, 400, 230);     //bulb minus     g.drawline(90, 290, 420, 230); }  public void turnon(graphics g) {     // title     g.drawstring(line[0], 150, 40);      //switch     g.drawstring(line[2], 130, 190);     g.drawrect(100, 150, 100, 20);     g.drawoval(115, 155, 10, 10);     g.drawoval(175, 155, 10, 10);     g.drawarc(140, 140, 20, 20, 180, -180);      //on switch     g.drawline(140, 150, 122, 133);     g.drawline(143, 142, 129, 128);     g.drawline(122, 133, 129, 128);      //power supply     g.drawstring(line[1], 50, 420);     g.drawrect(50, 320, 50, 80);     g.drawline(50, 320, 70, 290);     g.drawline(100, 320, 120, 290);     g.drawline(70, 290, 120, 290);     g.drawline(120, 370, 120, 290);     g.drawline(120, 370, 100, 400);     //plus     g.drawline(94, 310, 100, 310);     g.drawline(97, 307, 97, 313);     // minus     g.drawline(100, 300, 107, 300);     // pliers     g.drawrect(70, 305, 5, 10);     g.drawrect(90, 288, 5, 10);      //lightbulb     g.drawstring(line[3], 400, 250);     g.drawrect(400, 200, 20, 20);     g.drawoval(395, 170, 30, 30);     // pliers     g.drawrect(400, 220, 5, 10);     g.drawrect(415, 220, 5, 10);      // plus wire switch     g.drawline(72, 305, 120, 160);     //bulb switch     g.drawline(180, 160, 400, 230);     //bulb minus     g.drawline(90, 290, 420, 230); } } 

controller doesn't right.

public class controller {  public void openfile(string filename, draw drawobject) {     drawobject.readfile(filename); } } 

and actionlisterner class

import java.awt.event.actionlistener; import java.awt.event.actionevent;  public class alllisteners implements actionlistener {  public void actionperformed(actionevent e) {     circuit circuitobject = new circuit();     draw drawobject = new draw();     string buttonstring = e.getactioncommand();      if (buttonstring.equals("close")) {         drawobject.setison();         drawobject.repaint();      } else if (buttonstring.equals("open")) {         drawobject.setisoff();         drawobject.repaint();      } else {         system.out.println("unexpected error.");     } } } 

you've got lots of major problems code including:

  • drawing directly within jframe's paint method, fraught problems risk messing jframe's own complicated painting.
  • placing program logic within painting method, method don't have full control on when or if fires, , 1 shouldn't slow down
  • placing component creation code within painting method.
  • trying add multiple jbuttons willy nilly rather changing state of existing component.
  • creating multiple circuit objects.
  • trying use absolute positioning via setbounds(...) place component in container uses borderlayout.

i suggest

  • start on , scrap code.
  • draw in jpanel's paintcomponent method, tutorials tell do.
  • create buttons once , once , add them gui.
  • get program logic out of painting (here paintcomponent) method, , object state changing outside of method painting , painting only.
  • instead logic should belong controller, , should notified of button push.
  • so consider having button push notify control pushed,
  • the control changes state of program (changes variables)
  • and calls repaint paintcomponent method can use variables change drawing.
  • also, avoid using setbounds , null layouts if @ possible, , instead use layout managers/borders/nested jpanels place components.

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -