[英]GUI Using JFrame, & JPanel drawing custom shapes
我幾乎創建了一個Shape
類,其中Rectangle
, Circle
, Triangle
擴展了Shape
,以及Square
類擴展了Circle
。 我有與該主類一起工作的代碼,但是我很難將其轉換為GUI,因為我不確定如何將數字3組合在一起以及如何制作g.drawOval(with given x,y & radius)
並draw triangle(given x,y, base and height)
。
JFrame
類 public void display(Graphics g);
應該添加到基類和派生類中。 paintComponent
方法設置自定義JPanel
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 Circle
, Square
, Triangle
, Rectangle
類?
@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();
}
}
ArrayList<Shape>
而不是數組,因為這樣我就可以向集合中添加盡可能多的ArrayList<Shape>
,而不必擔心null項目。 "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.