图像未在特定索引点绘制。

huangapple go评论120阅读模式
英文:

Image is not getting drawn at the specific point index

问题

我正在尝试制作一个图形用户界面GUI程序在该程序中有一个点数组图像应在特定的点或索引处重新绘制代码如下-

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;

public class Trial {

    BufferedImage image = null;
    public JButton button;

    Trial(){        

        Point[] array = new Point[5];
        array[0] = new Point(150,200);

        button = new JButton("粘贴");
        button.setBounds(875, 525, 125, 50);

        try{
            image = ImageIO.read(new File("C:\\GUI Program\\src\\com\\company\\Square.PNG"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                java.awt.Graphics g = getGraphics();
                g.drawImage(image, array[0].x, array[0].y, null);
            }
        });

    }

    public static void main (String args[]){
        Trial trialObject = new Trial();
    }
}

图像没有在指定的点绘制出来。请帮我在特定索引处绘制图像。

我仍然是一个初学者程序员。所以任何答案对我来说都将是很大的帮助。

提前致谢。

英文:

I am trying to make a GUI program where there is an array of points and an image should be repainted at the specific point or index.The code is below:-

import java.awt.*;

public class Trial {

	BufferedImage image = null;
	public JButton button;

	Trial(){		

		Point[] array = new Point[5];
    		array[0] = new Point(150,200);

		button = new JButton("Paste");
            	button.setBounds(875, 525, 125, 50);

		try{
			image = ImageIO.read(new File("C:\\GUI Program\\src\\com\\company\\Square.PNG"));
		} catch (IOException e) {
                e.printStackTrace();
            	}

		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
                        java.awt.Graphics g = getGraphics();
                        g.drawImage(image, array[0].x, array[0].y, null);
            		});	
		}

	public static void main (String args[]){
		Trial trialObject = new Trial();
	}
}

The image is not being drawn at the point. Please help me draw the image at the specific index.

I am still a beginner programmer. So any answer would be a great help to me.

Thanks in advance.

答案1

得分: 1

你应该为自定义绘制创建一个自定义的 JPanel,然后重写 paintComponent() 方法进行绘制。阅读 Swing 教程中关于自定义绘制的部分以获取一些基础知识。

> 有一个点数组,并且需要在特定点重新绘制图像。

你应该创建一个自定义对象,其中包含绘制所需的所有信息。因此,在您的情况下,您需要两个属性:

  1. 点(Point)
  2. 图像(Image)

您将此自定义对象存储在 ArrayList 中。因此,您的类将需要一个 addCustomObject(...) 方法,将每个对象添加到 ArrayList 中。

然后在 paintComponent() 方法中,您通过迭代 ArrayList 并使用对象的属性绘制每个对象。

请查看自定义绘制方法中的在组件上绘制示例,以获取此方法的工作示例。

英文:

You should create a custom JPanel for the custom painting and then override the paintComponent() method to do the painting. Read the section from the Swing tutorial on Custom Painting for some basics.

> there is an array of points and an image should be repainted at the specific point

You should create a custom object that contains all the information needed to the the painting. So in your case you would need two properties:

  1. the Point
  2. the Image

You store this custom object in an ArrayList. So your class will need an addCustomObject(...) method to add each object to the ArrayList.

Then in the paintComponent() method you iterate through the ArrayList and paint each object using the properties of the object.

Check out the Draw On Component example found in Custom Painting Approaches for a working example of this approach.

答案2

得分: 0

我认为你应该创建一个JFrame和JPanel,并在其内部绘制图像。

JFrame f = new JFrame();
JPanel p = new JPanel()
{
    @Override
    public void paint(Graphics g) 
    {
        g.drawImage(image, array[0].x, array[0].y, null);
    }
};
f.add(p);
f.setVisible(true);
英文:

I think you should create a JFrame and JPanel and draw the image inside of it

JFrame f = new JFrame();
JPanel p = new JPanel()
{
    @Override
    public void paint(Graphics g) 
    {
        g.drawImage(image, array[0].x, array[0].y, null);
    }
};
f.add(p);
f.setVisible(true);

huangapple
  • 本文由 发表于 2020年10月19日 22:39:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64429644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定