繁体   English   中英

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

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

我几乎创建了一个Shape类,其中RectangleCircleTriangle扩展了Shape ,以及Square类扩展了Circle 我有与该主类一起工作的代码,但是我很难将其转换为GUI,因为我不确定如何将数字3组合在一起以及如何制作g.drawOval(with given x,y & radius)draw triangle(given x,y, base and height)

  1. Project6类将必须扩展JFrame
  2. Project6构造函数将必须设置GUI窗口。
  3. 一种新的抽象方法: public void display(Graphics g); 应该添加到基类和派生类中。
  4. 必须使用paintComponent方法设置自定义JPanel
  5. 新的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();

我在每个班级的末尾都添加这样的内容吗? IE CircleSquareTriangleRectangle类?

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

我无法更改数组的设置方式,但是这不应该是扩展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);
 }  

我是GUI的新手,因此很难做到这一点,但这是否可以绘制形状? 但是我收到一个错误消息,说不能从静态上下文中引用非静态方法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. 您的课程扩展了一个从不显示的JFrame
  2. 您应该在JPanel的paintComponent方法中绘制形状,该方法已添加到JFrame中。
  3. 我将使用ArrayList<Shape>而不是数组,因为这样我就可以向集合中添加尽可能多的ArrayList<Shape> ,而不必担心null项目。
  4. 然后,我将遍历paintComponent方法重写中的集合,并使用Graphics2D对象绘制每个Shape。
  5. 关于您的最后一个问题, "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