Lukas Hruby
Lukas Hruby

Reputation: 75

Adding more JComponents to JFrame

Single keys (in code KeyboardButtons) extends JComponent. When i'm trying to add the to main JFrame, i can do that for single key, when trying to add another one, the first one is not showing.

Can you please look at the code a tell me where the problem is?

MainFrame.java:

package keyboard;

import java.awt.*;
import javax.swing.JFrame;

public class MainFrame extends JFrame {


public MainFrame() throws HeadlessException {
    setTitle("Keyboard");
    setSize(1024, 768);

}

public static void main(String[] args) throws InterruptedException {
    JFrame mainWindow = new MainFrame();
    mainWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);

    Point left5 = new Point(210, 210);
    Point left4 = new Point(410, 110);
    Point left3 = new Point(580, 120);
    Point left2 = new Point(680, 200);
    Point left1 = new Point(800, 500);

    Keyboard kb = new Keyboard(left1, left2, left3, left4, left5);
    KeyboardButton[] buttons = kb.registerKeys();

    Container c = mainWindow.getContentPane();
    c.add(buttons[0]);
    c.add(buttons[1]);
    mainWindow.setVisible(true);

}


}

KeyboardButton.java:

package keyboard;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.*;
import javax.swing.JComponent;

public class KeyboardButton extends JComponent implements MouseListener {

Polygon polygon;
boolean isActive;
final Color ACTIVE_COLOR = Color.red;
final Color INACTIVE_COLOR = Color.blue;

public KeyboardButton(Polygon p) {
    polygon = p;
    addMouseListener(this);
}

private void checkMousePosition(MouseEvent e) {
    if (polygon.contains(e.getX(), e.getY())) {
    setState(true);
    }
}

public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
    checkMousePosition(e);
    System.out.println(this + " pressed");
}

public void mouseReleased(MouseEvent e) {
    setState(false);
    System.out.println(this + " released");

}

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.setColor(isActive ? ACTIVE_COLOR : INACTIVE_COLOR);
    g.drawPolygon(polygon);

}

void setState(boolean state) {
    isActive = state;
    repaint();
}
}

Keyboard.java:

package keyboard;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JComponent;

public class Keyboard extends JComponent {

Point[] leftFingers;
Point leftCenter = new Point(300, 600);

public Keyboard(Point left1, Point left2, Point left3, Point left4, Point left5) {
    leftFingers = new Point[5];
    leftFingers[0] = left1;
    leftFingers[1] = left2;
    leftFingers[2] = left3;
    leftFingers[3] = left4;
    leftFingers[4] = left5;
}

public KeyboardButton[] registerKeys() {
    Polygon[] polygons = generateKeyPolygons(calculateBordersOfKeys(calculateCentersBetweenEachTwoFingers(leftFingers)));
    KeyboardButton[] buttons = new KeyboardButton[5];
    for (int i = 0; i < polygons.length; i++) {
    buttons[i] = new KeyboardButton(polygons[i]);
    }
    return buttons;
}

private Point[] calculateBordersOfKeys(Point[] fingers) {
    Point[] centers = calculateCentersBetweenEachTwoFingers(fingers);
    Point[] result = new Point[6];

    result[0] = calculateCentralSymmetry(centers[0], fingers[0]);
    System.arraycopy(centers, 0, result, 1, centers.length);
    result[5] = calculateCentralSymmetry(centers[3], fingers[4]);
    return result;
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.red);
    g.drawOval(leftCenter.x - 25, leftCenter.y - 25, 50, 50);

    for (int i = 0; i < leftFingers.length; i++) {
    g.drawOval(leftFingers[i].x, leftFingers[i].y, 10, 10);

    }
}

private Polygon[] generateKeyPolygons(Point[] borders) {
    Polygon[] polygons = new Polygon[5];
    for (int i = 0; i < borders.length - 1; i++) {
    Polygon p = new Polygon();
    p.addPoint(leftCenter.x, leftCenter.y);
    p.addPoint(borders[i].x, borders[i].y);
    p.addPoint(borders[i + 1].x, borders[i + 1].y);
    polygons[i] = p;
    }

    return polygons;
}

private Point[] calculateCentersBetweenEachTwoFingers(Point[] fingers) {
    Point[] centers = new Point[4];
    for (int i = 0; i < fingers.length - 1; i++) {
    centers[i] = new Point(((fingers[i].x + fingers[i + 1].x) / 2), ((fingers[i].y + fingers[i + 1].y) / 2));
    }
    return centers;
}

private Point calculateCentralSymmetry(Point toReflected, Point center) {
    Point reflection = new Point();

    if (toReflected.x > center.x) {
    reflection.x = center.x - Math.abs(center.x - toReflected.x);
    } else {
    reflection.x = center.x + Math.abs(center.x - toReflected.x);
    }

    if (toReflected.y > center.y) {
    reflection.y = center.y - Math.abs(center.y - toReflected.y);
    } else {
    reflection.y = center.y + Math.abs(center.y - toReflected.y);
    }

    return reflection;
}
}

Upvotes: 0

Views: 639

Answers (1)

Ofek Ron
Ofek Ron

Reputation: 8580

Try to use another LayoutManager, or for what it seems to me it looks like you are trying to manualy paint shapes on the screen, i'd suggest painting them all on one layer. (have one JComponent's paintComponent() method which calls to KeyboardButton.paint() and other painting methods, then you can just add that one JComponent)

Upvotes: 1

Related Questions