Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forums:
Beginning Java
Swing / AWT / SWT
Why wont my code run? Does Anyone Know What I am Doing Wrong?
Ben Jonesson
Greenhorn
Posts: 1
posted 5 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JTextField; import javax.swing.JOptionPane; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.SwingUtilities; /* * Represents the graphics display panel within the turtle program. This panel contains an image * which is updated to reflect user commands. */ public class GraphicsPanel extends JPanel { private JTextField console = new JTextField(15); public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); } ImageIcon saveicon = new ImageIcon("images/save.png"); ImageIcon abouticon = new ImageIcon("images/about.png"); ImageIcon newicon = new ImageIcon("images/new.png"); ImageIcon loadicon = new ImageIcon("images/load.png"); ImageIcon exiticon = new ImageIcon("images/exit.png"); JMenuBar menuBar; JMenu file; JMenu help; JMenuItem load; JMenuItem save; private static void createAndShowGUI() { JFrame frame = new JFrame("Turtle Graphics"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new GraphicsPanel()); frame.pack(); frame.setVisible(true); } /* * The default BG colour of the image. */ private final static Color BACKGROUND_COL = Color.DARK_GRAY; private final static int TURTLE_X_SIZE = 8, TURTLE_Y_SIZE = 8; /* * The underlying image used for drawing. This is required so * any previous drawing activity is persistent on the panel. */ private BufferedImage image, turtleDisplay; // djm added private Color PenColour = Color.RED; private boolean penDown = false; private int xPos = 400 - TURTLE_X_SIZE/2, yPos = 200 - TURTLE_Y_SIZE/2; private int direction = 180; // robot pointing down the screen private JMenuItem newfile; private JMenuItem exit; private JMenuItem about; private int distance = 100; /* * Draw a line on the image using the given colour. */ public void drawLine(Color color, int x1, int y1, int x2, int y2) { Graphics graphics = image.getGraphics(); graphics.setColor(color); graphics.drawLine(x1, y1, x2, y2); } // djm added commands public void penDown() { penDown = true; } public void penUp() { penDown = false; } public void turnRight() { direction += 90; if (direction >= 360) { direction -= 360; } } public void turnLeft() { direction -= 90; if (direction < 0) { direction += 360; } } public void forward(int distance) { int x = xPos, y = yPos; // stored xPos and yPos are current location if (direction == 0) // robot facing up the screen, so forward subtracts y { y = yPos - distance; } else if (direction == 90) // robot facing right so forward add x { x = xPos + distance; } else if (direction == 180) // robot facing down the screen, so forwards adds to y { y = yPos + distance; } else if (direction == 270) // robot facing left, so forwards subtracts from x { x = xPos - distance; } else { System.out.println("strange, shouldn't get here"); } if (penDown) { drawLine(PenColour, xPos, yPos, x, y); } // now robot has moved to the new position xPos = x; yPos = y; repaint(); } /* * Clear the image contents. */ public void clear() { Graphics graphics = image.getGraphics(); graphics.setColor(BACKGROUND_COL); graphics.fillRect(0, 0, image.getWidth(), image.getHeight()); repaint(); } public void setColour(Color color) { Graphics graphics = turtleDisplay.getGraphics(); graphics.setColor(color); graphics.fillRect(0, 0, turtleDisplay.getWidth(), turtleDisplay.getHeight()); graphics = image.getGraphics(); graphics.setColor(color); PenColour = color; repaint(); } public void green() { setColour(Color.GREEN); } public void red() { setColour(Color.RED); } public void black() { setColour(Color.BLACK); } @Override public void paintComponent(Graphics graphics) { super.paintComponent(graphics); graphics.drawImage(image, 0, 0, null); graphics.drawImage(turtleDisplay, xPos - TURTLE_X_SIZE/2, yPos - TURTLE_Y_SIZE/2, null); } /* * Constructor. */ public GraphicsPanel() { add(console); console.addActionListener(new ActionListener() /* Command List */ { public void actionPerformed(ActionEvent arg0) { if (console.getText().contains("penup")) { penUp(); } else if (console.getText().contains("pendown")) { penDown(); } else if (console.getText().contains("turnleft")) { turnLeft(); } else if (console.getText().contains("turnright")) { turnRight(); } else if (console.getText().contains("forward")) { forward(distance); } else if (console.getText().contains("reset")) { clear(); } else if (console.getText().contains("green")) { green(); } else if (console.getText().contains("red")) { red(); } else if (console.getText().contains("black")) { black(); } else if (console.getText().contains("reset")) { clear(); } else { JOptionPane.showMessageDialog(console, "Invalid command, try again"); } console.setText(""); } } ); menuBar = new JMenuBar(); file = new JMenu("File"); load = new JMenuItem("Load", loadicon); save = new JMenuItem("Save", saveicon ); newfile = new JMenuItem("New", newicon); exit = new JMenuItem("Exit", exiticon); about = new JMenuItem("About", abouticon); file.add(load); file.add(save); file.add(newfile); file.add(exit); help.add(about); menuBar.add(file); help = new JMenu("Help"); about = new JMenuItem("About"); help.add(about); menuBar.add(help); add(menuBar); setBorder(BorderFactory.createLineBorder(Color.black)); } public Dimension getPreferredSize() { return new Dimension(800, 400); } { // main drawing area image = new BufferedImage(800, 400, BufferedImage.TYPE_INT_RGB); // small image to display on top of drawing area to represent the turtle turtleDisplay = new BufferedImage(TURTLE_X_SIZE, TURTLE_Y_SIZE, BufferedImage.TYPE_INT_RGB); // set up turtle setColour(PenColour); // Set max size of the panel, so that is matches the max size of the image. setMaximumSize(new Dimension(image.getWidth(), image.getHeight())); setSize(800, 400); setVisible(true); clear(); } }
Campbell Ritchie
Marshal
Posts: 79178
377
posted 5 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome to the Ranch
What happens when you try that code? What sort of error messages do you get?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Trouble with a setX method
Fluid Motion Moving Circles Smooth -solved-
Gray box where my applet should be
How do I save a variable value -Unsolved-
Circle collision Detection: Solved =)
More...