调用Java方法绘制图形(calling a java method to draw graphic

2019-06-21 05:43发布

我尝试画一些简单的图形框架。 我也想能够调整什么,我从我的主要方法绘图。 例如,设置一个字符串变量要被打印,或矩形的坐标。

我似乎遇到的问题是, paintComponent之前,我可以设置类变量方法被调用。 我将如何改变这个代码能够成立JPanel / JFrame变量之前它吸引到屏幕?

谢谢

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

public class Test {

    public static void main(String[] args) {
        FrameTest test_frame = new FrameTest();
        test_frame.test_string = "I WANT TO DRAW THIS STRING";
    }
}

class FrameTest extends JFrame{
    private static final long serialVersionUID = 1L;
    String test_string;

    public FrameTest(){
        this.test_string = "TEMP STRING FROM FRAME";
        JFrame gui = new JFrame();
        gui.setTitle("Test Title");
        gui.setSize(400,400);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Painting painting = new Painting();

        Container pane = gui.getContentPane();
        pane.setLayout(new GridLayout(1,1));

        pane.add(painting);
        gui.setVisible(true);

    }
}

class Painting extends JPanel{
    private static final long serialVersionUID = 1L;
    String test_string;

    public Painting(){
        setBackground(Color.WHITE);
        this.test_string = "TEMP STRING FROM PANEL";
    }

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

        g.setColor(Color.RED);
        g.drawString(test_string, 20, 20);
    }
}

Answer 1:

卸下FrameTest类test_string。 设置使用set方法直接test_string。 见例如:

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        FrameTest1 test_frame = new FrameTest1();
        test_frame.setContentString("I WANT TO DRAW THIS STRING");
    }

}

class FrameTest1 extends JFrame {
    private static final long serialVersionUID = 1L;

    Painting painting = new Painting();

    public FrameTest1() {
        JFrame gui = new JFrame();
        gui.setTitle("Test Title");
        gui.setSize(400, 400);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container pane = gui.getContentPane();
        pane.setLayout(new GridLayout(1, 1));

        pane.add(painting);
        gui.setVisible(true);

    }

    public void setContentString(String value) {
        painting.test_string = value;
    }
}

class Painting extends JPanel {
    private static final long serialVersionUID = 1L;
    String test_string;

    public Painting() {
        setBackground(Color.WHITE);
        this.test_string = "TEMP STRING FROM PANEL";
    }

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

        g.setColor(Color.RED);
        g.drawString(test_string, 20, 20);
    }
}


Answer 2:

在这里,你要分配AA串test_stringFrameTest未更新的同名变量Painting

test_frame.test_string = "I WANT TO DRAW THIS STRING";

为什么不添加一个更新的方法来FrameTest你一定要这样的引用:

public void setTestString(String test_string) {
    painting.setTestString(test_string);
}

并呼吁:

FrameTest test_frame = new FrameTest();
test_frame.setTestString("I WANT TO DRAW THIS STRING");

注:Java使用首字母大写,如testString



Answer 3:

你可以通过你想通过构造提请绘画类并把画框架以同样的方式,以及文字。

要了解关于Java的构造和参数的详细读阅读: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

我已经修改了代码,你需要什么,我还没有,虽然进行了测试。

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

public class Test {

    public static void main(String[] args) {
        Painting painting = new Painting("I WANT TO DRAW THIS STRING");
        FrameTest test_frame = new FrameTest(painting);
    }
}

class FrameTest extends JFrame{
    private static final long serialVersionUID = 1L;
    String test_string;

    public FrameTest(painting){
        super();
        this.test_string = "TEMP STRING FROM FRAME";
        JFrame gui = new JFrame();
        gui.setTitle("Test Title");
        gui.setSize(400,400);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container pane = gui.getContentPane();
        pane.setLayout(new GridLayout(1,1));

        pane.add(painting);
        gui.setVisible(true);

    }
}

class Painting extends JPanel{
    private static final long serialVersionUID = 1L;
    String test_string;

    public Painting(String test_string){
        super();
        this.test_string = test_string;
        setBackground(Color.WHITE);
    }

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

        g.setColor(Color.RED);
        g.drawString(test_string, 20, 20);
    }
}


Answer 4:

例如,设置一个字符串变量要被打印,或矩形的坐标。

创建BufferedImagemain(String[])有一种方法Painting.setImage(Image) ,显示在图像JLabel

这是更通用的,因为它可以在椭圆上的梯度BG的一部分接受字符串,或椭圆形的图像,或串的图像的图像..

显示许多图像

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;

public class ImageViewer {

    JPanel gui;
    /** Displays the image. */
    JLabel imageCanvas;

    /** Set the image as icon of the image canvas (display it). */
    public void setImage(Image image) {
        imageCanvas.setIcon(new ImageIcon(image));
    }

    public void initComponents() {
        if (gui==null) { 
            gui = new JPanel(new BorderLayout());
            gui.setBorder(new EmptyBorder(5,5,5,5));
            imageCanvas = new JLabel();

            JPanel imageCenter = new JPanel(new GridBagLayout());
            imageCenter.add(imageCanvas);
            JScrollPane imageScroll = new JScrollPane(imageCenter);
            imageScroll.setPreferredSize(new Dimension(300,100));
            gui.add(imageScroll, BorderLayout.CENTER);
        }
    }

    public Container getGui() {
        initComponents();
        return gui;
    }

    public static Image getRandomImage(Random random) {
        int w = 100 + random.nextInt(400);
        int h = 50 + random.nextInt(200);
        BufferedImage bi = new BufferedImage(
                w,h,BufferedImage.TYPE_INT_RGB);

        return bi;
    }

    public static void main(String[] args) throws Exception {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("Image Viewer");
                // TODO Fix kludge to kill the Timer
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final ImageViewer viewer = new ImageViewer();
                f.setContentPane(viewer.getGui());

                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);

                ActionListener animate = new ActionListener() {

                    Random random = new Random();

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        viewer.setImage(getRandomImage(random));
                    }
                };
                Timer timer = new Timer(1500,animate);
                timer.start();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}


文章来源: calling a java method to draw graphics