简体   繁体   English

如何在Java Applet中创建图形实例

[英]How to create graphic instances in a java applet

We've just learned how to create our own class, and this particular assignment we had to work with graphics. 我们刚刚学习了如何创建我们自己的类,而这个特殊的任务我们必须处理图形。 We had to draw a crayon, and then create a test program where there are 5 crayons lined up next to one another (so we just change the color and x,y of each one). 我们必须画一个蜡笔,然后创建一个测试程序,其中有5个蜡笔彼此相邻排列(因此我们只需更改每个蜡笔的颜色和x,y)。 I know how to change the color and x,y coords, but my question is... 我知道如何更改颜色和x,y坐标,但是我的问题是...

how do I 'print' each crayon? 如何“打印”每支蜡笔? Yes, it's an applet and yes I know I need a .html file. 是的,它是一个applet,是的,我知道我需要一个.html文件。 But what exactly goes in the test program in order for the crayon to show up when I run the .html file? 但是,在我运行.html文件时,为了使蜡笔显示在测试程序中究竟是什么? I've run non-applets before in test programs using System.out.println , but never any graphics. 我以前在使用System.out.println测试程序中运行过非小程序,但从未运行过任何图形。 Would it just be System.out.println(Crayon); 只是System.out.println(Crayon); ?

Also, how do I get multiple crayons? 另外,如何获得多个蜡笔? I'm assuming it's Crayon crayons = new Crayon ;, and then the next one might be 'Crayon crayons2 = new Crayons;`? 我假设它是Crayon crayons = new Crayon ;然后下一个可能是'蜡笔蜡笔2 =新蜡笔;`? I'm not sure. 我不确定。

The x,y coordinates need to be modified w/each crayon, but the UML for the assignment told me not to make them instance variables but instead to put it in 'public void paint (Graphics g, int x, int y)'. 需要用每个蜡笔修改x,y坐标,但是分配的UML告诉我不要让它们成为实例变量,而要放在“公共无效绘画(图形g,int x,int y)”中。 What I have so far for the test program (may or may not be correct): 到目前为止,我对测试程序有什么(可能不正确):

import javax.swing.JApplet;
import java.awt.*;

public class BoxOfCrayons extends JApplet {
   Crayon first = new Crayon (Color.red, 50, 250)

Start by having a read through 2D Graphics . 首先阅读2D图形

Basically, you will need to create some kind of list of Cryon s. 基本上,您将需要创建某种类型的Cryon列表。 This can either be a Collection or array, depending on what you know. 这可以是Collection或数组,具体取决于您所知道的。 I would personally use a ArrayList as it's flexible and easy to use, but doesn't suffer from the same constraints as an array. 我个人会使用ArrayList因为它灵活且易于使用,但不会像数组那样受到相同的约束。

Next, create you're self a custom component (ie BoxOfCryons ) which extends from JPanel . 接下来,创建一个自定义组件(即BoxOfCryons ),该组件从JPanel扩展。 Override this classes paintComponent method. 重写此类的paintComponent方法。 Within this method, run through the list of Cryon s and paint each one, incrementing the x offset by the width of the last Cryon . 在此方法中,遍历Cryon的列表并绘制每个,将x偏移量增加最后一个Cryon的宽度。

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    int x = 0;
    int y = 0;
    for (Crayon crayon : cryons) {
        crayon.paint(g2d, x, y);
        x += crayon.getWidth();
    }
    g2d.dispose();
}

Create yourself a new class that extends from JApplet . 创建一个从JApplet扩展的新类。 In it's init method, set the applets layout manager to BorderLayout and add an instance of the BoxOfCryons class to it. 在它的init方法中,将applets布局管理器设置为BorderLayout然后向其添加BoxOfCryons类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM