0

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);
}
  • 1
    *"code i found here"* 'Here' is a big place. Please narrow it down with link(s). More generally: 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Aug 23 '16 at 13:08
  • Can you post a [SSCCE](http://sscce.org/)? This will help pin point the problem that you're facing. You might be able to figure this out yourself too in the process! – anacron Aug 23 '16 at 13:08
  • .. 3) `new Font ("Arial", Font.PLAIN, 15)` would better be `new Font (Font.SANS_SERIF, Font.PLAIN, 15)` because Arial is not available on OS X. They would prefer to see Helvetica in any case. Using the 'logical' font name would result in Arial on Windows, Helvetica on OS X and whatever sens serif font is default on *nix. – Andrew Thompson Aug 23 '16 at 13:09

1 Answers1

3

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Well, basically its a game where i have a cm dot paper, some random points are selected and connected by a line. The player has to calculate the area of the given shape. So I need to draw the dots and lines – Filip Piták Aug 25 '16 at 11:00