paint() in java with no display

java repaint not working
java paint
java paint program example
java paintcomponent
java graphics
java graphics moving objects
java canvas
java paint method not working
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;




class game extends JFrame {
    public game(){   //this is constructor
        JFrame frame  = new JFrame();
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setTitle("Hello world");
    }

    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        Line2D line = new Line2D.Double(60,90,150,100);
        g2.draw(line);


    }


    public static void main(String args[]) {

        game l = new game();

    }


}

The above code is compiling in java but on running the code it only displays the frame and its title, but does not include any of the lines being drawn using the Graphics2D and Line2D, what is the mistake that is being made??? The frame being displayed does not show any content, why is that???


First, you are creating and displaying a JFrame which is not an instance of game, so there is no chance that it paints what you have in the paint method of game .

You usually don't want to create a subclass of JFrame for custom painting anyway, just create a subclass of JPanel, and set it as the content pane of the frame.

Also don't override paint, but paintComponent, which is the method responsible for painting the current component.

You should also call the parent method of paintComponent, to make sure that all the usual cleaning takes place correctly.

Also by convention, class names should start with an upper case letter.

One last thing, make the frame visible only once you have added all your components, or you may encounter visual glitches some day.

Putting it all together :

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

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

class Game extends JPanel {

    @Override
    public void paintComponent(final Graphics g) {

        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        Line2D line = new Line2D.Double(60, 90, 150, 100);
        g2.draw(line);

    }

    public static void main(final String args[]) {

        Game l = new Game();

        JFrame frame = new JFrame();

        frame.setSize(500, 500);
        frame.setTitle("Hello world");

        frame.setContentPane(l);

        frame.setVisible(true);

    }

}

Object-Oriented Programming and Java, So far, we have only seen the paint() method which is called in response to a repaint The other situation that requires a display update is not caused by state​  The less you paint, the faster it will be. If only part of your component needs to be updated, make paint requests using a version of repaint that specifies the painting region. For help on choosing efficient painting techniques, look for the string "performance" in the Java Media APIs home page.


In your constructor call method of JFrame class using this keyword because you extends JFrame class in your class.

public game(){   //this is constructor
/*JFrame frame  = new JFrame();
frame.setVisible(true);
frame.setSize(500,500);
frame.setTitle("Hello world");*/
    this.setVisible(true);
    this.setSize(500,500);
    this.setTitle("Hello world");
}

This solve your problem.

Solving Common Painting Problems (The Java™ Tutorials , Make sure your component is opaque. JPanel s, for example, are opaque by default in many but not all look and feels. To make components such as JLabel s​  The paint() method is called from an update() method, and is responsible for actually drawing the graphics. The method's sole argument is an instance of the Graphics class. The default implementation provided by class Component does nothing. How components are repainted. To reduce the time required to repaint the display, the AWT takes two


you dont need to create instance of JFrame class , modified you constructor as shown below

public game(){   //this is constructor
   setVisible(true);
   setSize(500,500);
   setTitle("Hello world");
}

Advanced Java Game Programming, public void paint ( JComponent component, Graphics2D graphics2D ) ( component, graphics2D ); } The update() and paint() methods are straightforward. Keep in mind that using full-screen exclusive mode does not necessarily improve  Java Swing | JPanel with examples JPanel, a part of Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organisation of components, however it does not have a title bar.


Programming Wireless Devices with the Java 2 Platform: Micro Edition, When the paint method is called, the clip region of the provided Graphics object need not separately buffer the drawing to get a clean update of the display. java.awt.Graphics.drawLine(int x1, int y1, int x2, int y2) In this code (x1, y1) is the start point of the line, and (x2, y2) is the end point of the line. So the code to draw a horizontal line is as follows: Graphics.drawLine(20, 100, 120, 100); The demo below accumulates all mentioned techniques. Move the slider to display various weather types.


Painting in AWT and Swing, To learn how to render nice graphics, visit the Java 2D Web site. it relatively painless for a graphical user interface (GUI) to render the right bits to the screen at the right time. It is not recommended that programs invoke paint() directly. The next picture shows compound borders. With compound borders, you can combine any two borders, which can themselves be compound borders. Using the Borders Provided by Swing. The code that follows shows how to create and set the borders you saw in the preceding figures. You can find the program's code in BorderDemo.java.


American Paint and Oil Dealer , These little rhymes will make catchy copy for show cards, newspaper ads, etc. window display, plus 'being on the job' equals more paint sales"——that's what Walter D. mm of lhi' many can renew \lih Java-[Ar Get a can today and m it work​  createContext in interface Paint Parameters: cm - the preferred ColorModel which represents the most convenient format for the caller to receive the pixel data, or null if there is no preference. r - the device space bounding box of the graphics primitive being rendered.