[英]Want to create a JFrame and JPanel in the same class and have my own paintComponent?
I am trying to create a simple JFrame and JPanel in the same class but I do not want to extends either of the JFrame or JPanel Java classes. 我试图在同一类中创建一个简单的JFrame和JPanel,但是我不想扩展JFrame或JPanel Java类。 I want to be able to also include my own paintComponent() which will override the Panel paint component so that I can paint my own paint component.
我还希望能够包含我自己的paintComponent(),它将覆盖面板绘画组件,以便可以绘画自己的绘画组件。 I would like to just create my
我只想创建我的
JPanel p = new JPanel (); JPanel p =新的JPanel(); JFrame f = new JFrame ();
JFrame f =新的JFrame();
in one class. 一堂课 At somepoint I will use this to try and find a way to subtract and add bufferedimages but that is for a different question.
在某个时候,我将使用它来尝试找到一种减去和添加bufferedimages的方法,但这是一个不同的问题。
Class: 类:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SubtractBufferedImage {
private static final long serialVersionUID = 1L;
Dimension dimension = new Dimension(755,450);
public SubtractBufferedImage() {
JFrame f = new JFrame();
JPanel d = new JPanel();/*User defined class that is above*/
d.setBackground(Color.BLACK);
f.add(new myPanel());
f.setSize(745,440);
f.setSize(dimension);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setTitle("Subtract Buffered Images");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
SubtractBufferedImage t = new SubtractBufferedImage();
}
}
class myPanel extends JPanel {
private BufferedImage bTest1 = null;
private BufferedImage bTest2 = null;
Dimension dimension = new Dimension(755,450);
private String iOne = "src/com/ddh/graphicsBook/CardImage/hold.jpg";
private String iTwo = "src/com/ddh/graphicsBook/CardImage/draw.jpg";
myPanel() {
bTest1 = loadAnyImage(iOne);
bTest2 = loadAnyImage(iTwo);
}
private BufferedImage loadAnyImage(String value){
BufferedImage newImage = null;
try {
newImage = ImageIO.read(new File(value));
} catch (IOException e) {
e.printStackTrace();
}
return newImage;
}
private void drawImageToScreen(Graphics g,BufferedImage i,int x,int y){
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(i,x,y,null);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
drawImageToScreen(g,bTest1,10,10);
drawImageToScreen(g,bTest2,150,10);
}
}
JPanel and JFrame are both Java classes and not interfaces, so you basically can't implement your own methods inside them (according to the SOLID rules, Open/Closed principle, they are open for extension but closed for modification). JPanel和JFrame都是Java类,而不是接口,因此您基本上不能在其中实现自己的方法(根据SOLID规则,Open / Closed原则,它们可以扩展但可以修改)。 To implement your own paintComponent you must extend your JPanel and this is how it is usually done.
要实现自己的paintComponent,必须扩展JPanel,这通常是这样做的。 I am not sure why you are reluctant to extend those classes, but if you do it you get all of the rest of your specification possible to fulfil:
我不确定为什么不愿意扩展这些类,但是如果这样做,则可以实现其余所有规范:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.