[英]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
类,其中Rectangle
, Circle
, Triangle
扩展了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)
。
JFrame
class JFrame
类 public void display(Graphics g);
public void display(Graphics g);
should be added to the base and derived classes. JPanel
must be set up with a paintComponent
method paintComponent
方法设置自定义JPanel
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
Circle
, Square
, Triangle
, Rectangle
类?
@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();
}
}
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项目。 "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.