将图形添加到Java小程序
我在工作的项目,我想画多个文本的小程序在不同的位置在小程序 我已经能够画出一个文本了。但我不知道如何在applet中绘制或添加另一个文本。当我画第二个的时候,第一个被移除了 代码如下:将图形添加到Java小程序,java,swing,applet,Java,Swing,Applet,我在工作的项目,我想画多个文本的小程序在不同的位置在小程序 我已经能够画出一个文本了。但我不知道如何在applet中绘制或添加另一个文本。当我画第二个的时候,第一个被移除了 代码如下: /** * Constructor */ public DrawText() { super(); } public void init(IHandler handler) { super.init(handler); mHandler = handler; try {
/**
* 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);
}
}
} 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);
}
}
我使用了不同的方法。我将文本添加到数组中,并在列表中添加新项时重新绘制
} 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);
}
}
} 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);
}
}
从paint调用super.paintComponent真的很糟糕,因为至少Java 1.4,图形上下文已经被保证是Graphics2DIf的一个实例。据我所知,每幅绘画都会创建一个新的上下文实例?核心系统负责创建上下文,因为至少Java 1.4,它保证是Graphics2D的一个实例,因此不需要检查它。您应该从paint调用super.paint,而不是super.paintComponents,事实上,我不鼓励您直接绘制顶级容器,并鼓励您在其paintComponent方法中绘制类似于JPanel的内容,然后将JPanel绘制到appletThanks以获取建议。我将用super.paint更改super.paintComponents,但怀疑这是解决方案。JPanel会是一个解决方案吗?为什么?