Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

paintComponent doesn't draw on JPanel

I want to add a few lines with labels(JLabel) on JPanel (added to JScrollPane). Earlier when I tried adding lines and label on the frame directly it worked (means lines and labels were visible) but after adding JScrollPane and Jpanel, the lines are not visible but labels are.

Any idea what could be wrong here?

public class gui {

static JFrame frame;
JPanel panel;

ArrayList<Line2D.Double> lines;

public gui() {
    panel = new JPanel();
    lines = new ArrayList<Line2D.Double>();
    
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            
            frame = new JFrame();
            
            addLine(10,10, 200,300);
            panel.repaint();
            
            panel.add(new test());
            
            panel.setPreferredSize(new Dimension(1200, 800));
        
            panel.setLayout(new BorderLayout());
            panel.setBackground(Color.WHITE);
            // Add scrollbar
            JScrollPane scrollBar=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
            scrollBar.setPreferredSize(panel.getPreferredSize());
            
            frame.getContentPane().add(scrollBar);
            
            frame.getContentPane().validate();

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.pack();
            frame.setVisible(true);
        }
    });
}    


public void addLine(int x1, int x2, int x3, int x4) {
    lines.add(new Line2D.Double(x1, x2, x3, x4));
}

public class test extends JPanel {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(1200, 800);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(Color.BLACK);
        for (Line2D.Double line : lines) {
            drawArrow(g2d,
                (int)line.getX1(),
                (int)line.getY1(),
                (int)line.getX2(),
                (int)line.getY2()
                );
        }
    }
    
    void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
        Graphics2D g = (Graphics2D) g1.create();
        System.out.println("here");
        double dx = x2 - x1, dy = y2 - y1;
        double angle = Math.atan2(dy, dx);
        int len = (int) Math.sqrt(dx*dx + dy*dy);
        AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
        at.concatenate(AffineTransform.getRotateInstance(angle));
        g.transform(at);

        // Draw horizontal arrow starting in (0, 0)
        g.drawLine(0, 0, len, 0);
        g.fillPolygon(new int[] {len, len-ARR_SIZE, len-ARR_SIZE, len},
                      new int[] {0, -ARR_SIZE, ARR_SIZE, 0}, 4);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Got it worked by replacing the 'panel = new JPanel();'

with panel = new test();


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...