strings95
strings95

Reputation: 681

Drawing a line in java with no inheritance

i'm trying to draw a line in a java program but the line has not drawn

i have try every function but still no line on JLable

i don't know why the graphics of JLable does not updated after i draw my line and it's still empty.

help me please

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class t
{
private static JFrame frame;
private static JLabel field;

public static void main(String[] args)
{
    frame = new JFrame("Simple Server");
    frame.setLayout(new FlowLayout());

    frame.setPreferredSize(new Dimension(1200, 700));
    frame.setSize(new Dimension(1200, 700));
    frame.setMinimumSize(new Dimension(1200, 700));

    frame.addWindowListener(new WindowAdapter()
    {
        @Override
        public void windowClosing(WindowEvent we)
        {
            System.gc();
            System.exit(0);
        }
    });

    int maxW = 1000, maxH = 600;
    field = new JLabel();
    field.setSize(maxW, maxH);
    field.setPreferredSize(new Dimension(maxW, maxH));
    field.setMaximumSize(new Dimension(maxW, maxH));
    field.setMinimumSize(new Dimension(maxW, maxH));

    field.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
    field.setBackground(Color.GREEN);
    field.setOpaque(true);

    frame.add(field, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);

    Graphics g = field.getGraphics();
    g.drawLine(0, 0, 100, 100);

    field.paintComponents(g);
    field.paint(g);
    field.paintAll(g);
    field.update(g);
    field.repaint();

    frame.paint(g);
    frame.paintAll(g);
    frame.paintComponents(g);
    frame.update(g);
    frame.repaint();
    frame.setVisible(true);
}

Upvotes: 2

Views: 810

Answers (3)

G.Srinivas Kishan
G.Srinivas Kishan

Reputation: 438

Use an infinite while loop and within add the line "g.drawLine(0,0,100,100);" alone and then end the main function. Then a line is visible on screen. Similarly you can use a loop to play an animation until a condition is completed to make it work.

The code should be like this below one :-

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class t implements Runnable
{
private static JFrame frame;
private static JLabel field;
private java.lang.Thread tdr;
t()
{
 tdr = new java.lang.Thread(this);
}

public static void main(String[] args)
{
    t tsk= new t();
    frame = new JFrame("Simple Server");
    frame.setLayout(new FlowLayout());

    frame.setPreferredSize(new Dimension(1200, 700));
    frame.setSize(new Dimension(1200, 700));
    frame.setMinimumSize(new Dimension(1200, 700));

    frame.addWindowListener(new WindowAdapter()
    {
        @Override
        public void windowClosing(WindowEvent we)
        {
            System.gc();
            System.exit(0);
        }
    });

    int maxW = 1000, maxH = 600;
    field = new JLabel();
    field.setSize(maxW, maxH);
    field.setPreferredSize(new Dimension(maxW, maxH));
    field.setMaximumSize(new Dimension(maxW, maxH));
    field.setMinimumSize(new Dimension(maxW, maxH));

    field.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
    field.setBackground(Color.GREEN);
    field.setOpaque(true);

    frame.add(field, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);
    Graphics g = field.getGraphics();
 while(true)
 {
    g.drawLine(0, 0, 100, 100);
 try{

    tsk.tdr.sleep(1000);
 }
 catch ( Exception e)
 {
     System.out.println(e.getMessage());
 }
  System.out.println("Thread started");
}
}

    @Override
    public void run() {
       tdr.start();
    }
}

Similarly use some other condition in the while loop instead of true, which would meet until the animation like moving a circle form point a to b is finished.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347314

getGraphics can return null and is, at best, a snapshot of what was painted on the last paint cycle.

While you can use this technique, the next time the component needs to be painted, anything you've painted to it will be erased.

Take a look at Perofrming Custom Painting and Painting in AWT and Swing for more details about how painting works

Upvotes: 2

tbodt
tbodt

Reputation: 17007

To be swing graphics conformant, do this:

public class CrossedLabel extends JLabel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(0, 0, 100, 100);
    }
}

Upvotes: 1

Related Questions