簡體   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