2017-12-08 7 views
0

Es läuft, die Tasten funktionieren, aber ich kann es einfach nicht mit der Farbe zu zeichnen(). Ich denke, es hat etwas mit der Hauptmethode auf SwingDraw zu tun, aber ich bin mir nicht 100% sicher. Vielen Dank für jede Hilfe und sorry für den langen Code:/Warum löst dieser Code nicht paint() aus?

SwingDraw:

// Import Core Java packages 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.event.*; 
import javax.swing.*; 

public class SwingDraw extends JFrame implements ActionListener, ItemListener{ 

// Initial Frame size 
static final int WIDTH = 1500;    // frame width 
static final int HEIGHT = 1000;    // frame height 

// Color choices 
static final String[] COLOR_NAMES = new String[]{"None", "Red", "Blue", "Green"}; 
static final Color COLORS[] = {null, Color.red, Color.blue, Color.green }; 

// Button control 
JButton circle; 
JButton roundRec; 
JButton threeDRec; 
JButton line; 
JButton square; 
JButton oval; 

JButton clear; 
JButton delete; 

// List to keep track of shapes 
JList<String> shapesKeeper; 

// Color choice box 
JComboBox<String> colorChoice = new JComboBox<>(COLOR_NAMES); 

SwingDrawCanvas betterCanvas = new SwingDrawCanvas(); 

/** 
* Constructor 
*/ 
public SwingDraw() { 
    // Create a frame 
    setSize(WIDTH, HEIGHT); 
    setLocation(130, 100); 
    // add window closing listener 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    // create panel for controls 
    JPanel topPanel = new JPanel(); 
    JPanel leftList = new JPanel(); 



    // create button control 
    JPanel buttonPanel = new JPanel(); 
    topPanel.add(buttonPanel, BorderLayout.NORTH); 

    circle = new JButton("Circle"); 
    buttonPanel.add(circle); 
    roundRec = new JButton("Rounded Rectangle"); 
    buttonPanel.add(roundRec); 
    threeDRec = new JButton("3D Rectangle"); 
    buttonPanel.add(threeDRec); 
    line = new JButton("Line"); 
    buttonPanel.add(line); 
    square = new JButton("Square"); 
    buttonPanel.add(square); 
    oval = new JButton("Oval"); 
    buttonPanel.add(oval); 
    clear = new JButton("Clear"); 
    buttonPanel.add(clear); 

    JPanel listPanel = new JPanel(); 
    leftList.add(listPanel, BorderLayout.WEST); 
    JList<String> shapesKeeper = new JList<>(); 
    listPanel.add(shapesKeeper); 
    delete = new JButton("Delete Shape"); 
    listPanel.add(delete); 

    // add button listener 
    circle.addActionListener(this); 
    roundRec.addActionListener(this); 
    threeDRec.addActionListener(this); 
    line.addActionListener(this); 
    square.addActionListener(this); 
    oval.addActionListener(this); 
    clear.addActionListener(this); 

    // create panel for color choices 
    JPanel colorPanel = new JPanel(); 
    JLabel label = new JLabel("Filled Color:"); 
    topPanel.add(colorPanel); 
    colorPanel.add(label); 
    JComboBox<String> colorChoice = new JComboBox<>(COLOR_NAMES); 
    colorPanel.add(colorChoice); 

    colorChoice.addItemListener(this); 

    add(topPanel, BorderLayout.NORTH); 
    add(leftList, BorderLayout.WEST); 
    setVisible(true); 

} // end of constructor 


/** 
* Implementing ActionListener 
*/ 
public void actionPerformed(ActionEvent event) { 
    if(event.getSource() == circle) { // circle button 
     betterCanvas.setShape(SwingDrawCanvas.CIRCLE); 
    } 
    else if(event.getSource() == roundRec) { // rounded rectangle button 
     betterCanvas.setShape(SwingDrawCanvas.ROUNDED_RECTANGLE); 
    } 
    else if(event.getSource() == threeDRec) { // 3D rectangle button 
     betterCanvas.setShape(SwingDrawCanvas.RECTANGLE_3D); 
    } 
    else if(event.getSource() == clear){ 
     betterCanvas.setShape(SwingDrawCanvas.CLEAR); 
    } 
    else if(event.getSource() == line){ 
     betterCanvas.setShape(SwingDrawCanvas.LINE); 
    } 
    else if(event.getSource() == square){ 
     betterCanvas.setShape(SwingDrawCanvas.SQUARE); 
    } 
    else if(event.getSource() == oval){ 
     betterCanvas.setShape(SwingDrawCanvas.OVAL); 

    } 
} 

/** 
* Implementing ItemListener 
*/ 
public void itemStateChanged(ItemEvent event) { 
    Color color = COLORS[colorChoice.getSelectedIndex()]; 
    betterCanvas.setFilledColor(color); 
} 

/** 
* the main method 
*/ 
public static void main(String[] argv) { 
    new SwingDraw(); 
} 
} 

SwingDrawCanvas:

public class SwingDrawCanvas extends JPanel implements MouseListener, MouseMotionListener { 

// Constants for shapes 
public static final int CIRCLE = 1; 
public static final int ROUNDED_RECTANGLE = 2; 
public static final int RECTANGLE_3D = 3; 
public static final int LINE = 4; 
public static final int SQUARE = 5; 
public static final int OVAL = 6; 

public static final int CLEAR = 7; 

// Coordinates of points to draw 
private int x1, y1, x2, y2; 

// shape to draw 
private int shape = CIRCLE; 
/** 
* Method to set the shape 
*/ 
public void setShape(int thisShape) { 
    System.out.println("HEY"); 
    this.shape = thisShape; 
    System.out.println(shape); 
} 

// filled color 
private Color filledColor = null; 
/** 
* Method to set filled color 
*/ 
public void setFilledColor(Color color) { 
    filledColor = color; 
} 

/** 
* Constructor 
*/ 
public SwingDrawCanvas() { 
    addMouseListener(this); 
    addMouseMotionListener(this); 
} // end of constructor 

/** 
* painting the component 
*/ 
public void paint(Graphics g) { 

    System.out.println("ARHFHASJDASHDHAs"); 
    super.paint(g); 

    System.out.println("AHAHAHAHAHAHAHA"); 

    g.drawString("FUCK", 1, 1); 

    // the drawing area 
    int x, y, width, height; 

    // determine the upper-left corner of bounding rectangle 
    x = Math.min(x1, x2); 
    y = Math.min(y1, y2); 

    // determine the width and height of bounding rectangle 
    width = Math.abs(x1 - x2); 
    height = Math.abs(y1 - y2); 


    if(filledColor != null) 
     g.setColor(filledColor); 
    switch (shape) { 
     case ROUNDED_RECTANGLE : 
      if(filledColor == null) 
       g.drawRoundRect(x, y, width, height, width/4, height/4); 
      else 
       g.fillRoundRect(x, y, width, height, width/4, height/4); 
      break; 
     case CIRCLE : 
      int diameter = Math.max(width, height); 
      if(filledColor == null) 
       System.out.println("HRE BITCHS"); 
      else 
       System.out.println("HEY FUCK YOU GUY"); 
      break; 
     case RECTANGLE_3D : 
      if(filledColor == null) 
       g.draw3DRect(x, y, width, height, true); 
      else 
       g.fill3DRect(x, y, width, height, true); 
      break; 
    } 
} 

/** 
* Implementing MouseListener 
*/ 
public void mousePressed(MouseEvent event) { 
    x1 = event.getX(); 
    y1 = event.getY(); 
} 

public void mouseReleased(MouseEvent event) { 
    x2 = event.getX(); 
    y2 = event.getY(); 
    repaint(); 
} 

public void mouseClicked(MouseEvent e) {} 
public void mouseEntered(MouseEvent e) {} 
public void mouseExited(MouseEvent e) {} 

/** 
* Implementing MouseMotionListener 
*/ 
public void mouseDragged(MouseEvent event) { 
    x2 = event.getX(); 
    y2 = event.getY(); 
    repaint(); 
} 

public void mouseMoved(MouseEvent e) {} 

}

ich nicht sehr gut bin, ich Ich suche nur irgendeine Hilfe, die Sie mir geben können: D

+0

Sie müssen 'betterCanvas' zu Ihrem Bild in' SwingDraw() 'hinzufügen. –

+0

Ihr 'SwingDrawCanvas'-Panel wurde nie zum Frame hinzugefügt. – Berger

+0

Zusätzlich zu diesen Tipps ändern Sie 'public void paint (Graphics g) {' in '@Override public void paintComponent (Graphics g) {' 1) Die Methode '@ Override' prüft zur Kompilierzeit, ob die Methode existiert. Es ist nur eine gute Methode, sie zu verwenden, wenn Sie eine Methode außer Kraft setzen. 2) Die richtige Methode, mit der eine benutzerdefinierte Bemalung für jede 'JComponent' oder Klasse durchgeführt werden kann, die sie erweitert, die 'paintComponent (..)' Methode. –

Antwort

0

Anstatt paint() zu haben, Ihre Funktion sollte Name paintComponent(Graphics g) sein. Beim Erstellen der paintComponent(Graphics g) Funktionen benötigen Sie @Override, so dass Java die Funktion erkennt, weil Sie paintComponent(Graphics g) nicht direkt aufrufen können.

Darüber hinaus können Sie jederzeit, wenn Sie Ihr Panel aktualisieren möchten, repaint() aufrufen oder Sie können die Größe Ihres JFrames nach dem kompilierten Code ändern. Beide Methoden rufen die paintComponent(Graphics g)-Funktion auf.

Verwandte Themen