swing - Draw in java like windows paint -
i made java app draw, windows paint, not know if it's better in 2 classes, or left unattended in class, , on other side. can make more efficient?
here code:
import java.awt.graphics; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class mypaint{ public static void main(string[] args){ jframe frame = new jframe("just paint"); container content = frame.getcontentpane(); content.setlayout(new borderlayout()); final paddraw drawpad = new paddraw(); content.add(drawpad, borderlayout.center); jpanel panel = new jpanel(); panel.setpreferredsize(new dimension(100, 68)); panel.setminimumsize(new dimension(100, 68)); panel.setmaximumsize(new dimension(100, 68)); jbutton twox = new jbutton ("2"); twox.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ drawpad.clear(); } }); jbutton yellowbutton = new jbutton("yellow"); yellowbutton.setforeground(new color(204, 204, 0)); yellowbutton.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ drawpad.yellow(); } }); jbutton greenbutton = new jbutton("green"); greenbutton.setforeground(new color(0, 204, 0)); greenbutton.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ drawpad.green(); } }); jbutton redbutton = new jbutton("red"); redbutton.setforeground(new color(255, 0, 0)); redbutton.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ drawpad.red(); } }); jbutton bluebutton = new jbutton("blue"); bluebutton.setforeground(color.blue); bluebutton.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ drawpad.blue(); } }); jbutton blackbutton = new jbutton("black"); blackbutton.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ drawpad.black(); } }); greenbutton.setpreferredsize(new dimension(80, 20)); redbutton.setpreferredsize(new dimension(80, 20)); yellowbutton.setpreferredsize(new dimension(80, 20)); bluebutton.setpreferredsize(new dimension(80, 20)); greenbutton.setpreferredsize(new dimension(80,20)); panel.add(blackbutton); panel.add(bluebutton); panel.add(redbutton); panel.add(greenbutton); panel.add(yellowbutton); content.add(panel, borderlayout.north); jradiobutton rdbtnpx = new jradiobutton("3 px"); panel.add(rdbtnpx); jradiobutton rdbtnpx_1 = new jradiobutton("5 px"); panel.add(rdbtnpx_1); jradiobutton rdbtnpx_2 = new jradiobutton("12 px"); panel.add(rdbtnpx_2); buttongroup bg = new buttongroup(); bg.add(rdbtnpx); bg.add(rdbtnpx_1); bg.add(rdbtnpx_2); rdbtnpx.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ drawpad.small(); } }); rdbtnpx_1.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ drawpad.medium(); } }); rdbtnpx_2.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ drawpad.big(); } }); jbutton clearbutton = new jbutton("clear"); clearbutton.setbackground(new color(255, 255, 255)); clearbutton.setfont(uimanager.getfont("textarea.font")); clearbutton.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ drawpad.clear(); } }); panel.add(clearbutton); frame.setsize(454, 440); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } } class paddraw extends jcomponent{ private image image; private graphics2d graphics2d; private int currentx , currenty , oldx , oldy ; public paddraw(){ setdoublebuffered(false); addmouselistener(new mouseadapter(){ public void mousepressed(mouseevent e){ oldx = e.getx(); oldy = e.gety(); } }); addmousemotionlistener(new mousemotionadapter(){ public void mousedragged(mouseevent e){ currentx = e.getx(); currenty = e.gety(); if(graphics2d != null) graphics2d.drawline(oldx, oldy, currentx, currenty); repaint(); oldx = currentx; oldy = currenty; } }); } public void paintcomponent(graphics g){ if(image == null){ image = createimage(getsize().width, getsize().height); graphics2d = (graphics2d)image.getgraphics(); graphics2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); clear(); } g.drawimage(image, 5, 5, null); } public void clear(){ graphics2d.setpaint(color.white); graphics2d.fillrect(0, 0, getsize().width, getsize().height); graphics2d.setpaint(color.black); repaint(); } public void red(){ graphics2d.setpaint(color.red); repaint(); } public void black(){ graphics2d.setpaint(color.black); repaint(); } public void yellow(){ graphics2d.setpaint(color.yellow); repaint(); } public void blue(){ graphics2d.setpaint(color.blue); repaint(); } public void green(){ graphics2d.setpaint(color.green); repaint(); } public void small(){ graphics2d.setstroke(new basicstroke(1));; } public void medium(){ graphics2d.setstroke(new basicstroke(5));; } public void big(){ graphics2d.setstroke(new basicstroke(12));; } }
try follow single responsibility principle.
it means class should change 1 reason.
keeping code wrote in 1 class breaking principle. split in different classes each 1 doing 1 thing.
Comments
Post a Comment