简体   繁体   中英

line not showing on image jframe

package carspeedometer;

import java.awt.Graphics;

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

class a1 {

    a1() {
        JFrame jf = new JFrame("Speedometer");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel jp = new JPanel();
        JLabel jb = new JLabel(new ImageIcon(
                "C:/Users/Vinayak/Desktop/tester.jpg"));
        jp.add(jb);

        jf.add(jp);
        jf.setVisible(true);
        jf.setSize(700, 700);
    }

    public void paint(Graphics g) {
        g.drawLine(70, 70, 200, 200);
    }

    public static void main(String...s) {
        new a1();
    }
}

Line is not showing on screen.I want to show line on top of the image.please help. Here i am trying to build a speedometer but first a line needs to be displayed

You can only draw in Swing if you override a drawing method of a component. Here your paint method overrides nothing because your class extends nothing. I suggest

  • that you create a class that extends JPanel
  • that you override the JPanel's paintComponent(Graphics g) method
  • that you use the @Override annotation to verify the override
  • that you place the JPanel into a JFrame and display it and
  • that you read the Swing Painting Tutorials . You don't want to guess at this stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM