Hey guys! I'm new to Java and really like learning it! But I've encountered a little snag. I wrote a small program and for some reason it's only executing two of the three classes. Here is the code:
Main Method class:
import javax.swing.JFrame; public class main { public static void main(String[] args) { JFrame frame = new JFrame(); Infout m = new Infout(); obj2 o = new obj2(); frame.add(o); frame.add(m); frame.setVisible(true); frame.setDefaultCloseOperation(3); frame.setSize(300, 400); frame.setTitle("Circle"); } }
Class that draws a keyboard-controlled circle.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Infout extends JPanel implements ActionListener, KeyListener { Timer t = new Timer(5, this); double x = 0, y = 0, velx = 0, vely = 0; public Infout(){ t.start(); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.fill(new Ellipse2D.Double(x, y, 40, 40)); g2.fill(new Rectangle2D.Double(0, 270, 300, 5)); g2.fill(new Rectangle2D.Double(140, 270, 5, 300)); g2.fill(new Rectangle2D.Double(140, 60, 70, 5)); g2.fill(new Rectangle2D.Double(50, 140, 5, 70)); g2.fill(new Rectangle2D.Double(150, 130, 5, 40)); g2.fill(new Rectangle2D.Double(190, 210, 40, 5)); if (x >= 120 && y >= 270) { System.out.println("sum1 has reached tha corner"); g.drawString("You win!",115,35); } if (x <= 120 && y >= 270) { System.out.println("sum1 has reached tha corner"); g.drawString("You lose!",115,35); } if (x == 120 && y >= 270){ velx = 0; vely = 0; } if (x == 31.5 && y <= 200 && y >= 100){ velx = 0; } if (x == 132 && y <= 170 && y >= 100){ velx = 0; } if (x <= 190 && x >= 120 && y == 42){ velx = 0; } if (x <= 210 && x >= 171 && y == 192){ velx = 0; } } public void actionPerformed(ActionEvent e) { System.out.println("x "+x+"y "+y); repaint(); x += velx; y += vely; if (x < 0 || x > 260) { velx = 0; vely = 0; } if (y < 0 || y > 340) { velx = 0; vely = 0; } } public void up() { vely = -1.5; velx = 0; } public void down() { vely = 1.5; velx = 0; } public void left() { velx = -1.5; vely = 0; } public void right() { velx = 1.5; vely = 0; } public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if (code == KeyEvent.VK_UP) { up(); } if (code == KeyEvent.VK_DOWN) { down(); } if (code == KeyEvent.VK_RIGHT) { right(); } if (code == KeyEvent.VK_LEFT) { left(); } } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} }
Class that's supposed to draw a static circle but for some reason, is not showing up on the JFrame :
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import javax.swing.JPanel; public class obj2 extends JPanel { public void paintComponent(Graphics g1) { super.paintComponent(g1); Graphics2D g3 = (Graphics2D)g1; Ellipse2D circle = new Ellipse2D.Double(50.0D, 50.0D, 40.0D, 40.0D); g3.fill(circle); } }
So basically, main.java and Infout.java work, but obj2.java is not executing at all. It's supposed to draw a circle on the JFrame but isn't. Am I doing something wrong?
Thanks!
- Ab