将绘图添加到Java applet

时间:2014-10-14 05:53:01

标签: java swing applet

我正在开发项目,我想在applet中的不同位置绘制多个文本到applet。

我已经可以绘制单个文本了。但我无法弄明白如何绘制或添加另一个文本到applet。当我绘制第二个时,第一个被删除。

以下是代码:

/**
 * 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);
    }
}

提前感谢您的帮助。

此致 伊戈尔

----------------------------------------------- - >更新

我有更新代码在JPanel中绘制但仍然没有雪茄

//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);
    }
}

------------------------------------------->更新1

我使用了不同的方法。我将文本添加到数组中,并在将新项目添加到列表时再次绘制它。

    } 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);
    }
}

1 个答案:

答案 0 :(得分:0)

我使用了不同的方法。我将文本添加到数组中,并在将新项目添加到列表时再次绘制它。

    } 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);
    }
}