java - How to do different drawings in different methods using paintComponent? -
i have 2 classes, 1 extends panel , other extends frame.
first class, panel(used drawings):
import java.awt.canvas; import java.awt.color; import java.awt.font; import java.awt.graphics; import javax.swing.jlabel; import javax.swing.jpanel; public class graficpne extends jpanel{ public int dim1 = 0, dim2 = 0; public jlabel lblsem1graficpne, lblsem2graficpne; public graficpne(){ super(); this.setlayout(null); this.setupcomponents(); } public void setupcomponents(){ lblsem1graficpne = new jlabel("sem i"); lblsem2graficpne = new jlabel("sem ii"); lblsem1graficpne.setfont(new font("serif", font.plain, 12)); lblsem2graficpne.setfont(new font("serif", font.plain, 12)); this.add(lblsem1graficpne); this.add(lblsem2graficpne); } public void laycomponents(int firstconv, int secondconv){ lblsem1graficpne.setbounds(20, getheight() - firstconv - 20, 100, 20); lblsem2graficpne.setbounds(100, getheight() - secondconv - 20, 100, 20); } public void paint(graphics g){ super.paint(g); g.setcolor(color.black); g.drawrect(0, 0, getwidth()-1, getheight()-1); this.setbackground(color.green); g.setcolor(color.red); g.fillrect(20, getheight()-dim1, 40, dim1); g.setcolor(color.blue); g.fillrect(100, getheight()-dim2, 40, dim2); } public void setdata(int dim1, int dim2){ this.dim1 = dim1; this.dim2 = dim2; } }
second class, frame, used add panel on it:
import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jtextfield; public class graficfrm extends jframe implements actionlistener{ private jbutton btnapply; private jlabel lblsem1, lblsem2; private jtextfield txtsem1, txtsem2; private graficpne ob; //the object first class private int firstconv = 0, secondconv = 0; public graficfrm() { super("grafic soft"); initsettings(); setupcomponents(); } private void initsettings() { this.setdefaultcloseoperation(jframe.exit_on_close); this.setlayout(null); this.setsize(400, 450); this.setlocation(100, 100); this.setresizable(false); } private void setupcomponents() { ob = new graficpne(); lblsem1 = new jlabel("sem i"); txtsem1 = new jtextfield(); lblsem2 = new jlabel("sem ii"); txtsem2 = new jtextfield(); btnapply = new jbutton("apply"); ob.setbounds(30, 50, 340, 200); lblsem1.setbounds(30, 280, 100, 20); txtsem1.setbounds(150, 280, 220, 20); lblsem2.setbounds(30, 330, 100, 20); txtsem2.setbounds(150, 330, 220, 20); btnapply.setbounds(170, 380, 80, 30); this.add(lblsem1); this.add(ob); this.add(txtsem1); this.add(lblsem2); this.add(txtsem2); this.add(btnapply); btnapply.addactionlistener(this); } public void getconvertedvalues(){ string txtfield1 = txtsem1.gettext(); firstconv = integer.parseint(txtfield1); string txtfield2 = txtsem2.gettext(); secondconv = integer.parseint(txtfield2); } public void gethighvalues(){ if((firstconv > 100) || (secondconv > 100)) { joptionpane.showmessagedialog(this, "only values < 100 allowed"); firstconv = 0; secondconv = 0; txtsem1.settext(""); txtsem2.settext(""); ob.removeall(); ob.repaint(); } } public void actionperformed(actionevent e) { if(e.getsource().equals(btnapply)){ try{ new graficfrm(); getconvertedvalues(); gethighvalues(); ob.setdata(firstconv, secondconv); ob.laycomponents(firstconv,secondconv); ob.repaint(); }catch(numberformatexception ex1){ joptionpane.showmessagedialog(this, "only natural numbers allowed"); } } } public static void main(string[] args) { graficfrm frm = new graficfrm(); frm.setvisible(true); }
}
all want create new method called getnegativevalues(), called pressing apply button, , method executed when firstconv or secondconv < 0. block of method, getnegativevalues(), want keep green background of panel, in paint(graphics g), draw center horizontal line(g.drawline(0, getheight()/2, getwidth(), getheight()/2);). then, when execute program , type, let's value 55 first textfield , -40 second one, draw 55 on center horizontal line , -40 under center horizontal line, , 0 point on center horizontal line.
le: @nice cow i'll make short recap: so, first, when execute app, want appear green panel , 2 labels have 2 textfields, in example(try follow example, please)(all good, did this, part done). after, when complete 2 textfields let's 2 positive values, , when press apply button happen in example(without center vertical line)(so did this, also). good, problem is: if complete 1 of 2 textfields negative value , other positive one, , after press apply button, want application recognize there negative value typped in 1 of textfields, draw me on green panel center horizontal line(after press apply button) , draw 2 rectangles did in first picture uploaded. please me, said i'll understand want do, , want appreciate effort , i'm sorry (again) if confused(again).
i guess, looking method presented below, might can calculate, starting point of rectangles trying draw on jpanel
. here pos
positive value , neg
negative value:
public void setvalues(int above, int below) { midpoint = getheight() / 2; heightabove = math.abs(above); heightbelow = math.abs(below); /* * if both negative values, * need mid-point in case. */ if (above < 0 && below < 0) { rectabove = midpoint; diff = 120; rectbelow = midpoint; flag = true; } else if (above >= 0 && below >= 0) { rectabove = getheight() - heightabove; diff = 120; rectbelow = getheight() - heightbelow; flag = false; } else if (above < 0 && below >= 0) { rectabove = midpoint; diff = 40; rectbelow = midpoint - heightbelow; flag = true; } else if (above >= 0 && below < 0) { rectabove = midpoint - heightabove; diff = 40; rectbelow = midpoint; flag = true; } repaint(); }
here code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class paintingrectangles { private jtextfield abovefield; private jtextfield belowfield; private jbutton applybutton; private mycanvas canvas; private void displaygui() { jframe frame = new jframe("painting rectangles example"); frame.setdefaultcloseoperation(jframe.dispose_on_close); jpanel contentpane = new jpanel(); contentpane.setlayout(new borderlayout(5, 5)); jpanel rightpanel = new jpanel(); rightpanel.setlayout(new gridlayout(3, 1, 5, 5)); abovefield = new jtextfield(10); belowfield = new jtextfield(10); applybutton = new jbutton("apply"); applybutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent ae) { int above = 0; int below = 0; try { above = integer.parseint(abovefield.gettext()); below = integer.parseint(belowfield.gettext()); } catch(numberformatexception nfe) { abovefield.settext("0"); belowfield.settext("0"); above = 0; below = 0; nfe.printstacktrace(); } canvas.setvalues(above, below); } }); rightpanel.add(abovefield); rightpanel.add(belowfield); rightpanel.add(applybutton); canvas = new mycanvas(); contentpane.add(canvas, borderlayout.center); contentpane.add(rightpanel, borderlayout.line_end); frame.setcontentpane(contentpane); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { new paintingrectangles().displaygui(); } }); } } class mycanvas extends jpanel { private int midpoint = getheight() / 2; private int rectabove = getheight() / 2; private int rectbelow = getheight() / 2; private int width = 40; private int heightabove = 0; private int heightbelow = 0; private int diff = 0; private boolean flag = false; public mycanvas() { setbackground(color.green); } public void setvalues(int above, int below) { midpoint = getheight() / 2; heightabove = math.abs(above); heightbelow = math.abs(below); /* * if both negative values, * need mid-point in case. */ if (above < 0 && below < 0) { rectabove = midpoint; diff = 120; rectbelow = midpoint; flag = true; } else if (above >= 0 && below >= 0) { rectabove = getheight() - heightabove; diff = 120; rectbelow = getheight() - heightbelow; flag = false; } else if (above < 0 && below >= 0) { rectabove = midpoint; diff = 40; rectbelow = midpoint - heightbelow; flag = true; } else if (above >= 0 && below < 0) { rectabove = midpoint - heightabove; diff = 40; rectbelow = midpoint; flag = true; } repaint(); } @override public dimension getpreferredsize() { return (new dimension(400, 400)); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(color.black); g.drawrect(0, 0, getwidth(), getheight()); if (flag) g.drawline(0, getheight() / 2, getwidth(), getheight() / 2); makerectangles(g); } private void makerectangles(graphics g) { g.setcolor(color.red); g.fillrect(40, rectabove, width, heightabove); g.setcolor(color.blue); g.fillrect(diff, rectbelow, width, heightbelow); g.dispose(); } }
here output:
Comments
Post a Comment