java绘制功能不起作用

时间:2014-12-20 01:02:15

标签: java swing user-interface

我正在使用java绘制椭圆并按行连接它们,当我扫描变量时,绘制功能根本不起作用

import java.awt.Color;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Paint;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Main extends JFrame {
    public Main (){
        setTitle("Graph");
        setSize(1000, 1000);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public   void paint (Graphics g,int x,int z,int y) {
        g.fillOval(x, x+20, 40, 40);
        g.drawLine(x+10, x+50,x+z+10, z+30);
        g.fillOval(x+z, z, 40, 40);
    }

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int x = scan.nextInt();
        int z =scan.nextInt();
        int y =scan.nextInt();

        x=x+1;
        z=z+1;
        y=y+1; 

        Main m=new Main();
        Graphics g=m.getGraphics();
        m.paint(g, x, z, y);
    }
}

2 个答案:

答案 0 :(得分:1)

Graphics g=m.getGraphics();不是自定义绘画是如何在Swing中完成的,除了能够返回null之外,它只是一个"快照"最后画的是什么,可以随时涂上

public void paint (Graphics g,int x,int z,int y) {不是在绘制过程中调用的方法。 Java并不神奇地知道这种方法可以用于绘画,你需要告诉它。

此外,您不应该将基于控制台的输入与GUI混合,选择其中一个......

首先看看

有关绘画如何在Swing中工作的更多细节。

你应该避免直接绘制到顶级容器,如JFrame,除了你实际上没有向框架添加任何新功能的事实,它们(顶级容器)不是双缓冲的,并且可能导致闪烁在更新期间,JFrame(以及JWindowJApplet)在框架和用户之间有JRootPane和内容窗格(有时是玻璃窗格),这可能导致任何问题绘画问题的数量。

更好的解决方案是根据JPanel之类的内容创建自定义组件并覆盖它paintComponent(确保在进行任何自定义绘画之前调用super.paintComponent

Example

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main101 {

    public static void main(String[] args) {
        new Main101();
    }

    public Main101() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int x = 10;
        private int z = 10;

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.fillOval(x, x + 20, 40, 40);

            g2d.drawLine(x + 10, x + 50, x + z + 10, z + 30);

            g2d.fillOval(x + z, z, 40, 40);
            g2d.dispose();
        }

    }

}

答案 1 :(得分:0)

你必须覆盖JFrame s'涂料方法。除非你这样做,它不会画到画框,阅读本文, Overriding and Hiding Methods

这是一个演示

public class Main extends JFrame {

    int x;
    int y;
    int z;

    public Main(int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
        setTitle("Graph");

        setSize(1000, 1000);

        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    @Override
    public void paint(Graphics g) {

        g.fillOval(x, x + 20, 40, 40);

        g.drawLine(x + 10, x + 50, x + z + 10, z + 30);

        g.fillOval(x + z, z, 40, 40);
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int x = scan.nextInt();
        int z = scan.nextInt();
        int y = scan.nextInt();
        x = x + 1;
        z = z + 1;
        y = y + 1;
        Main m = new Main(x, y, z);

    }
}