Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm guessing that you're trying to draw to a JPanel and then expect that the JPanel should get larger to accommodate your drawing, and that not only is this not working for you, but your losing the drawing if you call repaint() on the JPanel.</p> <p>If so, then,</p> <ul> <li>Just because you draw beyond the size of a JPanel or other JComponent will not automatically make that component larger since the component itself has no "knowledge" that you're drawing beyond its bounds. If you want to resize a component, you have to do this yourself.</li> <li>It is best to have the JPanel set its own size by overriding its <code>getPreferredSize()</code> method. This will prevent other code from resetting its size to something else.</li> <li>You do not want to get a component's graphics by calling <code>getGraphics()</code> on it. The Graphics object so obtained is short lasting and anything drawn will disappear as soon as the next time that paint(...) or paintComponent() is called. It's OK to call getGraphics() on a BufferedImage, but not on a Component or JComponent (unless you have a darn good reason for doing so and know how to protect against the Graphics object going null).</li> <li>Override JPanel or some other JComponent and draw in the <code>paintComponent(...)</code> override method of this component. If you're using a BufferedImage, then you draw the BufferedImage in this paintComponent method.</li> </ul> <p>For example:</p> <pre><code>import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.image.BufferedImage; import javax.swing.*; @SuppressWarnings("serial") public class TestDrawing extends JPanel { private static final int PREF_W = 800; private static final int PREF_H = 600; private DrawingPanel drawingPanel = new DrawingPanel(); public TestDrawing() { JPanel northPanel = new JPanel(); northPanel.add(new JButton(new AbstractAction("Draw Lines") { @Override public void actionPerformed(ActionEvent arg0) { drawingPanel.drawLines(); drawingPanel.repaint(); } })); setLayout(new BorderLayout()); add(new JScrollPane(drawingPanel)); add(northPanel, BorderLayout.PAGE_START); } @Override public Dimension getPreferredSize() { return new Dimension(PREF_W, PREF_H); } private static void createAndShowGui() { TestDrawing mainPanel = new TestDrawing(); JFrame frame = new JFrame("TestDrawing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } class DrawingPanel extends JPanel { private static final int BI_W = 500; private static final int FLOOR = 22; private static final int FLO_H = 100; private BufferedImage img = new BufferedImage(BI_W, FLO_H * FLOOR, BufferedImage.TYPE_INT_ARGB); public DrawingPanel() { } public void drawLines() { Graphics g = img.getGraphics(); g.setColor(Color.black); for (int i = 0; i &lt; FLOOR; i++) { g.drawLine(0, i * FLO_H, 300, i * FLO_H); g.setColor(Color.RED); } g.dispose(); } @Override public Dimension getPreferredSize() { return new Dimension(BI_W, FLO_H * FLOOR); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (img != null) { g.drawImage(img, 0, 0, this); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload