提问人:Deezer Nutz 提问时间:5/25/2023 更新时间:5/25/2023 访问量:30
如何在 Eclipse 中使用 GObject?(爪哇)
How do I use GObject in Eclipse? (Java)
问:
我正在测试我在 Eclipse 中找到的一个程序。其中一个类声明包括
'''公共类标题扩展了 GObject'''
在这里,我收到错误“GObject无法解析为类型”
当 GStyle、GSegment、GPosition、GText 由于 GObject 的错误而使用时,我也会遇到错误。我正在尝试制作一个适用于这些东西的 GUI
完整代码:
import javax.swing.*;
import java.util.Scanner;
import no.geosoft.cc.graphics.*;
import java.awt.*;
class TOH_solver_demo extends JFrame{
private Peg[] bolt;
private JButton s_button;
private GWindow my_wndw;
private T_of_Hanoi TOH_;
private int num_disk;
public TOH_solver_demo(int n_disk){
super();
num_disk = n_disk;
// to exit the JFrame on closing
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Creating a canvas from GWindow class
my_wndw = new GWindow(new Color(190, 220, 190));
// adding the canvas
getContentPane().add(my_wndw.getCanvas());
// Create an object of GScene class
GScene scene = new GScene (my_wndw);
double w_1[] = {0.0, 0.0, 0.0};
double w_2[] = {4.0, 0.0, 0.0};
double w_3[] = {0.0, num_disk*2, 0.0};
scene.setWorldExtent(w_1, w_2, w_3);
// creating an object of Title class and adding
// it to the scene
scene.add(new Title());
// Using a for loop to iteratively
// create pegs(3 here) and adding it to the scene
int nPegs = 3;
bolt = new Peg[nPegs];
for(int j=0; j<nPegs; j++) {
bolt[j] = new Peg(j+1.0);
scene.add(bolt[j]);
}
// Using a for loop to iteratively add the number of disks
// provided by the user to the first peg and
// futher adding it to the scene
for(int j=0; j<n_disk; j++) {
Custom_Disk my_disk = new
Custom_Disk((double)(n_disk-j)/n_disk);
my_disk.set_disk_pos(1.0, j);
bolt[0].add(my_disk);
}
// packing everything
pack();
// setting the size of the canvas into
// dimension of 600, 600
setSize(new Dimension(600, 600));
// setting the visibility of the canvas to 'true' so
// that it can be seen
setVisible(true);
// Create an object of TowersOfHanoi class
TOH_ = new T_of_Hanoi();
// calling the instance method solve to
// solve the Towers of Hanoi and display it
TOH_.solve();
}
public void disk_mover(int src_bolt, int dest_bolt)
{
// Creating an object of Custom_Disk class to move the disc
Custom_Disk my_disk =
(Custom_Disk)bolt[src_bolt].getChild(bolt[src_bolt].getNChildren()-1);
// variables to store the horizontal position of bolt
// according to the disk
double hor_0 = bolt[src_bolt].x_getter();
double hor_1 = bolt[dest_bolt].x_getter();
// variables to store the vertical position of disk in bolt
double vert_0 = my_disk.get_disk_y();
double vert_1 = num_disk+4.0;
// Animatation step of vertical movement of disk
double my_step = 0.2;
double y_pos = vert_0;
while(y_pos < vert_1) {
my_disk.set_disk_pos(hor_0, y_pos);
my_disk.redraw();
my_wndw.refresh();
y_pos += my_step;
}
bolt[src_bolt].remove(my_disk);
bolt[dest_bolt].add(my_disk);
// Animatation step of horizontal movement of disk
my_step = 0.05;
double x_pos = hor_0;
while(x_pos != hor_1){
my_disk.set_disk_pos(x_pos, y_pos);
my_disk.redraw();
my_wndw.refresh();
x_pos += (hor_1 > hor_0 ? my_step : -my_step);
if (Math.abs(x_pos - hor_1) < 0.01)
x_pos = hor_1;
}
// Animatation step of vertical movement of disk
my_step = 0.2;
y_pos = vert_1;
vert_1 = bolt[dest_bolt].getNChildren()-1;
while(y_pos > vert_1){
if (Math.abs (y_pos - vert_1) < 0.01)
y_pos = vert_1;
my_disk.set_disk_pos(x_pos, y_pos);
my_disk.redraw();
my_wndw.refresh();
y_pos -= my_step;
}
}
// To create Title in the canvas
class Title extends GObject{
// variable to store an object of GSegment
private GSegment custom_anchor;
// constructor of Title class
public Title(){
// create an object GStyle class
GStyle custom_style = new GStyle();
// setting the foreground color
custom_style.setForegroundColor(new Color(80, 100, 200));
custom_style.setLineStyle(GStyle.LINESTYLE_INVISIBLE);
// setting the font to serif with a size of 24
custom_style.setFont(new Font("serif", Font.PLAIN, 24));
setStyle(custom_style);
// instantiating a GSegment class object
custom_anchor = new GSegment();
addSegment(custom_anchor);
// Creating an object of Gtext class to
// display "towers of hanoi solver"
GText text = new GText("TOH SOLVER", GPosition.SOUTHWEST);
// setting the GText object to the GSegment object
custom_anchor.setText(text);
}
public void draw(){
custom_anchor.setGeometry(20, 20);
}
}
// Peg class to display the peg as a graphics
class Peg extends GObject{
private double[] peg_xy;
private GSegment curr_peg;
private double my_peg_pos;
public Peg(double x)
{
my_peg_pos = x;
// creating an object of GStyle class
GStyle custom_style = new GStyle();
// setting the background color of peg
custom_style.setBackgroundColor(new Color (100, 100, 100));
setStyle(custom_style);
// creatinf an object of GSegment for the peg
curr_peg = new GSegment();
addSegment(curr_peg);
// position of peg
peg_xy = new double[] {my_peg_pos - 0.05, 0.0,
my_peg_pos - 0.05, num_disk + 2,
my_peg_pos + 0.05, num_disk + 2,
my_peg_pos + 0.05, 0.0,
my_peg_pos - 0.05, 0.0};
}
public void draw(){
curr_peg.setGeometryXy(peg_xy);
}
public double x_getter(){
return my_peg_pos;
}
}
class Custom_Disk extends GObject{
private double disk_size;
private GSegment curr_disk;
private double disk_x, disk_y;
public Custom_Disk (double size)
{
disk_size = size;
// creating an object of GStyle class for styling of disk
GStyle custom_style = new GStyle();
// setting the background color of disk
custom_style.setBackgroundColor(new Color(255, 150, 150));
// setting the forecolor of disk
custom_style.setForegroundColor(new Color(255, 0, 0));
setStyle(custom_style);
// instantiating curr_disk Gsegment object
curr_disk = new GSegment();
addSegment(curr_disk);
}
public double get_disk_y(){
return disk_y;
}
public void set_disk_pos(double x_pos, double y_pos){
disk_x = x_pos;
disk_y = y_pos;
}
public void draw(){
double[] pos_xy = new
double[]{disk_x - disk_size / 2.0, disk_y,
disk_x - disk_size / 2.0, disk_y + 1.0,
disk_x + disk_size / 2.0, disk_y + 1.0,
disk_x + disk_size / 2.0, disk_y,
disk_x - disk_size / 2.0, disk_y};
curr_disk.setGeometryXy(pos_xy);
}
}
// class to solve the Tower of Hanoi problem
class T_of_Hanoi{
public void solve(){
solve(num_disk, 0, 2, 1);
}
// solve the problem recursively
private void solve(int n_disk, int src_bolt, int dest_bolt, int auxiliary){
if(n_disk==1)
disk_mover(src_bolt, dest_bolt);
else if(n_disk>1) {
solve(n_disk - 1, src_bolt, auxiliary, dest_bolt);
disk_mover(src_bolt, dest_bolt);
solve(n_disk-1, auxiliary, dest_bolt, src_bolt);
}
}
}
}
class Prog467_8_11{
// driver method
public static void main (String[] args){
// Scanner class object to take user input for number of disks
Scanner sc = new Scanner(System.in);
// Display to ask the user for the number of disks
System.out.println("Enter the number of disks");
int user_num_disk = sc.nextInt();
TOH_solver_demo demo = new TOH_solver_demo(user_num_disk);
}
}
答: 暂无答案
评论
GObject
import no.geosoft.cc.graphics.*;