静态上下文中的非静态方法

发布于 2024-10-31 19:38:06 字数 2971 浏览 0 评论 0原文

为什么我不能推荐?你知道如何修复它吗?我想用DDA算法画线。 请帮忙。

import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;
import java.util.Scanner;
import java.io.IOException;
import javax.swing.*;

class Test extends JPanel {

    private void JPanel1MouseClicked(MouseEvent evt){
        int x = evt.getX();
        int y = evt.getY();
        System.out.println("X to: " + x + " Y to: " + y);

    }

    public void sprawdz(double xx1, double xx2, double yy1, double yy2){

    }

    public static void main(String[] args) {
        String x1;
        String x2;
        String y1;
        String y2;

        Scanner sc = new Scanner(System.in);
        System.out.print("Podaj pierwsza wspolrzedna pierwszego punktu: ");
        x1 = sc.nextLine();
        System.out.print("Podaj druga wspolrzedna pierwszego punktu: ");
        x2 = sc.nextLine();
        System.out.print("Podaj pierwsza wspolrzedna drugiego punktu: ");
        y1 = sc.nextLine();
        System.out.print("Podaj druga wspolrzedna drugiego punktu: ");
        y2 = sc.nextLine();






            //DDA2 nowy = new DDA2(x1, x2, y1, y2);
            Test nowy = new Test();
            DDA2.licz(x1, x2, y1, y2);




        JFrame ramka = new JFrame();
        ramka.setSize(300,300);
        ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ramka.getContentPane().add(new Test());
        ramka.setVisible(true);

        // JPanel jp = new JPanel();
        // jp.setBackground(Color.WHITE);
        // jp.setSize(100,100);
        // jp.setLayout(new BorderLayout());
        // jp.setVisible(true);


    }


    class DDA2 {
        double dxx1 = Double.parseDouble(xx1);
        double dxx2 = Double.parseDouble(xx2);
        double dyy1 = Double.parseDouble(yy1);
        double dyy2 = Double.parseDouble(yy2);
        double dx = x2 - x1;
        double dy = y2 - y1;
        public void licz(String xx1, String xx2, String yy1, String yy2){

            if (Math.abs(dx) >= Math.abs(dy))
                {
                    double m = Math.abs(dx);
                    System.out.println("DX" + m);
                }
                else
                {


                    // ALGORYTYM PRZYROSTOWY
                    double m = Math.abs(dy);
                    //System.out.println("DY" + m);
                    double x = dxx1;
                    double y = dyy1;
                    for (int i=1; i <= m; i++)
                    {
                    x = x + dx/m;
                    y = y + dy/m;

                    }
                }

        System.out.println("Wspolrzednie punktu pierwszego to: " + "(" + dxx1 + "; " + dxx2 +")");
        System.out.println("Wspolrzednie punktu drugiego to: " + "(" + dyy1 + "; " + dyy2 + ")");
        }



    }

    // public void paintComponent(Graphics g){
        // super.paintComponent(g);
        // g.setColor(Color.RED);
        // g.fillRect((int) x, (int) y, 1, 1);
    // }
}

Why I cannot reffer? Do You know how to fix it? I would like to draw line by DDA algorithm.
Please help.

import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;
import java.util.Scanner;
import java.io.IOException;
import javax.swing.*;

class Test extends JPanel {

    private void JPanel1MouseClicked(MouseEvent evt){
        int x = evt.getX();
        int y = evt.getY();
        System.out.println("X to: " + x + " Y to: " + y);

    }

    public void sprawdz(double xx1, double xx2, double yy1, double yy2){

    }

    public static void main(String[] args) {
        String x1;
        String x2;
        String y1;
        String y2;

        Scanner sc = new Scanner(System.in);
        System.out.print("Podaj pierwsza wspolrzedna pierwszego punktu: ");
        x1 = sc.nextLine();
        System.out.print("Podaj druga wspolrzedna pierwszego punktu: ");
        x2 = sc.nextLine();
        System.out.print("Podaj pierwsza wspolrzedna drugiego punktu: ");
        y1 = sc.nextLine();
        System.out.print("Podaj druga wspolrzedna drugiego punktu: ");
        y2 = sc.nextLine();






            //DDA2 nowy = new DDA2(x1, x2, y1, y2);
            Test nowy = new Test();
            DDA2.licz(x1, x2, y1, y2);




        JFrame ramka = new JFrame();
        ramka.setSize(300,300);
        ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ramka.getContentPane().add(new Test());
        ramka.setVisible(true);

        // JPanel jp = new JPanel();
        // jp.setBackground(Color.WHITE);
        // jp.setSize(100,100);
        // jp.setLayout(new BorderLayout());
        // jp.setVisible(true);


    }


    class DDA2 {
        double dxx1 = Double.parseDouble(xx1);
        double dxx2 = Double.parseDouble(xx2);
        double dyy1 = Double.parseDouble(yy1);
        double dyy2 = Double.parseDouble(yy2);
        double dx = x2 - x1;
        double dy = y2 - y1;
        public void licz(String xx1, String xx2, String yy1, String yy2){

            if (Math.abs(dx) >= Math.abs(dy))
                {
                    double m = Math.abs(dx);
                    System.out.println("DX" + m);
                }
                else
                {


                    // ALGORYTYM PRZYROSTOWY
                    double m = Math.abs(dy);
                    //System.out.println("DY" + m);
                    double x = dxx1;
                    double y = dyy1;
                    for (int i=1; i <= m; i++)
                    {
                    x = x + dx/m;
                    y = y + dy/m;

                    }
                }

        System.out.println("Wspolrzednie punktu pierwszego to: " + "(" + dxx1 + "; " + dxx2 +")");
        System.out.println("Wspolrzednie punktu drugiego to: " + "(" + dyy1 + "; " + dyy2 + ")");
        }



    }

    // public void paintComponent(Graphics g){
        // super.paintComponent(g);
        // g.setColor(Color.RED);
        // g.fillRect((int) x, (int) y, 1, 1);
    // }
}

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

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

发布评论

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

评论(4

蝶舞 2024-11-07 19:38:06

licz 方法设为静态。您在没有实例的情况下调用它。此外 - 您有依赖于方法参数的实例变量 - 这不是直接可能的。也将它们移动到方法主体中。

一般来说,您有两个选择:

  • 拥有一切静态 - 如果您不需要具有某种状态的对象,并且每次调用都是对某些给定参数的一次性操作,那么这就是正确的方法。我认为这就是你的情况。

  • 有一个实例。使用您希望在整个调用过程中重用的一组给定参数来构造它。然后将方法声明为非静态并决定哪些变量应属于实例。

Make the licz method static. You are calling it without an instance. Furthermore - you have instance variables that depend on method parameters - this is not directly possible. Move them in the method body as well.

Generally speaking, you have two options:

  • have everything static - if you don't require your an object that has some state, and each invocation is a one-time operation on some given parameters, then this is the proper way to go. I think this is your case.

  • have an instance. Construct it with a given set of parameters that you want to reuse throughout invocations. Then declare the methods non-static and decide which variables should belong to the instance.

醉殇 2024-11-07 19:38:06

方法 DDA2.licz() 未声明为静态,因为它使用的 DDA2 类字段也不是静态的。因此它只能应用于 DDA2 的实例,而不能应用于静态上下文(即类似 DDA2 d = new DDA2(); d.licz(...); 可以工作,但不能 DDA2.licz(...);)。

The method DDA2.licz() is not declared static since it uses fields of class DDA2 which are not static also. Thus it can only be applied to an instance of DDA2 but not in a static context (i.e. something like DDA2 d = new DDA2(); d.licz(...); will work but not DDA2.licz(...);).

蘸点软妹酱 2024-11-07 19:38:06

您需要实例化 DDA2 并在该类的新实例上调用 licz。您几乎已经注释掉了正确的代码。

将 DDA2.licz(x1, x2, y1, y2) 替换为

DDA2 nowy = new DDA2();
nowy.licz(x1, x2, y1, y2);

编辑:完全错过了 DDA2 的损坏定义。 @Bozho 的答案是正确的。

You need to instantiate DDA2 and call licz on the new instance of that class. You nearly have the correct code commented out.

Replace DDA2.licz(x1, x2, y1, y2) with

DDA2 nowy = new DDA2();
nowy.licz(x1, x2, y1, y2);

Edit: Missed the broken definition of DDA2 altogether. @Bozho's answer is the correct one.

旧时光的容颜 2024-11-07 19:38:06

在这种情况下,DAA2 是一个内部类。
所以你不能让它的方法静态。
您应该将 DDA2 类设为静态,然后将 licz 方法设为静态。

将 DAA2 类移出测试类并使用以下方式:

DAA2 daa2 = new DAA2();
daa2.licz(x1, x2, y1, y2);

In this case, the DAA2 is an inner class.
So you can't made it's methods static.
You should make DDA2 class static and then made the licz method static.

or

Move DAA2 class out of Test class and the use this way :

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