java - drawString and Ellipse2D compatability issues -
i have gui using drawstring label rows of ellipse2d objects. problem both aren't displayed on same tabbedpane @ same time want them to.
question: why happening?
drawellipse.java
import javax.swing.*; import java.awt.*; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.geom.ellipse2d; import java.util.*; @suppresswarnings("serial") class drawellipses extends jpanel { private static final int oval_width = 30; private static final color inactive_color = color.red; private static final color active_color = color.green; private java.util.list<point> points; private java.util.list<ellipse2d> ellipses = new arraylist<>(); private map<ellipse2d, color> ellipsecolormap = new hashmap<>(); /* * method used populate ellipses initialized ellipse2d */ public drawellipses(java.util.list<point> points) { jlabel outbound = new jlabel("<html><font size=6>external port</font></html>"); this.points = points; (point p : points) { int x = p.x - oval_width / 2; int y = p.y - oval_width / 2; int w = oval_width; int h = oval_width; ellipse2d ellipse = new ellipse2d.double(x, y, w, h); ellipses.add(ellipse); ellipsecolormap.put(ellipse, inactive_color); } mymouseadapter mlistener = new mymouseadapter(); addmouselistener(mlistener); addmousemotionlistener(mlistener); add(outbound); } /* * paintcomponent used paint ellipses */ @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g; g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); (ellipse2d ellipse : ellipses) { g2.setcolor(ellipsecolormap.get(ellipse)); g2.fill(ellipse); } } /* * mouseadapter extended mousepressed event detects if x, y coordinates * of drawn ellipse clicked. if color inactive changed active , * vice versa. */ private class mymouseadapter extends mouseadapter { @override /* * when mousepressed event occurs, color toggled between active , inactive */ public void mousepressed(mouseevent e) { color c = null; (ellipse2d ellipse : ellipses) { if (ellipse.contains(e.getpoint())) { c = (ellipsecolormap.get(ellipse) == inactive_color) ? active_color : inactive_color; ellipsecolormap.put(ellipse, c); } } repaint(); } } /* *used button click action change ellipses active_color */ public void activateall(){ (ellipse2d ellipse : ellipses){ ellipsecolormap.put(ellipse, active_color); } repaint(); } /* *used button click action change ellipses inactive_color */ public void deactivateall(){ (ellipse2d ellipse : ellipses){ ellipsecolormap.put(ellipse, inactive_color); } repaint(); } }
drawstring.java
import javax.swing.*; import java.awt.*; class drawstring extends jpanel { private static final color string_color = color.black; public drawstring() {} @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(string_color); g.drawstring("1", 40, 80); g.drawstring("2", 40, 160); } }
the problem layout manager. layout manager responsible setting size/location of each component on panel.
also, when custom painting need override getpreferredsize()
method of each component layout manager can use information set size/location of each component. if size (0, 0) there nothing paint.
read section swing tutorial on custom painting more information , working examples.
if need more post proper sscce demonstrates problem. code posted in last question not sscce. don't need 50 points demonstrate concept. don't need actions since not related alignment of components. point of sscce simplify code.
Comments
Post a Comment