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; } }

以上就是Adding more JComponents to JFrame的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏