Filip Piták
Filip Piták

Reputation: 9

Drawing in JLabel

i have a problem with drawing in my JFrame app. I have these two functions:

Im quite new to such graphics in Java, i was wondering if someone would be kind and help me. I need to add the line on the JLabel called areaImage. I tried using some done code i found here but none worked for me. Is my code usable with some bugs? Or is it completely bad?

Please dont just post a link with some code, Im not skilled enough to understand it and then change it so it fits my app...

This one just makes the window, adds the components:

public void game (int difficulty)
{
    getContentPane().removeAll();

    areaImage = new JLabel ();
    areaImage.setBounds (50,100,650,500);
    areaImage.setForeground(Color.WHITE);   
    areaImage.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
    add(areaImage);

    paint (100,120,500,500, null);

    info = new JLabel ("  Write your answer into the text field");
    info.setBounds(730,180,250,50);
    info.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
    info.setFont(new Font ("Arial", Font.PLAIN, 15));
    areaImage.setForeground(Color.red);
    add(info);

    inputField = new JTextField("");
    inputField.setBounds(810, 240, 80, 50);
    add(inputField);

    checkAnswer = new JButton ("Check");
    checkAnswer.setBounds(750, 330, 200, 50);
    checkAnswer.setContentAreaFilled(false);
    checkAnswer.setOpaque(false);
    checkAnswer.addActionListener(this);
    checkAnswer.setFont (new Font("Arial",Font.PLAIN, 30));
    add(checkAnswer);

    next = new JButton ("Next");
    next.setBounds(750,440,200,50);
    next.setContentAreaFilled(false);
    next.setOpaque(false);
    next.addActionListener(this);
    next.setFont (new Font("Arial",Font.PLAIN, 30));
    add(next);

    end= new JButton ("Exit");
    end.setBounds (750,550,200,50);
    end.setFont(new Font("Arial", Font.PLAIN, 30));
    end.addActionListener(this);
    end.setOpaque(false);
    end.setContentAreaFilled(false);
    add(end);

    revalidate();
    repaint();
}

This one is the drawing function:

private void paint (int x, int xx, int y, int yy, Graphics g)
{
   super.paint(g);
   Graphics2D g2 = (Graphics2D) g;
   g.drawLine(x,y,xx,yy);
   Line2D lin = new Line2D.Float(100, 100, 250, 260);
   g2.draw(lin);
}

Upvotes: 0

Views: 936

Answers (1)

camickr
camickr

Reputation: 324098

Please dont just post a link with some code, Im not skilled enough to understand it

Well that is how you learn. You can't expect us to debug your code and rewrite it every time you have a little problem. If you are not willing to learn by playing with working examples, then I'm not sure how we can help you.

I need to add the line on the JLabel

Then you need to override the painting code for the JLabel. You should never invoke a painting method directly because the painting will be lost the next time Swing determines the component needs to be repainted.

So start by learning how to do painting properly. There is a working example in the Swing tutorial on Custom Painting. The first example just draws a string on a panel but it will be easy enough for you do just draw a line on the label. It is a single statement you add to the paintComponent() method.

The real question is why are you trying to draw this line on a label? I'm sure there is a better solution if we understand the real requirement. You should not be hardcoding the location/size of the line since you don't know how big the label will be.

Upvotes: 3

Related Questions