Saturday, 15 February 2014

change size rounded button in java -



change size rounded button in java -

hi programming game , thought rounded button nice curious how alter size of said button bigger? here code

class="snippet-code-html lang-html prettyprint-override">public javaapplication3(string label) { super(label); dimension size = getpreferredsize(); size.width = size.height = math.max(size.width,size.height); setpreferredsize(size); setcontentareafilled(false); } protected void paintcomponent(graphics g) { if (getmodel().isarmed()) { g.setcolor(color.red); } else { g.setcolor(color.yellow); } g.filloval(0, 0, getsize().width-1,getsize().height-1); super.paintcomponent(g); } protected void paintborder(graphics g) { g.setcolor(getforeground()); g.drawoval(0, 0, getsize().width-1, getsize().height-1); } shape shape; public boolean contains(int x, int y) { if (shape == null || !shape.getbounds().equals(getbounds())) { shape = new ellipse2d.float(0, 0, getwidth(), getheight()); } homecoming shape.contains(x, y); }

hope can help :)

this changes size of button every time click on it.

addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { system.out.println("klick"); setsize(new dimension(newsizewidth, newsizeheight)); } }); }

just phone call

setsize(new dimension(newsizewidth, newsizeheight));

method.

here working code:

import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.shape; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.geom.ellipse2d; import javax.swing.jbutton; import javax.swing.jframe; public class test2 extends jbutton { int width = 100; int height = 100; public test2(string label) { super(label); setsize(width, height); setcontentareafilled(false); addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { system.out.println("klick"); setsize(new dimension(width--, height--)); } }); } protected void paintcomponent(graphics g) { if (getmodel().isarmed()) { system.out.println(width + " " + height); g.setcolor(color.red); } else { g.setcolor(color.yellow); } g.filloval(0, 0, getsize().width - 1, getsize().height - 1); super.paintcomponent(g); } protected void paintborder(graphics g) { g.setcolor(getforeground()); g.drawoval(0, 0, getsize().width - 1, getsize().height - 1); } shape shape; public boolean contains(int x, int y) { if (shape == null || !shape.getbounds().equals(getbounds())) { shape = new ellipse2d.float(0, 0, getwidth(), getheight()); } homecoming shape.contains(x, y); } static jframe f; public static void main(string[] args) { f = new jframe(); f.setdefaultcloseoperation(jframe.exit_on_close); f.setlocationrelativeto(null); f.setsize(200, 200); f.add(new test2("label")); f.setlayout(new borderlayout()); f.setvisible(true); } }

java size jbutton shape

No comments:

Post a Comment