import java.awt.*; import javax.swing.*; public class ExtraPixelProblem extends JComponent { //////////////////////////////////////////////// // Paint //////////////////////////////////////////////// public void paint(Graphics page) { boolean useBrokenVersion = false; // <-- Edit to see broken/working version if (useBrokenVersion) brokenPaint(page); else fixedPaint(page); } //////////////////////////////////////////////// // Broken paint // Demonstrates the "extra pixel" or "thin white stripe" problem //////////////////////////////////////////////// public void brokenPaintStripe(Graphics page, int stripe, int stripes, Color color) { int height = getHeight()/stripes; int top = stripe * height; page.setColor(color); page.fillRect(0, top, getWidth(), height); page.setColor(Color.white); page.drawString("stripe #" + stripe + " height = " + height, 20, top+20); } public void brokenPaint(Graphics page) { page.setColor(Color.red); page.fillRect(0, 0, getWidth(), getHeight()); brokenPaintStripe(page, 0, 3, Color.black); brokenPaintStripe(page, 1, 3, Color.blue); brokenPaintStripe(page, 2, 3, Color.black); int height = getHeight(); int extraPixels = height % 3; String msg = ""; page.setColor((extraPixels == 0) ? Color.white : Color.red); if (extraPixels == 0) { page.setColor(Color.white); msg = "so there are no extra pixels (no red line below!)"; } else { page.setColor(Color.red); msg = "so there is a " + extraPixels + "-pixel red line below!"; } page.drawString("window height = " + height + " = (3*" + (height/3) + ") + " + extraPixels + ", " + msg, 20, height-15); } //////////////////////////////////////////////// // Fixed paint // Fixes the "extra pixel" or "thin white stripe" problem //////////////////////////////////////////////// public void fixedPaintStripe(Graphics page, int stripe, int stripes, Color color) { int top = stripe * getHeight()/stripes; int nextTop = (stripe+1) * getHeight()/stripes; int height = nextTop - top; page.setColor(color); page.fillRect(0, top, getWidth(), height); page.setColor(Color.white); page.drawString("stripe #" + stripe + " height = " + height, 20, top+20); } public void fixedPaint(Graphics page) { page.setColor(Color.red); page.fillRect(0, 0, getWidth(), getHeight()); fixedPaintStripe(page, 0, 3, Color.black); fixedPaintStripe(page, 1, 3, Color.blue); fixedPaintStripe(page, 2, 3, Color.black); int height = getHeight(); int extraPixels = height % 3; String msg = ""; page.setColor((extraPixels == 0) ? Color.white : Color.red); page.setColor(Color.white); page.drawString("window height = " + height, 20, height-15); } ////////////////////////////////////////////// /// END OF YOUR CODE /// /// (you may ignore all the code below!!! /// ////////////////////////////////////////////// public Dimension getPreferredSize() { int initialWidth = 500; int initialHeight = 302; // so there are 2 pixels left over! return new Dimension(initialWidth, initialHeight); } public static void main(String[] args) { JComponent jc = newInstance(); JFrame frame = new JFrame(jc.getClass().getName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel cp = new JPanel(); cp.setLayout(new BorderLayout()); cp.add(jc); frame.setContentPane(cp); frame.pack(); frame.setVisible(true); } // Returns an instance of this class as a JComponent. This is necessary so // students can rename this class without changing the "main" method's body. public static JComponent newInstance() { StackTraceElement[] trace = null; try { throw new RuntimeException(); } catch (Exception e) { trace = e.getStackTrace(); } try { return (JComponent)Class.forName(trace[0].getClassName()).newInstance(); } catch (Exception e) { return null; } } }