简体   繁体   English

GUI使用JFrame和JPanel绘制自定义形状

[英]GUI Using JFrame, & JPanel drawing custom shapes

Pretty much I create a Shape class, with Rectangle , Circle , Triangle extending Shape , and a Square class extending Circle . 我几乎创建了一个Shape类,其中RectangleCircleTriangle扩展了Shape ,以及Square类扩展了Circle I have the code working with this main class, but I'm having a tough time converting it into GUI because I'm not sure how to do number 3 to make this come together and how to make a g.drawOval(with given x,y & radius) and draw triangle(given x,y, base and height) . 我有与该主类一起工作的代码,但是我很难将其转换为GUI,因为我不确定如何将数字3组合在一起以及如何制作g.drawOval(with given x,y & radius)draw triangle(given x,y, base and height)

  1. Project6 class will have to extend the JFrame class Project6类将必须扩展JFrame
  2. Project6 constructor will have to set up the GUI window. Project6构造函数将必须设置GUI窗口。
  3. A new abstract method: public void display(Graphics g); 一种新的抽象方法: public void display(Graphics g); should be added to the base and derived classes. 应该添加到基类和派生类中。
  4. A custom JPanel must be set up with a paintComponent method 必须使用paintComponent方法设置自定义JPanel
  5. The new display(Graphics g) method will have to draw the shapes on the GUI window and be called from a loop in the paintComponent method. 新的display(Graphics g)方法将必须在GUI窗口上绘制形状,然后从paintComponent方法的循环中调用它。

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

public class Project6 extends JFrame {   

private Shape [] thearray = new Shape[100]; 

public static void main (String [] args) {

Project6 tpo = new Project6();
tpo.run();
}

public void run () {
int count = 0;

thearray[count++] = new Circle(20, 20, 40);
thearray[count++] = new Triangle(70, 70, 20, 30);
thearray[count++] = new Rectangle(150, 150, 40, 40);
thearray[count++] = new Square(100, 100, 50, 75);

for (int i = 0; i < count; i ++ ) {    
   thearray[i].display();              
 }  

int offset = 0;
double totalarea = 0.0;
while (thearray[offset] != null) {          
   totalarea = totalarea + thearray[offset].area();   
   offset++;
} 
System.out.println("The total area for " + offset + " Shape objects is " + totalarea);
}

public Project6() {
JFrame frame = new JFrame();
frame.setSize(800, 700);                           
frame.setTitle("Shapes: Circle, Triangle, Rectangle, Square");
frame.setLocationRelativeTo(null);                 //Center Frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}  

public static class MyPanel extends JPanel {

  public static JPanel showJPanel(Graphics g) {
   panel = new MyPanel();
   return panel;
   }

  @Override
  public void paintComponent(Graphics g) {
  super.paintComponent(g);
  for(int i = 0; i < thearray.length && thearray[i] != null; i++) {
  thearray[i].display();

Do I add something like this at the end of each of my classes? 我在每个班级的末尾都添加这样的内容吗? IE Circle , Square , Triangle , Rectangle class? IE CircleSquareTriangleRectangle类?

 @Override
 public void draw(Graphics g) {
  g.drawRect(getXPos(), getYPos(), width, height);
  }

I can't change the way the array is set up, but isn't this supposed to be the class that extends JFrame? 我无法更改数组的设置方式,但是这不应该是扩展JFrame的类吗?

 public Project6() {
 JFrame frame = new JFrame();
 frame.setSize(800, 700);                           
 frame.setTitle("Shapes: Circle, Triangle, Rectangle, Square");
 frame.setLocationRelativeTo(null);                 //Center Frame
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setVisible(true);
 }  

I'm new to GUI so this is a little hard to do, but would this work for drawing the shapes? 我是GUI的新手,因此很难做到这一点,但这是否可以绘制形状? But I get an error saying nonstatic method get() cant be referenced from static context 但是我收到一个错误消息,说不能从静态上下文中引用非静态方法get()

class NewPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(Triangle.getXPos(), 0, 0, Triangle.getYPos());
g.drawLine(Triangle.getXPos(), 0, Triangle.getXPos, Triangle.getYPos());
g.drawLine(Triangle.getXPos(), Triangle.getYPos, 0, Triangle.getYPos());
g.drawRect(Rectangle.getXPos(), Rectangle.getYPos(), Rectangle.getWidth(), Rectangle.getHeight());
g.drawRect(Square.getXPos(), Square.getYPos(), Square.getWidth(), Square.getHeight());
g.drawOval(Circle.getXPos(), Circle.getYPos(), Circle.getRadius(), 10);

for(int i = 0; i < thearray.length && thearray[i] != null; i++) {
thearray[i].display();
}
} 
  1. Your class extends a JFrame that is never displayed 您的课程扩展了一个从不显示的JFrame
  2. You should draw your shapes in the paintComponent method of a JPanel, one that is added to your JFrame. 您应该在JPanel的paintComponent方法中绘制形状,该方法已添加到JFrame中。
  3. I would use an ArrayList<Shape> , not an array, since this way I'd be able to add as many or as few Shapes to my collection and not have to worry about null items. 我将使用ArrayList<Shape>而不是数组,因为这样我就可以向集合中添加尽可能多的ArrayList<Shape> ,而不必担心null项目。
  4. I'd then iterate through the collection in the paintComponent method override and draw each Shape using a Graphics2D object. 然后,我将遍历paintComponent方法重写中的集合,并使用Graphics2D对象绘制每个Shape。
  5. Regarding your last question, "Do I add something like this at the end of each of my classes? ie(Circle, square, triangle, rectangle class?..." no, there's no need for a "draw" method since you'll be using the paintComponent method to do your drawing. 关于您的最后一个问题, "Do I add something like this at the end of each of my classes? ie(Circle, square, triangle, rectangle class?..."不,因为您需要将使用paintComponent方法进行绘制。

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

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