存储图像并在不同的类中显示

发布于 2024-12-26 11:32:11 字数 985 浏览 2 评论 0原文

我试图在“GetImage”类中获取多个图像,并将它们显示在主类中。

有人能给我举个例子吗?我尝试了很多其他示例,但它们不起作用,因为我有两节课。

这是我尝试过的一个。

主类:

import java.awt.*;
import hsa.*;

public class Test
{
static Console c;  

    public void Display()
    {
        GetImage c = new GetImage();
        c.paint(g);
    }

    public Test()
    {
        c = new Console ();
    }

    public static void main (String[] args) throws Exception
    {
        Test = new Test();
        a.Display();

    }

}

单独类:

import java.awt.*;
import hsa.Console;
import java.awt.event.*;

public class GetImage extends Frame
{

    Image image;
    String imageName = "ImageFileName.jpg";

    public void paint (Graphics g)
    {
        Toolkit tool = Toolkit.getDefaultToolkit ();
        image = tool.getImage (imageName);
        g.drawImage (image, 30, 30, this); // location of the image
        g.drawString (imageName, 100, 50); // location of the name
    }
}

I'm trying to get mutiple images in 'GetImage' class, and disply them in the main class.

Can anybody show me an example how to do it?? I tried bunch of other samples but they didn't work since I have two classes.

Here is one that I tried.

main clss:

import java.awt.*;
import hsa.*;

public class Test
{
static Console c;  

    public void Display()
    {
        GetImage c = new GetImage();
        c.paint(g);
    }

    public Test()
    {
        c = new Console ();
    }

    public static void main (String[] args) throws Exception
    {
        Test = new Test();
        a.Display();

    }

}

seperate class:

import java.awt.*;
import hsa.Console;
import java.awt.event.*;

public class GetImage extends Frame
{

    Image image;
    String imageName = "ImageFileName.jpg";

    public void paint (Graphics g)
    {
        Toolkit tool = Toolkit.getDefaultToolkit ();
        image = tool.getImage (imageName);
        g.drawImage (image, 30, 30, this); // location of the image
        g.drawString (imageName, 100, 50); // location of the name
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2025-01-02 11:32:11

我对 hsa 软件包不太熟悉,但快速谷歌搜索后发现,这是某家公司的教育软件包,该公司现已停业,如果我错了,请纠正我。因此,就我个人而言,如果可以的话,我会尽量避免使用他们的任何东西。

如果您必须在学校或其他地方使用它,那么您可能希望完全坚持使用他们的软件包,而不是将 hsa 与 awt 混合搭配。像这样的东西可能会实现你想要的,但我又不熟悉 hsa 包。

import java.awt.*;
import hsa.*;

public class Test
{
    static Console c;  

    public void Display()
    {
        GetImage gI = new GetImage(c,25,80,12);
    }

    public Test()
    {
        c = new Console ();
    }

    public static void main (String[] args) throws Exception
    {
        Test = new Test();
        a.Display();

    }

}


import java.awt.*;
import hsa.ConsoleCanvasGraphics;
import java.awt.event.*;

public class GetImage extends ConsoleCanvasGraphics
{

    Image image, image2;
    String imageName = "ImageFileName.jpg", image2Name = "Image2FileName.jpg";
    public GetImage(ConsoleParent parent, int rows, int columns, int fontSize)
    {
        Toolkit tool = Toolkit.getDefaultToolkit ();
        image = tool.getImage (imageName);
        image2 = tool.getImage (image2Name);
        super(parent,rows,columns,fontSize);
        drawImage(image,30,30,this);
        drawImage(image2,30,60,this);
        drawString(imageName,100,50,new Font("TimesRoman", Font.PLAIN,  20),Color.BLACK);
        drawString(image2Name,100,80,new Font("TimesRoman", Font.PLAIN,  20),Color.BLACK);
    }

}

再说一次,我自己会尽量避免使用 hsa,但如果您打算使用它并且需要在程序中拥有两个单独的类,那么上面应该是可能有效的内容的粗略概述。

I'm not very familiar with the hsa package, but some quick googling says it's an educational package from some company that has since gone out of business, correct me if I'm wrong. So personally I'd try to avoid using any of their stuff if you can.

If you have to use this for school or something then you probably want to stick entirely with their package instead of mix and matching hsa with awt. Something like this might accomplish what you want, but again I'm not familiar with the hsa package.

import java.awt.*;
import hsa.*;

public class Test
{
    static Console c;  

    public void Display()
    {
        GetImage gI = new GetImage(c,25,80,12);
    }

    public Test()
    {
        c = new Console ();
    }

    public static void main (String[] args) throws Exception
    {
        Test = new Test();
        a.Display();

    }

}


import java.awt.*;
import hsa.ConsoleCanvasGraphics;
import java.awt.event.*;

public class GetImage extends ConsoleCanvasGraphics
{

    Image image, image2;
    String imageName = "ImageFileName.jpg", image2Name = "Image2FileName.jpg";
    public GetImage(ConsoleParent parent, int rows, int columns, int fontSize)
    {
        Toolkit tool = Toolkit.getDefaultToolkit ();
        image = tool.getImage (imageName);
        image2 = tool.getImage (image2Name);
        super(parent,rows,columns,fontSize);
        drawImage(image,30,30,this);
        drawImage(image2,30,60,this);
        drawString(imageName,100,50,new Font("TimesRoman", Font.PLAIN,  20),Color.BLACK);
        drawString(image2Name,100,80,new Font("TimesRoman", Font.PLAIN,  20),Color.BLACK);
    }

}

Again, I'd try to avoid hsa myself, but if you're set on using it and need to have two separate classes in your program then the above should be a rough outline of something that might work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文