Java在swing/awt中制作“点/像素”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4216568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:40:55  来源:igfitidea点击:

Java making a 'dot/pixel' In swing/awt

java

提问by James Rattray

I want to know how to make a dot/pixel at a certain x,y co-ordinate on my JFrame.

我想知道如何在我的 JFrame 上的某个 x,y 坐标处制作一个点/像素。

Anyone know some simple code for this?

有人知道一些简单的代码吗?

采纳答案by Prine

I have created a small example program:

我创建了一个小示例程序:

public class Test extends JFrame {

    public Test() {
        this.setPreferredSize(new Dimension(400, 400));
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        // define the position
        int locX = 200;
        int locY = 200;

        // draw a line (there is no drawPoint..)
        g.drawLine(locX, locY, locX, locY); 
    }

    public static void main(String[] args) {
        Test test = new Test(); 
    }
}

You could also use the update or paintComponents method which would be much nicer. But then you have to make sure, that it gets called. If you have problems and it does not get called you could use the following solution: Why is paint()/paintComponent() never called?

您还可以使用更佳的 update 或paintComponents 方法。但是你必须确保它被调用。如果您遇到问题并且没有被调用,您可以使用以下解决方案:为什么从不调用paint()/paintComponent()?

回答by Curtis

Best compromise between simplicity and usefulness would probably be to extend JPanel, and override paintComponent( Graphics ). Then place that panel in your JFrame (with an appropriate layout. There are some usage notes here: http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JComponent.html#paintComponent%28java.awt.Graphics%29

简单性和实用性之间的最佳折衷可能是扩展 JPanel,并覆盖paintComponent( Graphics )。然后将该面板放在您的 JFrame 中(具有适当的布局。这里有一些使用说明:http: //download.oracle.com/javase/1.4.2/docs/api/javax/swing/JComponent.html#paintComponent% 28java.awt.Graphics%29

回答by nkint

see

void update(Graphics g) 

method of JFrame class. graphics API ( like draw point, draw line, draw arc, etc ) are in Graphics class.

JFrame 类的方法。图形 API(如画点、画线、画圆弧等)在 Graphics 类中。

EDIT: http://www.javadb.com/drawing-a-line-using-java-2d-graphics-api

编辑:http: //www.javadb.com/drawing-a-line-using-java-2d-graphics-api

回答by Qwerky

Ask yourself if your reallywant to extend JFrameor JPanel. If you decide that you don't then you could create a basic JComponent. You may have varying success with this depending on what layout manager you use.

问问自己是否真的想扩展JFrameJPanel。如果您决定不这样做,那么您可以创建一个基本的JComponent. 根据您使用的布局管理器,您可能会取得不同的成功。

public class PixelComponent extends JComponent
{
    private Color color;

    public PixelComponent(Color color)
    {
        super();
        this.color = color;
    }

    public PixelComponent()
    {
        this(Color.BLACK);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.setColor(color);
        g.fillRect(0, 0, 1, 1);
    }
}

回答by Jhonatan S. Souza

Send the Graphics Reference and axis x and y to make a pixel:

发送图形参考和 x 轴和 y 轴以制作像素:

private void doPixel(Graphics g, int x, int y){ g.fillRect(x, y, 1, 1); }

private void doPixel(Graphics g, int x, int y){ g.fillRect(x, y, 1, 1); }