java无法编辑JFrame类,JAVA无法从另一个类中绘制到JFrame上

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:

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:

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 i i cant use o paint to the 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. 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).

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. Pass in the width and height with the Graphics2D in the JVMDiagram draw method in JVMComponent's paintComponent method.

Side issue: never call repaint() from within a painting method or from code that is called from within a painting method.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值