一个对象如何被移动到一个新的点?

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

How can an object be moved to a new point?

问题

这段代码在一个框架中绘制了一个圆:

import java.awt.Canvas;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Color;

public class Drawing extends Canvas {

    public static void main(String[] args) {
        JFrame frame = new JFrame("My Drawing");
        Canvas canvas = new Drawing();
        canvas.setSize(400, 400);
        frame.add(canvas);
        frame.pack();
        frame.setVisible(true);
    }

    public void paint(Graphics g) {
	    g.fillOval(50, 50, 50, 50);
    }
}

现在如何将它移动到一个新的点(例如 (100,100))?

英文:

This code draws a circle in a frame

import java.awt.Canvas;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Color;

public class Drawing extends Canvas { 

    public static void main(String[] args) {
        JFrame frame = new JFrame("My Drawing");
        Canvas canvas = new Drawing();
        canvas.setSize(400, 400);
        frame.add(canvas);
        frame.pack();
        frame.setVisible(true);
    }

    public void paint(Graphics g) {
	    g.fillOval(50, 50, 50, 50);
    }
}

Now how can it be moved to a new point (for example (100,100))?

答案1

得分: 0

使用 setLocation(Point p) 将组件移动到新位置。

在你的示例中:

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

public class Drawing extends Canvas {

    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame("My Drawing");
        Canvas canvas = new Drawing();
        canvas.setSize(400, 400);
        frame.add(canvas);
        frame.pack();
        frame.setVisible(true);
        Thread.sleep(1000);

        canvas.setLocation(new Point(100, 100));

    }

    public void paint(Graphics g) {
        g.fillOval(50, 50, 50, 50);
    }
}
英文:

Use setLocation(Point p) which moves the component to a new location.

In your example:

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

public class Drawing extends Canvas {

    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame("My Drawing");
        Canvas canvas = new Drawing();
        canvas.setSize(400, 400);
        frame.add(canvas);
        frame.pack();
        frame.setVisible(true);
        Thread.sleep(1000);

        canvas.setLocation(new Point(100,100));

    }

    public void paint(Graphics g) {
        g.fillOval(50, 50, 50, 50);
    }
}

答案2

得分: 0

Swing中,您不需要Canvas来绘制形状。您可以扩展JPanel并覆盖其paintComponent()方法。请参考以下内容。

  1. AWT和Swing中的绘图
  2. 执行自定义绘图

您只需要在调用fillOval()方法时更改xy坐标的值。这里是一个演示小应用程序。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Drawing extends JPanel implements ActionListener, Runnable {
    private static final String MOVE = "Move";

    private int x;
    private int y;
    private JFrame frame;

    public Drawing() {
        x = 10;
        y = 10;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();
        switch (actionCommand) {
            case MOVE:
                moveCircle();
                break;
            default:
                JOptionPane.showMessageDialog(frame,
                                              actionCommand,
                                              "Unhandled",
                                              JOptionPane.WARNING_MESSAGE);
        }
    }

    @Override
    public void run() {
        showGui();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                          RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.BLUE);
        g.fillOval(x, y, 50, 50);
    }

    private JButton createButton(String text, int mnemonic, String tooltip) {
        JButton button = new JButton(text);
        button.setMnemonic(mnemonic);
        button.setToolTipText(tooltip);
        button.addActionListener(this);
        return button;
    }

    private JPanel createButtonsPanel() {
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.add(createButton(MOVE, KeyEvent.VK_M, "Moves circle."));
        return buttonsPanel;
    }

    private JPanel createCanvas() {
        setPreferredSize(new Dimension(400, 400));
        return this;
    }

    private void moveCircle() {
        x = 100;
        y = 100;
        repaint();
    }

    private void showGui() {
        frame = new JFrame("Drawing");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(createCanvas(), BorderLayout.CENTER);
        frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Drawing());
    }
}

当您运行上述代码时,您将在应用程序窗口的左上角附近看到一个蓝色的圆圈。

当您点击Move按钮时,您将看到圆圈在不同位置绘制。

英文:

In Swing you don't need a Canvas to draw shapes. You can extend JPanel and override its paintComponent() method. Please refer to the following.

  1. Painting in AWT and Swing
  2. Performing Custom Painting

All you need to do is change the values of the x and y coordinates when you call method fillOval(). Here is a small application that demonstrates.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Drawing extends JPanel implements ActionListener, Runnable {
    private static final String  MOVE = "Move";

    private int  x;
    private int  y;
    private JFrame  frame;

    public Drawing() {
        x = 10;
        y = 10;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();
        switch (actionCommand) {
            case MOVE:
                moveCircle();
                break;
            default:
                JOptionPane.showMessageDialog(frame,
                                              actionCommand,
                                              "Unhandled",
                                              JOptionPane.WARNING_MESSAGE);
        }
    }

    @Override
    public void run() {
        showGui();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                          RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.BLUE);
        g.fillOval(x, y, 50, 50);
    }

    private JButton createButton(String text, int mnemonic, String tooltip) {
        JButton button = new JButton(text);
        button.setMnemonic(mnemonic);
        button.setToolTipText(tooltip);
        button.addActionListener(this);
        return button;
    }

    private JPanel createButtonsPanel() {
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.add(createButton(MOVE, KeyEvent.VK_M, "Moves circle."));
        return buttonsPanel;
    }

    private JPanel createCanvas() {
        setPreferredSize(new Dimension(400, 400));
        return this;
    }

    private void moveCircle() {
        x = 100;
        y = 100;
        repaint();
    }

    private void showGui() {
        frame = new JFrame("Drawing");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(createCanvas(), BorderLayout.CENTER);
        frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Drawing());
    }
}

When you run the above code, you will see a blue circle near the top left corner of the application window.

When you click on the Move button, you will see the circle drawn in a different location.

huangapple
  • 本文由 发表于 2020年9月21日 22:33:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63994436.html
匿名

发表评论

匿名网友

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

确定