2015-07-30 3 views
0

Так что это мой код:JFrame краска сбой?

package project; 

import java.awt.Graphics; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 

import javax.swing.JFrame; 


public class Project extends JFrame { 
String thing = "none"; 
int x = 100; 
int y = 100; 
int w = 1600; 
int h = 800; 
int ww = 5; 
int hh = 5; 
public class AL extends KeyAdapter{ 
    public void keyPressed(KeyEvent e) { 
     int KeyCode = e.getKeyCode(); 
     if (KeyCode == e.VK_NUMPAD1){ 
      downleft(); 
      if (thing == "none" || thing == "d" || thing == "dr" || thing == "l" || thing == "r" || 
        thing == "ul" || thing == "u" || thing == "ur") { 
       thing = "dl"; 
       System.out.println("Drawing down to the left!"); 
      } 
     } 
     if (KeyCode == e.VK_NUMPAD2){ 
      down(); 
      if (thing == "none" || thing == "dl" || thing == "dr" || thing == "l" || thing == "r" || 
        thing == "ul" || thing == "u" || thing == "ur") { 
       thing = "d"; 
      System.out.println("Drawing down!"); 
      } 
     } 
     if (KeyCode == e.VK_NUMPAD3){ 
      downright(); 
      if (thing == "none" || thing == "dl" || thing == "d" || thing == "l" || thing == "r" || 
        thing == "ul" || thing == "u" || thing == "ur") { 
       thing = "dr"; 
      System.out.println("Drawing down to the right!"); 
      } 
     } 
     if (KeyCode == e.VK_NUMPAD4){ 
      left(); 
      if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "r" || 
        thing == "ul" || thing == "u" || thing == "ur") { 
       thing = "l"; 
      System.out.println("Drawing to the left"); 
      } 
     } 
     if (KeyCode == e.VK_NUMPAD6){ 
      right(); 
      if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" || 
        thing == "ul" || thing == "u" || thing == "ur") { 
       thing = "r"; 
      System.out.println("Drawing to the right!"); 
      } 
     } 
     if (KeyCode == e.VK_NUMPAD7){ 
      upleft(); 
      if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" || 
        thing == "r" || thing == "u" || thing == "ur") { 
       thing = "ul"; 
      System.out.println("Drawing up to the left!"); 
      } 
     } 
     if (KeyCode == e.VK_NUMPAD8){ 
      up(); 
      if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" || 
        thing == "r" || thing == "ul" || thing == "ur") { 
       thing = "u"; 
      System.out.println("Drawing up!"); 
      } 
     } 
     if (KeyCode == e.VK_NUMPAD9){ 
      upright(); 
      if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" || 
        thing == "r" || thing == "ul" || thing == "u") { 
       thing = "ur"; 
      System.out.println("Drawing up to the right!"); 
      } 
     } 
     if (KeyCode == e.VK_B){ 
      ww += 5; 
      hh += 5; 
      System.out.println("Resized! It is now " + ww + " by " + hh + "!"); 
      if (ww > 150) { 
       ww = 150; 
       hh = 150; 
       System.out.println("The size went too high! Resized back to 150 by 150!"); 
      } 
     } 
     if (KeyCode == e.VK_S){ 
      ww -= 5; 
      hh -= 5; 
      System.out.println("Resized! It is now " + ww + " by " + hh + "!"); 
      if (ww < 5) { 
       ww = 5; 
       hh = 5; 
       System.out.println("The size went too low! Resized back to 5 by 5!"); 
      } 
     } 
    } 
} 
public void downleft(){ 
    x-=5; 
    y+=5; 
} 
public void down(){ 
    y+=5; 
} 
public void downright(){ 
    x+=5; 
    y+=5; 
} 
public void left(){ 
    x-=5; 
} 
public void right(){ 
    x+=5; 
} 
public void upleft(){ 
    x-=5; 
    y-=5; 
} 
public void up(){ 
    y-=5; 
} 
public void upright(){ 
    x+=5; 
    y-=5; 
} 
public Project(){ 
    addKeyListener(new AL()); 
    setTitle("Escher Sketch"); 
    setResizable(false); 
    setVisible(true); 
    setSize(w, h); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 
public void paint(Graphics g) { 
    g.fillRect(x, y, ww, hh); 
    repaint(); 
} 
public static void main(String[] args) { 
    new Project(); 
} 
} 

По какой-то причине, когда я запускаю его это просто черное окно с маленьким белым rectanble в верхнем левом углу. Я читал код много раз, и мне кажется, что он должен работать. Он работал на моем другом компьютере, затем я попытался поместить файл на этот компьютер, и это не так. Если бы кто-нибудь мог мне помочь, это было бы здорово.

+0

У обоих компьютеров аналогичные версии Java установлены? Я понимаю, что время от времени экспорт кода Java может привести к сбою работы на некоторых машинах из-за его кодирования. – SomeStudent

+0

Я знаю, что тот, на котором он не работает, имеет последнюю версию, но я не уверен в том, на каком я его сделал. – Rep0man

+0

a. Не рекомендуется рисовать на JFrame ... Скорее, нарисуйте JPanel и добавьте это в контентную панель JFrame.
b. не переписывайте в свой метод рисования. Вместо этого используйте что-то вроде класса таймера качания для плановой перерисовки. – ControlAltDel

ответ

0

Изменить код из Project() части в этом:

public Project(){ 
    addKeyListener(new AL()); 
    setTitle("Escher Sketch"); 
    setResizable(false); 
    setVisible(true); 
    setSize(w, h); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    getContentPane().add(new MyGraphics()); 
} 
public static void main(String[] args) { 
    new Project(); 
} 
class MyGraphics extends JPanel { 
    public void paintComponent(Graphics g) { 
     g.fillRect(x, y, ww, hh); 
     repaint(); 
    } 
} 
}