4

My problem is I want to draw a huge Panel but its not possible to see this panel in a small size frame so i supposed to use ScrollPane and I used it..

But by scrolling clashes occurs so i cant see any panel there .i just want to fix it

Please anyone see my code and run it and help to solve the problem

import java.awt.*;
import javax.swing.*;

public class Swing{
    JFrame frame;
    Panel panel;
    public static void main(String [] args){
        Swing a  = new Swing();
        a.go();
    }
    public void go(){
        frame  = new JFrame();
        panel = new Panel();
        panel.setPreferredSize(new Dimension(5000, 5000));
        JScrollPane scroll = new JScrollPane(panel);
        frame.add(scroll);
        frame.pack();
        frame.setVisible(true);
    }

    class Panel extends JPanel{
        public void paintComponent(Graphics g){
            Graphics2D a = (Graphics2D)g;
            a.setColor(Color.RED);
            a.drawLine(50, 50, 5000, 5000);
        }
    }
}

Thanks in advance!

mKorbel
  • 109,107
  • 18
  • 130
  • 305
user3148422
  • 85
  • 1
  • 12
  • 2
    As an aside, nice code example! It gets straight to the point, and (for those of us that understand Swing painting) makes it clear why it failed. – Andrew Thompson Dec 31 '13 at 06:13

1 Answers1

6

Always make sure to call super.paintComponent(g); to redraw the rest of the component. Otherwise these types of painting artifacts are seen.

import java.awt.*;
import javax.swing.*;

public class Swing{
    JFrame frame;
    Panel panel;
    public static void main(String [] args){
        Swing a  = new Swing();
        a.go();
    }
    public void go(){
        frame  = new JFrame();
        panel = new Panel();
        panel.setPreferredSize(new Dimension(5000, 5000));
        JScrollPane scroll = new JScrollPane(panel);
        frame.add(scroll);
        frame.pack();
        frame.setVisible(true);
    }

    class Panel extends JPanel{
        public void paintComponent(Graphics g){
            super.paintComponent(g);  // VERY IMPORTANT!
            Graphics2D a = (Graphics2D)g;
            a.setColor(Color.RED);
            a.drawLine(50, 50, 5000, 5000);
        }
    }
}
Paul Samsotha
  • 197,959
  • 33
  • 457
  • 689
Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
  • @MadProgrammer has alluded to a point that comes up often. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) - I will not edit the answer, since it is a 'one line change' to fix the immediate problem. But please read that thread carefully. I suspect in this situation, it would be better to override `getPreferredSize()` in the custom component. – Andrew Thompson Dec 31 '13 at 06:05
  • Glad you got it sorted. :) Please [accept](http://meta.stackexchange.com/a/65088/155831) the answer if it helped solve the problem. (That is about all the thanks we need.) – Andrew Thompson Dec 31 '13 at 06:12
  • 1
    @user3148422 you should give the check mark back to this answer. I Only added a suggestion. The `super.paintComponent` was your _main_ problem. – Paul Samsotha Dec 31 '13 at 06:16