简体   繁体   English

JAVA无法从另一个类中绘制到JFrame

[英]JAVA can't paint onto JFrame from within another class

I am aware this is my error. 我知道这是我的错误。 My question is why isn't this working what am i missing i can call this is i put it a method instead of a class so i am assuming theirs something wrong with the third class? 我的问题是,为什么这样不起作用,我想念的是什么,我可以称其为方法而不是类,因此我假设他们的第三类有问题吗?

Class 1: 第1类:

package assignment.pkg1.java;

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

public class JVMVeiwer  {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    final int FRAME_WIDTH = 1000; // Frame Width
    final int FRAME_HEIGHT = 800; // Frame Height
    JFrame frame = new JFrame();

    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); //Sets Frame Size
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("JVM Diagram");// Sets the Title       
    JVMComponent component = new JVMComponent();
    frame.setBackground(Color.WHITE);
    frame.add(component); // adds the diagram to the JFrame       
    frame.setVisible(true); // Makes the frame visible
}

} }

Class 2: 第2类:

package assignment.pkg1.java;

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


public class JVMComponent extends  JComponent { 

@Override
public void  paintComponent(Graphics g) {      
    super.paintComponent(g);        
    Graphics2D g2 = (Graphics2D) g; // recover the graphic  
    JVMDiagram diagram = new JVMDiagram(); // creates an instance of JVM Diagram
    diagram.draw(g2);
  }
}

Class 3 this is the one ii cant use o paint to the jframe: 第3类,这是我不能在jframe上使用的绘画:

package assignment.pkg1.java;

import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JComponent;

public class JVMDiagram {
// Constructor
public JVMDiagram() {

}
// Draw method for shape
public  void draw(Graphics2D g2) {
// Detailed instructions to draw shape
    int x = getWidth();
    int y = getHeight();
    int temp, temp2;
    int width = x / 2;
    int height = x / 2;
    x = (x - width) / 2;
    y= (y - height) / 2; 

    g2.setColor(Color.RED);
    g2.drawOval(x, y, width, height);
    g2.drawRect(x, y, width, height);
    g2.setColor(Color.RED);
    g2.drawLine(x, y, width + x, height + y);
    g2.drawRoundRect(x, y, width, height, y, y);

    g2.drawLine(x + width, y, x, height + y);
}

} }

Your problem is that you're misusing inheritance. 您的问题是您滥用继承。 Your JVMDiagram is extending JVMComponent and it shouldn't. 您的JVMDiagram正在扩展JVMComponent,但不应该。 Yes, you gain JVMComponent's getWidth() and getHeight() method, but they don't mean anything since JVMDiagram isn't being added to the GUI as a component, shouldn't be added as a component, and it has 0 height and width (print them out). 是的,您获得了JVMComponent的getWidth()和getHeight()方法,但是它们没有任何意义,因为JVMDiagram没有作为组件添加到GUI,不应作为组件添加,并且高度为0,并且宽度(打印出来)。

Rethink your design, don't use inheritance for this. 重新考虑您的设计, 不要为此使用继承。 Instead pass in values from one object to the other if needed. 如果需要,可以将值从一个对象传递到另一个。 For instance, create a JVMDiagram field within JVMComponent and initialize it. 例如,在JVMComponent中创建一个JVMDiagram字段并将其初始化。 Pass in the width and height with the Graphics2D in the JVMDiagram draw method in JVMComponent's paintComponent method. 在JVMComponent的paintComponent方法的JVMDiagram绘制方法中使用Graphics2D传递宽度和高度。

Side issue: never call repaint() from within a painting method or from code that is called from within a painting method. 附带问题:永远不要从绘画方法内或从绘画方法内调用的代码调用repaint()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM