Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the painted line in the following code does not appear on Jframe, is there something wrong with paintComponent method

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Gf extends JPanel {

  public void paintComponent(Graphics g) {
    super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(new Color(0, 0, 139));

      g2d.drawLine(10, 10, 100, 10);
    }
  

public static void main(String[] args) {
    Gf points = new Gf();
    JFrame f = new JFrame("Points"); f.setUndecorated(true);f.setResizable(false);f.setAlwaysOnTop( true );f.getContentPane().setBackground(Color.BLACK);
Image icon =Toolkit.getDefaultToolkit().getImage("somelocation");f.setIconImage(icon);		
    f.add(points);
    f.setSize(250, 200);
    f.setLocationRelativeTo(null);f.setLayout(null);
    f.setVisible(true);



  
  }
}


What I have tried:

changinf Jfram with a Jpanel doesnt help or changing colours dont help either.
Posted
Updated 18-Sep-22 18:51pm

1 solution

public static void main(String[] args) {
    Gf points = new Gf();
    JFrame f = new JFrame("Points"); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setUndecorated(true);
    f.setResizable(false);
    f.setAlwaysOnTop( true );
    
    f.getContentPane().setBackground(Color.BLACK);//no need for this becauise Panel will take the show
    //Image icon =Toolkit.getDefaultToolkit().getImage("somelocation");
    //f.setIconImage(icon);		
    f.getContentPane().add(points);
    f.setBounds(0,0,250, 200);
    f.setVisible(true);
  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900