2015-03-18 32 views
0

一年級的java學生,目前已經工作了大約8周的經驗。所以,我需要創建三個圓圈並使用繪圖面板進行顯示。然後我需要創建靜態方法,該方法應該具有兩個圓的半徑(兩個參數)的參數,如果第一個圓較小,則該方法應返回-1,如果兩個圓具有相同的大小,則返回0或返回1如果第一圈較大。根據此方法的返回值,主要方法應打印一行輸出,如:繪製三個圓圈,存儲數據並對其進行比較

綠圈小於紅圈。

我已經繪製了圓圈,但我不知道如何從用戶存儲該值,然後進行比較。任何幫助都會很棒。這是我到目前爲止有:

public class Circles { 
    public static final Scanner CONSOLE=new Scanner(System.in); 

    //prompt user 
    public static int promptForInt(String prompt) { 
    System.out.print(prompt); 
    return CONSOLE.nextInt(); 
    } 

    //Static method w/ parameters for the radiuses of red circle 
    public static int redCircle(Graphics g){ 
    g.setColor(Color.RED); 
    int radius = promptForInt("Please enter the raidus of the red circle: "); 
    int x = promptForInt("Please enter the x-coordinate of the red circle: "); 
    int y= promptForInt("Please enter the y-coordinate of the red circle: "); 
    g.fillOval(0+x, 0+y, radius*2, radius*2); 
    } 

    //Static method w/ parameters for the radiuses of the green circle 
    public static void greenCircle(Graphics g){ 
    g.setColor(Color.GREEN); 
    int radius = promptForInt("Please enter the raidus of the green circle: "); 
    int x = promptForInt("Please enter the x-coordinate of the green circle: "); 
    int y= promptForInt("Please enter the y-coordinate of the green circle: "); 
    g.fillOval(0+x, 0+y, radius*2, radius*2); 
    } 

    //Static method w/ parameters for the radiuses of the green circle 
    public static void blueCircle(Graphics g){ 
    g.setColor(Color.BLUE); 
    int radius = promptForInt("Please enter the raidus of the blue circle: "); 
    int x = promptForInt("Please enter the x-coordinate of the blue circle: "); 
    int y= promptForInt("Please enter the y-coordinate of the blue circle: "); 
    g.fillOval(0+x, 0+y, radius*2, radius*2); 
    } 

    //compares two circles 
    public static void compareCircles(int r1, int r2){ 
    return r1-r2; 

    } 

    //exe starts here 
    public static void main(String [] args){ 
    DrawingPanel panel = new DrawingPanel(400,300); 
    Graphics g = panel.getGraphics(); 

    System.out.println("Lab 5 written by me.\n"); 
    redCircle(g); 
    greenCircle(g); 
    blueCircle(g); 
    } 
} 

編輯以由MadProgrammer

/* 
Stuart Reges and Marty Stepp 
February 24, 2007 
Changes by Tom Bylander in 2010 (no anti-alias, repaint on sleep) 
Changes by Tom Bylander in 2012 (track mouse clicks and movement) 
Changes by Tom Bylander in 2013 (fix bug in tracking mouse clicks) 
Changes by S. Robbins in 2014 (getters for width and height) 
Changes by S. Robbins in 2014 (addKeyListener added) 
Changes by S. Robbins in 2014 (catch exception on default close so that it works in an applet) 
Changes by S. Robbins in 2015 (buffer key events) 
Changes by S. Robbins in 2015 (show mouse status by default is off) 

The DrawingPanel class provides a simple interface for drawing persistent 
images using a Graphics object. An internal BufferedImage object is used 
to keep track of what has been drawn. A client of the class simply 
constructs a DrawingPanel of a particular size and then draws on it with 
the Graphics object, setting the background color if they so choose. 

To ensure that the image is always displayed, a timer calls repaint at 
regular intervals. 
*/ 

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import java.util.ArrayList; 

public class DrawingPanel implements ActionListener { 
private static final String versionMessage = 
    "Drawing Panel version 1.1, January 25, 2015"; 
private static final int DELAY = 100; // delay between repaints in millis 
private static final boolean PRETTY = false; // true to anti-alias 
private static boolean showStatus = false; 
private static final int MAX_KEY_BUF_SIZE = 10; 

private int width, height; // dimensions of window frame 
private JFrame frame;   // overall window frame 
private JPanel panel;   // overall drawing surface 
private BufferedImage image; // remembers drawing commands 
private Graphics2D g2;  // graphics context for painting 
private JLabel statusBar;  // status bar showing mouse position 
private volatile MouseEvent click;  // stores the last mouse click 
private volatile boolean pressed;  // true if the mouse is pressed 
private volatile MouseEvent move;  // stores the position of the mouse 
private ArrayList<KeyInfo> keys; 

// construct a drawing panel of given width and height enclosed in a window 
public DrawingPanel(int width, int height) { 
    this.width = width; 
    this.height = height; 
    keys = new ArrayList<KeyInfo>(); 
    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 

    statusBar = new JLabel(" "); 
    statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    statusBar.setText(versionMessage); 

    panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); 
    panel.setBackground(Color.WHITE); 
    panel.setPreferredSize(new Dimension(width, height)); 
    panel.add(new JLabel(new ImageIcon(image))); 

    click = null; 
    move = null; 
    pressed = false; 

    // listen to mouse movement 
    MouseInputAdapter listener = new MouseInputAdapter() { 
    public void mouseMoved(MouseEvent e) { 
     pressed = false; 
     move = e; 
     if (showStatus) 
      statusBar.setText("moved (" + e.getX() + ", " + e.getY() + ")"); 
    } 

    public void mousePressed(MouseEvent e) { 
     pressed = true; 
     move = e; 
     if (showStatus) 
      statusBar.setText("pressed (" + e.getX() + ", " + e.getY() + ")"); 
    } 

    public void mouseDragged(MouseEvent e) { 
     pressed = true; 
     move = e; 
     if (showStatus) 
      statusBar.setText("dragged (" + e.getX() + ", " + e.getY() + ")"); 
    } 

    public void mouseReleased(MouseEvent e) { 
     click = e; 
     pressed = false; 
     if (showStatus) 
      statusBar.setText("released (" + e.getX() + ", " + e.getY() + ")"); 
    } 

    public void mouseEntered(MouseEvent e) { 
//  System.out.println("mouse entered"); 
     panel.requestFocus(); 
    } 

    }; 
    panel.addMouseListener(listener); 
    panel.addMouseMotionListener(listener); 
    new DrawingPanelKeyListener(); 

    g2 = (Graphics2D)image.getGraphics(); 
    g2.setColor(Color.BLACK); 
    if (PRETTY) { 
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
    g2.setStroke(new BasicStroke(1.1f)); 
    } 

    frame = new JFrame("Drawing Panel"); 
    frame.setResizable(false); 
    try { 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // so that this works in an applet 
    } catch (Exception e) {} 
    frame.getContentPane().add(panel); 
    frame.getContentPane().add(statusBar, "South"); 
    frame.pack(); 
    frame.setVisible(true); 
    toFront(); 
    frame.requestFocus(); 

    // repaint timer so that the screen will update 
    new Timer(DELAY, this).start(); 
} 

public void showMouseStatus(boolean f) { 
    showStatus = f; 
} 

public void addKeyListener(KeyListener listener) { 
    panel.addKeyListener(listener); 
    panel.requestFocus(); 
} 

// used for an internal timer that keeps repainting 
public void actionPerformed(ActionEvent e) { 
    panel.repaint(); 
} 

// obtain the Graphics object to draw on the panel 
public Graphics2D getGraphics() { 
    return g2; 
} 

// set the background color of the drawing panel 
public void setBackground(Color c) { 
    panel.setBackground(c); 
} 

// show or hide the drawing panel on the screen 
public void setVisible(boolean visible) { 
    frame.setVisible(visible); 
} 

// makes the program pause for the given amount of time, 
// allowing for animation 
public void sleep(int millis) { 
    panel.repaint(); 
    try { 
    Thread.sleep(millis); 
    } catch (InterruptedException e) {} 
} 

// close the drawing panel 
public void close() { 
    frame.dispose(); 
} 

// makes drawing panel become the frontmost window on the screen 
public void toFront() { 
    frame.toFront(); 
} 

// return panel width 
public int getWidth() { 
    return width; 
} 

// return panel height 
public int getHeight() { 
    return height; 
} 

// return the X position of the mouse or -1 
public int getMouseX() { 
    if (move == null) { 
    return -1; 
    } else { 
    return move.getX(); 
    } 
} 

// return the Y position of the mouse or -1 
public int getMouseY() { 
    if (move == null) { 
    return -1; 
    } else { 
    return move.getY(); 
    } 
} 

// return the X position of the last click or -1 
public int getClickX() { 
    if (click == null) { 
    return -1; 
    } else { 
    return click.getX(); 
    } 
} 

// return the Y position of the last click or -1 
public int getClickY() { 
    if (click == null) { 
    return -1; 
    } else { 
    return click.getY(); 
    } 
} 

// return true if a mouse button is pressed 
public boolean mousePressed() { 
    return pressed; 
} 

public synchronized int getKeyCode() { 
    if (keys.size() == 0) 
    return 0; 
    return keys.remove(0).keyCode; 
} 

    public synchronized char getKeyChar() { 
    if (keys.size() == 0) 
    return 0; 
    return keys.remove(0).keyChar; 
} 

    public synchronized int getKeysSize() { 
    return keys.size(); 
    } 

private synchronized void insertKeyData(char c, int code) { 
    keys.add(new KeyInfo(c,code)); 
    if (keys.size() > MAX_KEY_BUF_SIZE) { 
    keys.remove(0); 
//  System.out.println("Dropped key"); 
    } 
} 

private class KeyInfo { 
    public int keyCode; 
    public char keyChar; 

    public KeyInfo(char keyChar, int keyCode) { 
    this.keyCode = keyCode; 
    this.keyChar = keyChar; 
    } 
} 

private class DrawingPanelKeyListener implements KeyListener { 

    int repeatCount = 0; 

    public DrawingPanelKeyListener() { 
    panel.addKeyListener(this); 
    panel.requestFocus(); 
    } 

    public void keyPressed(KeyEvent event) { 
//  System.out.println("key pressed"); 
    repeatCount++; 
    if ((repeatCount == 1) || (getKeysSize() < 2)) 
     insertKeyData(event.getKeyChar(),event.getKeyCode()); 
    } 

    public void keyTyped(KeyEvent event) { 
    } 

    public void keyReleased(KeyEvent event) { 
    repeatCount = 0; 
    } 

} 

} 
+0

'Graphics g = panel.getGraphics();'不是自定義繪畫在Swing中的工作方式 - 請參閱[執行自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting/)for更多細節(以及[在AWT和Swing中繪畫](http://www.oracle.com/technetwork/java/painting-140037.html)以獲取關於繪畫實際工作情況的更多細節)。不要將UI和基於控制檯的編程混合在一起,這是一個糟糕的主意,因爲UI可能不會爲用戶打開控制檯 – MadProgrammer 2015-03-18 23:01:51

+0

第一次我聽說過Swing這個名字,說實話。我們的教授讓我們下載一個名爲DrawingPanel的java文件,我們導入它來創建圖形。這就是我們迄今爲止所做的一切。 – JBGriffin 2015-03-18 23:04:17

+0

如果你可以發佈'DrawPanel',它可能會更容易提出建議 – MadProgrammer 2015-03-18 23:11:47

回答

0

請求在DrawPanel添加創建自己的類圈,每個存儲圈的一個實例圓的位置並將這些圈子保存爲變量。

0

只是比較半徑(或圓角),其他所有信息都是無關緊要的:

public static int compareCircles(int r1, int r2){ 
    return r1 - r2;  
} 

這是比較傳統的方式,即使它不返回-1/0/1。 如果你真的想要這些返回值,你仍然可以修補。

+0

我可以爭辯和贏得不使用-1/0/1的情況,這很容易。我將在哪裏聲明compareCircles方法,以及如何從redCircle,blueCircle等存儲半徑? – JBGriffin 2015-03-18 23:17:42

+0

由於所有代碼都是靜態的,因此您必須將它們保存在繪圖方法的全局變量中,因此它可以在主體中訪問。 – Mordechai 2015-03-18 23:26:58

+0

那麼我需要在say,redCircle結尾處做一個返回int半徑?例如: 'public static int redCircle(Graphics g){ .... return radius; }' – JBGriffin 2015-03-18 23:34:59