Home > OS >  Drawline not working with JPanel or JFrame
Drawline not working with JPanel or JFrame

Time:03-09

I cant seem to get the line to appear. I have a Background color and a few pictures. If I have frame.setSize(x, y); and frame.setVisible(true); then the outcome is as expected but without the line there. If I change the code and remove frame. from these two lines, leaving setSize(x,y); and setVisible(true);. I have tried using extends JPanel and extends JFrame but neither work. I have tried adding and removing @Override, paintComponent and g2d.drawLine.

It either one or the other, how do I get both to work?

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


public class CodeBreaker extends JPanel
{
    JFrame frame = new JFrame("Code Breaker!"); 
    Picture picture = new Picture("Empty.png"); 
    JLabel label = new JLabel(picture);
    JLabel label2 = new JLabel(picture);
    JLabel label3 = new JLabel(picture);
    JLabel label4 = new JLabel(picture);

    JLabel label5 = new JLabel(picture);
    JLabel label6 = new JLabel(picture);
    JLabel label7 = new JLabel(picture);
    JLabel label8 = new JLabel(picture);

    JPanel panel = new JPanel();
    JPanel panel2 = new JPanel();
    
    public CodeBreaker()
    {

        frame.setSize(600, 900); 
        frame.setContentPane(panel); 
        frame.add(panel2);
        frame.setResizable(true); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        frame.getContentPane().setBackground(new Color(141, 100, 21));  

        panel.add(label);
        panel.add(label2);
        panel.add(label3);
        panel.add(label4);
    
        panel2.add(label5);
        panel2.add(label6);
        panel2.add(label7);
        panel2.add(label8);
        
        panel2.setOpaque(false);  
        
        frame.setVisible(true); 
    }

    /*
    void drawLines(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
 
        g2d.drawLine(120, 50, 360, 50);
 
    }
    */

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.white);
        g.drawLine(120, 50, 360, 50);
    }
}

And this is my main:

public class CodeBreakerDriver
{
    public static void main(String[] args)
    {
        CodeBreaker cb = new CodeBreaker();
    }
}

CodePudding user response:

Introduction

Oracle has a helpful tutorial, enter image description here

You can see a black line in the lower right of the GUI.

Explanation

All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

Create the JFrame and JPanels in separate methods. This makes the code much easier to read and understand.

Use Swing layout managers. The JFrame has a default BorderLayout. I used a FlowLayout for both JPanels.

Code

Here's the complete runnable code. I made the Picture class an inner class so I could post the code as one block.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CodeBreakerGUI implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CodeBreakerGUI());
    }
    
    private final Picture picture;
    
    public CodeBreakerGUI() {
        this.picture = new Picture();
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Code Breaker!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createTopPanel(), BorderLayout.NORTH);
        frame.add(createBottomPanel(), BorderLayout.SOUTH);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    public JPanel createTopPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 25, 25));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        JLabel label = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label);
        
        JLabel label2 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label2);
        
        JLabel label3 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label3);
        
        JLabel label4 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label4);
        
        return panel;
    }
    
    public JPanel createBottomPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 25, 25)) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.black);
                g.drawLine(120, 50, 360, 50);
            }
        };
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        JLabel label5 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label5);
        
        JLabel label6 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label6);
        
        JLabel label7 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label7);
        
        JLabel label8 = new JLabel(new ImageIcon(picture.getEmptyImage()));
        panel.add(label8);
        
        return panel;
    }
    
    public class Picture {
        
        private final BufferedImage emptyImage;
        
        public Picture() {
            this.emptyImage = new BufferedImage(64, 64, BufferedImage.TYPE_INT_RGB);
        }

        public BufferedImage getEmptyImage() {
            return emptyImage;
        }
        
    }

}
  • Related