Thread: JPanel Problems
View Single Post
  #6 (permalink)  
Old 10-13-2007, 02:42 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
when I resize it everything disappears
That doesn't happen with this.
You may have to change the name of InteractiveRectangle below to avoid name–clashing.
Or you could run this in another folder (separate from your current class files).
Code:
import java.awt.*; import javax.swing.*; public class SDF extends JFrame { private InteractiveRectangle iRect; private SDF sdFrame; public SDF(InteractiveRectangle iR) { sdFrame = this; iRect = iR; sdFrame.setTitle(iRect.getContigName()); SDisplayPanel sdPanel = new SDisplayPanel(iRect); sdFrame.add(sdPanel); sdFrame.pack(); } private class SDisplayPanel extends JPanel { private InteractiveRectangle intRect; private int paintY = 20; public SDisplayPanel(InteractiveRectangle iR) { intRect = iR; setPreferredSize(new Dimension(500,500)); setBackground(Color.WHITE); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; String tempString = intRect.getContigSeq(); int xDistance = 20; for(int i = 0; i < tempString.length(); i++) { if(i < (intRect.getStartOfUsedRegion() -1) || i > (intRect.getEndOfUsedRegion()-1)) g2.setColor(Color.BLACK); else g2.setColor(Color.RED); g2.drawString(String.valueOf(tempString.charAt(i)), xDistance, paintY); xDistance += 8; if(xDistance > 480) { xDistance = 20; paintY += 15; } } g2.setColor(Color.BLACK); paintY +=20; String totalLength = ("The total length is: " + String.valueOf(intRect.getTotalLength())); g2.drawString(totalLength, 20, paintY); } } public static void main(String[] args) { SDF sdf = new SDF(new InteractiveRectangle("hello world")); sdf.setLocation(200,200); sdf.setVisible(true); } } class InteractiveRectangle { String contigName; public InteractiveRectangle(String name) { contigName = name; } public String getContigName() { return contigName; } public String getContigSeq() { return "hello"; } public int getStartOfUsedRegion() { return 0; } public int getEndOfUsedRegion() { return 4; } public int getTotalLength() { return 5; } }
Reply With Quote