0

I am working on project where I would like to draw multiple text to applet on different locations in applet.

I am already able to draw a single text. But I can't figure it out how to draw or add another text to applet. When I draw the second one, first one gets removed.

Here is the code:

/**
 * Constructor
 */
public DrawText() {
    super();
}

public void init(IHandler handler) {
    super.init(handler);
    mHandler = handler;

    try {
        if (font == null) {
            AddFont myFont = new AddFont();
            font = myFont.createFont();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


@Override
public void paint(Graphics g) {
    if (g instanceof Graphics2D) {
        try {
            super.paintComponents(g);
            Graphics2D g2 = (Graphics2D) g;
            System.out.println("Izrisujem tekst 16");
            SetTitle(g2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


private void SetTitle(Graphics2D g) {
    if (_SetTitleText != null) {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Font FontNew;
        if (_SetTitleSize > 0) {
            FontNew = font.deriveFont(Font.PLAIN, _SetTitleSize);
        } else {
            FontNew = font.deriveFont(Font.PLAIN, 26);
        }
        if (_SetTitleColor != null) {
            String[] TitleColorArr = getRGB(_SetTitleColor);
            Color TitleColor = new Color(Integer.parseInt(TitleColorArr[0]), Integer.parseInt(TitleColorArr[1]), Integer.parseInt(TitleColorArr[2]));
            g.setColor(TitleColor);
        }
        g.setFont(FontNew);
        g.drawString(_SetTitleText, _SetTitleX, _SetTitleY);
    }
}

Thanks for help in advance.

Regards, Igor

-------------------------------------------------> UPDATE

I have update code to draw in JPanel but still no cigar

//Main class
public void init(IHandler handler) {
    super.init(handler);
    mHandler = handler;
    myPanel = new MainJP();
    myPanel.setVisible(true);
    this.add(myPanel);
}

JPanel:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    System.out.println("paintComponent");
    SetTitle(g);
}

public void SetTitle(Graphics g) {
    System.out.println("SetTitle");
    if (_TitleText != null) {
        Font FontNew;
        if (_TitleSize > 0) {
            FontNew = font.deriveFont(Font.PLAIN, _TitleSize);
        } else {
            FontNew = font.deriveFont(Font.PLAIN, 26);
        }
        if (_TitleColor != null) {
            String TitleColorArr = getRGB(_TitleColor);
            g.setColor(TitleColor);
        }
        g.setFont(FontNew);
        g.drawString(_TitleText, _TitleX, _TitleY);
    }
}

-------------------------------------------> UPDATE 1

I have used different approach. I am adding my text to array and draw it all over again when new item added to list.

    } else if (property == DRAW) {
        if (property == DRAW) {
            myTekstItem = new Tekst(_SetText, _SetTextBold, _SetTextItalic, _SetX, _SetY, _SetColor, _SetSize);
            int i = 0;
            if (TekstList.size() > 0) {

                for (Tekst TekstObj : TekstList) {
                    if (TekstObj.Tekst == _SetText && TekstObj.X == _SetX && TekstObj.Y == _SetY && TekstObj.Color == _SetColor && TekstObj.Size == _SetSize) {
                        i++;
                    }
                }
            }

            if (i == 0) {
                TekstList.add(myTekstItem);
            }
            this.repaint();
            _SetTextBold = false;
            _SetTextItalic = false;
        }

public void paint(Graphics g) {
    if (g instanceof Graphics2D) {
        try {
            Graphics2D g2 = (Graphics2D) g;
            for (Tekst TekstObj : TekstList) {
                SetText(g2, TekstObj.Tekst, TekstObj.Bold, TekstObj.Italic, TekstObj.X, TekstObj.Y, TekstObj.Color, TekstObj.Size);
            }
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    }
}

private void SetText(Graphics2D g, String pText, boolean pBold, boolean pItalic, int pX, int pY, String pColor, int pSize) {
    if (pText != null) {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Font FontNew;
        if (pSize > 0) {
            FontNew = font.deriveFont(Font.PLAIN, pSize);
        } else {
            FontNew = font.deriveFont(Font.PLAIN, 26);
        }
        if (pBold)
        {
            FontNew = font.deriveFont(Font.BOLD, FontNew.getSize());
        }
        if (pItalic)
        {
            FontNew = font.deriveFont(Font.ITALIC, FontNew.getSize());
        }
        if (pBold == false && pItalic == false)
        {
            FontNew = font.deriveFont(Font.PLAIN, FontNew.getSize());
        }

        if (pColor != null) {
            String[] TitleColorArr = getRGB(_SetColor);
            Color TitleColor = new Color(Integer.parseInt(TitleColorArr[0]), Integer.parseInt(TitleColorArr[1]), Integer.parseInt(TitleColorArr[2]));
            g.setColor(TitleColor);
        }
        g.setFont(FontNew);
        g.drawString(pText, pX, pY);
    }
}
Igor
  • 171
  • 4
  • 15
  • Calling `super.paintComponent` from `paint` is really, really bad – MadProgrammer Oct 14 '14 at 05:58
  • Since (at least Java 1.4), the `Graphics` context has been guaranteed to be an instance of `Graphics2D` – MadProgrammer Oct 14 '14 at 05:59
  • If I understand you every painting creates new instance of context? – Igor Oct 14 '14 at 06:06
  • The core system is responsible for creating the context, since (at least Java 1.4), it's guaranteed to be an instance of `Graphics2D`, so there is no need to check for it. You should be calling `super.paint` from `paint`, not `super.paintComponents`, in fact, I'd discourage you from painting directly to top level containers like this and would encourage you to paint to something like a `JPanel` from within in its `paintComponent` method instead and then an the `JPanel` to the applet – MadProgrammer Oct 14 '14 at 06:09
  • Thanks for suggestion. I will change super.paintComponents with super.paint but doubt this is solution. Woul be JPanel a solution and why? – Igor Oct 14 '14 at 06:16
  • It would be better because it would make your design more flexible and would also be double buffered by default - Also, remember, painting is destructive, each time `paint` is called, you are expected to repaint EVERYTHING that needs to be painted – MadProgrammer Oct 14 '14 at 06:18
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). – Andrew Thompson Oct 14 '14 at 07:04
  • I have updatet drawing to JPanel but the result is same. – Igor Oct 14 '14 at 08:46
  • Why do you think you should be getter more than one text string printed? – MadProgrammer Oct 14 '14 at 09:06
  • Cause I call JAR ->SetTitle from oracle forms two times – Igor Oct 14 '14 at 09:15
  • Click anywhere on this this [`AnimationTest`](http://stackoverflow.com/a/3256941/230513) to add text fields. – trashgod Oct 14 '14 at 09:58
  • I have updated my fist post with solution. Regards, Igor – Igor Oct 15 '14 at 11:55

1 Answers1

0

I have used different approach. I am adding my text to array and draw it all over again when new item added to list.

    } else if (property == DRAW) {
        if (property == DRAW) {
            myTekstItem = new Tekst(_SetText, _SetTextBold, _SetTextItalic, _SetX, _SetY, _SetColor, _SetSize);
            int i = 0;
            if (TekstList.size() > 0) {

                for (Tekst TekstObj : TekstList) {
                    if (TekstObj.Tekst == _SetText && TekstObj.X == _SetX && TekstObj.Y == _SetY && TekstObj.Color == _SetColor && TekstObj.Size == _SetSize) {
                        i++;
                    }
                }
            }

            if (i == 0) {
                TekstList.add(myTekstItem);
            }
            this.repaint();
            _SetTextBold = false;
            _SetTextItalic = false;
        }

public void paint(Graphics g) {
    if (g instanceof Graphics2D) {
        try {
            Graphics2D g2 = (Graphics2D) g;
            for (Tekst TekstObj : TekstList) {
                SetText(g2, TekstObj.Tekst, TekstObj.Bold, TekstObj.Italic, TekstObj.X, TekstObj.Y, TekstObj.Color, TekstObj.Size);
            }
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    }
}

private void SetText(Graphics2D g, String pText, boolean pBold, boolean pItalic, int pX, int pY, String pColor, int pSize) {
    if (pText != null) {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Font FontNew;
        if (pSize > 0) {
            FontNew = font.deriveFont(Font.PLAIN, pSize);
        } else {
            FontNew = font.deriveFont(Font.PLAIN, 26);
        }
        if (pBold)
        {
            FontNew = font.deriveFont(Font.BOLD, FontNew.getSize());
        }
        if (pItalic)
        {
            FontNew = font.deriveFont(Font.ITALIC, FontNew.getSize());
        }
        if (pBold == false && pItalic == false)
        {
            FontNew = font.deriveFont(Font.PLAIN, FontNew.getSize());
        }

        if (pColor != null) {
            String[] TitleColorArr = getRGB(_SetColor);
            Color TitleColor = new Color(Integer.parseInt(TitleColorArr[0]), Integer.parseInt(TitleColorArr[1]), Integer.parseInt(TitleColorArr[2]));
            g.setColor(TitleColor);
        }
        g.setFont(FontNew);
        g.drawString(pText, pX, pY);
    }
}
Igor
  • 171
  • 4
  • 15