1

我是 Java 新手,所以感谢您的时间和帮助!

基本上我想让文本显示在我创建的图形上。基本上我做了一个奖杯,希望它在上面显示“#1”。

谁能帮助我如何做到这一点?再次感谢你

以下是我到目前为止编写的代码。您可以看到“#1”的位置已全部设置并完成,但它出现在图形后面。

import javax.swing.JApplet;
import java.awt.*; 
import java.net.*;
import javax.imageio.*;
import java.io.*;
import java.awt.Graphics2D;

public class BusinessCard extends JApplet
{

    /**
     * Paint method for applet.
     * 
     * @param  g   the Graphics object for this applet
     */
    public void paint(Graphics page)
    {
        //Variables used in rectangle
        int x = 0;
        int y = 0;
        int width = 500;
        int height = 300;



        page.drawRect(x, y, width, height);  //draws the rectangle using variables

        //Displays name
        Font f = new Font("Helvetica", Font.BOLD, 26);
        page.setFont(f);
        page.drawString ("anonymous", 300,100);

        //Displays company
        Font g = new Font("Helvetica", Font.PLAIN, 18);
        page.setFont(g);
        page.drawString ("blank", 320, 120);

        //Displays email
        Font h = new Font("Helvetica", Font.PLAIN, 15);
        page.setFont(h);
        page.drawString ("email", 315,140);

        //int for the logo
        final int MID = 350;
        final int TOP = 168;

        setBackground (Color.yellow);  //Revisit. For some reason it only turns yellow                   after you resize the window

        Font i = new Font("Helvetica", Font.BOLD, 16);
        page.setFont(i);
        page.drawString ("#1", MID+20, TOP+20);

        page.setColor (Color.orange);
        page.fillOval (MID, TOP, 60, 60); //bottom half of the trophy. the rounded part.
        page.drawArc (MID-8, TOP+15, 25, 25, 100, 160); //left arc
        page.drawArc (MID+43, TOP+15 , 25, 25, 280, 160); //right arc
        page.fillRect (MID+1, TOP+1, 59, 25); //make the top of the trophy flat basically
        page.fillRect (MID+22, TOP+60, 15, 25); //neck of the trophy
        page.drawLine (MID+48, TOP+84, MID+10, TOP+84); //base of the trophy


    }
4

3 回答 3

1

我认为你必须先画奖杯,然后再写文字!

无论您绘制什么,都会出现在控件的顶层。

于 2012-09-18T22:03:05.630 回答
1

首先,按照 sabre_raider 所说的去做。你以前确实想要它,但也必须修复其他东西

你在橙色上画橙色。您绘制奖杯,然后将页面背景设置为黄色,然后再次将“#1”绘制为橙色。更改setBackgroundpage.setColor以黄色绘制(这似乎是您想要的)。setBackground 设置小程序的背景,我假设这不是您想要的,而是用黄色绘制“#1”。如果您确实打算设置背景,请确保在Applet'init方法中进行设置!不是每次重画。(除此之外,在 init 中,设置了 setSize(500, 300) 以正确调整窗口大小以适应您的消息大小)

另外,我建议将其拆分为不同的方法,因为这只会使其更具可读性。以下是您的方法的一般外观:

/**
 * Paint method for applet.
 * 
 * @param g
 *            the Graphics object for this applet
 */
public void paint(Graphics page)
{
    drawBorder( page );
    drawText( page );
    drawTrophy( page, 1 );
}

private void drawBorder(Graphics page) {
    int x = 0;
    int y = 0;
    int width = 500;
    int height = 300;

    page.setColor( Color.black );
    page.drawRect( x, y, width, height );
}

private void drawText(Graphics page) {
    // Displays name
    Font f = new Font( "Helvetica", Font.BOLD, 26 );
    page.setFont( f );
    page.drawString( "anonymous", 300, 100 );

    // Displays company
    Font g = new Font( "Helvetica", Font.PLAIN, 18 );
    page.setFont( g );
    page.drawString( "blank", 320, 120 );

    // Displays email
    Font h = new Font( "Helvetica", Font.PLAIN, 15 );
    page.setFont( h );
    page.drawString( "email", 315, 140 );
}

private void drawTrophy(Graphics page, int number) {
    final int MID = 350;
    final int TOP = 168;
    page.setColor( Color.orange );
    page.fillOval( MID, TOP, 60, 60 ); // bottom half of the trophy. the rounded part.
    page.drawArc( MID - 8, TOP + 15, 25, 25, 100, 160 ); // left arc
    page.drawArc( MID + 43, TOP + 15, 25, 25, 280, 160 ); // right arc
    page.fillRect( MID + 1, TOP + 1, 59, 25 ); // make the top of the trophy flat basically
    page.fillRect( MID + 22, TOP + 60, 15, 25 ); // neck of the trophy
    page.drawLine( MID + 48, TOP + 84, MID + 10, TOP + 84 ); // base of the trophy

    Font i = new Font( "Helvetica", Font.BOLD, 16 );
    page.setColor( Color.yellow );
    page.setFont( i );
    page.drawString( "#" + number, MID + 20, TOP + 20 );
}
于 2012-09-18T22:11:44.423 回答
1

首先,我不会直接在顶级容器上绘画。我会使用类似 a 的东西JPanel来进行自定义绘画,然后将其添加到顶级容器中。

其次,您应该尽可能避免覆盖paint。如果您按照第一点的建议使用自定义组件,则应paintComponent改为使用。

第三,总是调用super.paint(或者super.paintComponent如果您使用的是自定义组件)。

第四,任何其他身体都是正确的。您应该尝试在图形之后绘制文本...

这是我使用的代码:

public class BusinessCard extends JApplet {

    /**
     * Paint method for applet.
     *
     * @param g the Graphics object for this applet
     */
    public void paint(Graphics page) {

        super.paint(page);

        //Variables used in rectangle
        int x = 0;
        int y = 0;
        int width = 500;
        int height = 300;



        page.drawRect(x, y, width, height);  //draws the rectangle using variables

        //int for the logo
        final int MID = 350;
        final int TOP = 168;

        page.setColor(Color.orange);
        page.fillOval(MID, TOP, 60, 60); //bottom half of the trophy. the rounded part.
        page.drawArc(MID - 8, TOP + 15, 25, 25, 100, 160); //left arc
        page.drawArc(MID + 43, TOP + 15, 25, 25, 280, 160); //right arc
        page.fillRect(MID + 1, TOP + 1, 59, 25); //make the top of the trophy flat basically
        page.fillRect(MID + 22, TOP + 60, 15, 25); //neck of the trophy
        page.drawLine(MID + 48, TOP + 84, MID + 10, TOP + 84); //base of the trophy

        page.setColor(Color.yellow);  //Revisit. For some reason it only turns yellow                   after you resize the window

        Font font = UIManager.getFont("Label.font");

        page.setFont(font.deriveFont(Font.BOLD, 16));
        page.drawString("#1", MID + 20, TOP + 20);

        //Displays name
        page.setFont(font.deriveFont(Font.BOLD, 26));
        page.drawString("anonymous", 300, 100);

        //Displays company
        page.setFont(font.deriveFont(Font.PLAIN, 18));
        page.drawString("blank", 320, 120);

        //Displays email
        page.setFont(font.deriveFont(Font.PLAIN, 15));
        page.drawString("email", 315, 140);


    }
}

它产生了

在此处输入图像描述

请注意,每个人都可能安装了您尝试使用的字体(您的代码导致小程序崩溃)

于 2012-09-18T23:01:41.770 回答