tutorials.de Buch-Aktion 05/2012
ERLEDIGT
JA
ANTWORTEN
9
ZUGRIFFE
728
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Code46 Code46 ist offline Mitglied Gold
    Registriert seit
    Sep 2008
    Beiträge
    184
    Hi Forum,
    Ich habe ein Sudoku programm mit einem Grid.Wenn die eine Line erstelle, zeichnet er das hinter dem Grid. Wie kann ich das ins Vordergrund bringen ?


    Würde mich auf eure post freuen
     

  2. #2
    Avatar von zerix
    zerix zerix ist offline Hausmeister
    tutorials.de Moderator
    Registriert seit
    May 2005
    Beiträge
    4.335
    Hallo,

    es wäre von Vorteil, wenn du etwas Code posten würdest. Sonst kann man leider nur raten, da wir ja nicht wissen wie du dein Programm aufgebaut hast.

    Gruß

    Sascha
     
    Es ist schwer Allwissend zu sein. Aber ich komme damit klar. ;-)

  3. #3
    Code46 Code46 ist offline Mitglied Gold
    Registriert seit
    Sep 2008
    Beiträge
    184
    Dies ist mein Code:
    public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Line2D x1 = new Line2D.Float(0, 266, 798, 266);
    Line2D x2 = new Line2D.Float(0, 404, 798, 404);
    Line2D y1 = new Line2D.Float(271, 128, 271, 541);
    Line2D y2 = new Line2D.Float(535, 128, 535, 541);
    g2.draw(x1);
    g2.draw(x2);
    g2.draw(y1);
    g2.draw(y2);



    }
     

  4. #4
    Avatar von zerix
    zerix zerix ist offline Hausmeister
    tutorials.de Moderator
    Registriert seit
    May 2005
    Beiträge
    4.335
    Wie du eine Linie zeichnest ist mir schon klar. Mal abgesehen davon, dass man die paintComponent und nicht die paint-Methode überschreiben sollte.

    Poste mal bitte noch den restlichen Code, damit man den Aufbau deiner GUI sieht.

    Gruß

    Sascha
     
    Es ist schwer Allwissend zu sein. Aber ich komme damit klar. ;-)

  5. #5
    Code46 Code46 ist offline Mitglied Gold
    Registriert seit
    Sep 2008
    Beiträge
    184
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    
    import java.io.FileWriter;
    import java.io.File;
    import java.io.IOException;
    import java.awt.geom.Line2D;
    import java.awt.Graphics2D;
    import java.awt.Graphics;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.HashMap;
    import java.util.Formatter;
    import static java.awt.Color.*;
    import static sudokupuzzle.Puzzle.*;
     
     
    /**
     * The main class for running the Sudoku  program.  The idea is
     * that the program presents you with a Sudoku puzzle to solve.  The user
     * has some control over how difficult the puzzle is by choosing how many
     * blank cells there are.  
     * 
     * The program can check a partially completed puzzle for duplicate values
     * (row, column or region).
     * 
     * The program allows the user to associate notes with cells
     * to indicate values that they think are likely or unlikely.  Notes are
     * accessed by right-clicking a cell.  Cells with notes are
     * highlighted.
     * 
     * The time taken to complete a puzzle is displayed.
     */
    public class SudokuView extends javax.swing.JFrame {
        
        /**
         * Normal constructor.
         */
        public SudokuView() {
            initComponents();
            openNotes = new HashMap<CellCoordinates, CellNoteView>();
        }
     
        
     
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {
     
            jPanel4 = new javax.swing.JPanel();
            jPanel2 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            btnDown = new javax.swing.JButton();
            txtNumberOfBlanks = new javax.swing.JTextField();
            btnUp = new javax.swing.JButton();
            btnGetPuzzle = new javax.swing.JButton();
            btnCheck = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jPanel3 = new javax.swing.JPanel();
            jLabel2 = new javax.swing.JLabel();
            lblTimer = new javax.swing.JLabel();
            btnPauseRestart = new javax.swing.JButton();
            jPanel1 = new JPanel(){
            public void paint(Graphics g){
            super.paint(g);
            paint(g);
            }
            };
            //post create
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Sudoku Puzzle");
     
            jPanel4.setLayout(new java.awt.GridLayout(2, 0));
     
            jPanel2.setOpaque(false);
            jPanel2.setPreferredSize(new java.awt.Dimension(800, 50));
     
            jLabel1.setFont(new java.awt.Font("Verdana", 0, 14));
            jLabel1.setText("Blanks");
            jPanel2.add(jLabel1);
     
            btnDown.setFont(new java.awt.Font("Verdana", 0, 14));
            btnDown.setText("<<");
            btnDown.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnDownActionPerformed(evt);
                }
            });
            jPanel2.add(btnDown);
     
            txtNumberOfBlanks.setFont(new java.awt.Font("Verdana", 0, 14));
            txtNumberOfBlanks.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
            txtNumberOfBlanks.setText("9");
            txtNumberOfBlanks.setPreferredSize(new java.awt.Dimension(30, 30));
            txtNumberOfBlanks.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txtNumberOfBlanksActionPerformed(evt);
                }
            });
            jPanel2.add(txtNumberOfBlanks);
     
            btnUp.setFont(new java.awt.Font("Verdana", 0, 14));
            btnUp.setText(">>");
            btnUp.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnUpActionPerformed(evt);
                }
            });
            jPanel2.add(btnUp);
     
            btnGetPuzzle.setFont(new java.awt.Font("Verdana", 0, 14));
            btnGetPuzzle.setText("Get Puzzle");
            btnGetPuzzle.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnGetPuzzleActionPerformed(evt);
                }
            });
            jPanel2.add(btnGetPuzzle);
     
            btnCheck.setFont(new java.awt.Font("Verdana", 0, 14));
            btnCheck.setText("Check");
            btnCheck.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnCheckActionPerformed(evt);
                }
            });
            jPanel2.add(btnCheck);
     
            jButton3.setFont(new java.awt.Font("Verdana", 0, 14));
            jButton3.setText("Blank Page");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
            jPanel2.add(jButton3);
     
            jButton2.setFont(new java.awt.Font("Verdana", 0, 14));
            jButton2.setText("Save");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
            jPanel2.add(jButton2);
     
            jPanel4.add(jPanel2);
     
            jPanel3.setPreferredSize(new java.awt.Dimension(400, 30));
     
            jLabel2.setFont(new java.awt.Font("Verdana", 0, 14));
            jLabel2.setText("elapsed");
            jPanel3.add(jLabel2);
     
            lblTimer.setBackground(new java.awt.Color(51, 51, 51));
            lblTimer.setFont(new java.awt.Font("Verdana", 0, 14));
            lblTimer.setForeground(new java.awt.Color(255, 255, 255));
            lblTimer.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
            lblTimer.setOpaque(true);
            lblTimer.setPreferredSize(new java.awt.Dimension(50, 18));
            jPanel3.add(lblTimer);
     
            btnPauseRestart.setFont(new java.awt.Font("Verdana", 0, 14));
            btnPauseRestart.setText("Pause");
            btnPauseRestart.setEnabled(false);
            btnPauseRestart.setPreferredSize(new java.awt.Dimension(90, 27));
            btnPauseRestart.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnPauseRestartActionPerformed(evt);
                }
            });
            jPanel3.add(btnPauseRestart);
     
            jPanel4.add(jPanel3);
     
            getContentPane().add(jPanel4, java.awt.BorderLayout.NORTH);
     
            jPanel1.setPreferredSize(new java.awt.Dimension(420, 420));
            //post init
     
            txtCell = new JTextField[9][9];
     
            for (int i = 0; i < txtCell.length; i++) {
                for (int j = 0; j < txtCell[0].length; j++) {
                    txtCell[i][j] = new JTextField();
                    txtCell[i][j].setHorizontalAlignment(javax.swing.JTextField.RIGHT);
                    txtCell[i][j].setFont(new java.awt.Font("Verdana", 0, 14));
                    txtCell[i][j].setPreferredSize(new java.awt.Dimension(30, 30));
                    txtCell[i][j].setEditable(false);
                    txtCell[i][j].addMouseListener(new java.awt.event.MouseAdapter() {
                        public void mouseClicked(java.awt.event.MouseEvent evt) {
                            txtCellMouseClicked(evt);
                        }
                    });
                    jPanel1.add(txtCell[i][j]);
                }
            }
            jPanel1.setLayout(new java.awt.GridLayout(9, 9));
            getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
     
            pack();
        }// </editor-fold>//GEN-END:initComponents
     
        /**
         * The Pause/Restart button has been clicked to pause or restart the 
         * timer.
         * @param evt The ActionEvent
         */
        private void btnPauseRestartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPauseRestartActionPerformed
            if (tim != null && tim.isRunning()) { // timer is running so need to pause
                tim.stop();
                btnPauseRestart.setText("Restart");
            }
            else { // not running so need to restart
                startTiming(true);
                btnPauseRestart.setText("Pause");            
            }
        }//GEN-LAST:event_btnPauseRestartActionPerformed
     
        /**
         * The Check button has been pressed so the puzzle needs to be checked for
         * duplicates.  If there are no duplicates and no empty cells then the
         * puzzle has been solved.
         * @param evt The ActionEvent
         */
        private void btnCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCheckActionPerformed
     
            char [] [] grid = new char [txtCell.length] [txtCell[0].length];
            for (int i = 0; i < txtCell.length; i++) {
                for (int j = 0; j < txtCell[0].length; j++) {
                    if (!txtCell[i][j].getText().trim().equals("")) { // something in the cell
                        grid [i][j] = txtCell[i][j].getText().charAt(0);
     
                    } else { // cell is empty
                        grid [i][j] = EMPTY_CELL;
                       
                    }
           
                    // Set background for cell.  GREEN means it has a note,
                    // WHITE means no note and is editable, LIGHT_GRAY means
                    // it is one of the fixed cells in the puzzle and so can't
                    // be edited.
                    if (txtCell[i][j].isEditable()) {
                       if (cellNotes.getNote(new CellCoordinates(i + 1, j + 1)) != null) { // it has a note
                           txtCell[i][j].setBackground(GREEN);
                       }
                       else { // no note
                        txtCell[i][j].setBackground(WHITE);
                       
                       }
                        
                    }
                    else {
                        txtCell[i][j].setBackground(LIGHT_GRAY);   
                    }
     
     
                }
     
            }    
         
    JOptionPane.showMessageDialog(null,"Some Boxes are still empty");
     
     
     
     
     
            // Use the Puzzle class to find if there are any duplicates and set
            // the background to red of any duplicates found.
            Puzzle p = new Puzzle (grid);
            int [][][]duplicates = p.getCoordinatesOfDuplicates();
            if (duplicates != null) {
                for (int [][] coords : duplicates) {
                    txtCell[coords[0][0]][coords[0][1]].setBackground(RED);
                    txtCell[coords[1][0]][coords[1][1]].setBackground(RED);
                }
            
            }
     
            if (p.isSolved()) {
                tim.stop();
                btnPauseRestart.setText("Pause");
                btnPauseRestart.setEnabled(false);
                JOptionPane.showMessageDialog(null,"Solved in " + (System.currentTimeMillis() - startTime) + " millisecs");
            }
       
            
            
        }//GEN-LAST:event_btnCheckActionPerformed
     
        /**
         * The "get puzzle" button has been clicked and so a new Puzzle must
         * be generated and displayed.
         * @param evt The ActionEvent
         */
        private void btnGetPuzzleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGetPuzzleActionPerformed
            // Generate a puzzle with the number of blanks requested by the user.
            PuzzleGenerator pg = new PuzzleGenerator();  
            int numBlanks = Integer.parseInt(txtNumberOfBlanks.getText());
             int blanks = Integer.parseInt(txtNumberOfBlanks.getText());
            if(blanks <= 0)
            {
            JOptionPane.showMessageDialog(null, "The Value is less or equal to 0");
     
            } else if (blanks >= 81)
            {
            JOptionPane.showMessageDialog(null, "The Value is bigger or equal then 81");
             }
     else{
     Puzzle p = new Puzzle(pg.getPuzzle(numBlanks));
     
            cellNotes = new CellNotes();
            
            // Get the values of the cells from the generated puzzle
            // and display them.  The fixed values that have been 
            // generated are not editable and have a LIGHT_GRAY background.  The blanks
            // are editable and have a WHITE background.
            for (int i = 0; i < txtCell.length; i++) {
                for (int j = 0; j < txtCell[0].length; j++) {
                    int value = p.getCell(i, j);
                    if (value != EMPTY_CELL) {
                        txtCell[i][j].setText("" + value);
                        txtCell[i][j].setEditable(false);
                        txtCell[i][j].setBackground(LIGHT_GRAY);
                    }
                    else {
                        txtCell[i][j].setText("");    
                        txtCell[i][j].setEditable(true);  
                        txtCell[i][j].setBackground(WHITE);
                    }
                }
            }
     
            // Get rid of any notes windows that may still be open from a
            // previous puzzle.
            for (CellNoteView cnv: openNotes.values()) {
                cnv.dispose();
            }
            openNotes = new HashMap<CellCoordinates, CellNoteView>();
            
            // Initialise the timer
            btnPauseRestart.setText("Pause");
            btnPauseRestart.setEnabled(true);
            if (tim != null) { // Stop any previous timer if still running.
                tim.stop();
            }
            startTiming(false);
            }
        }//GEN-LAST:event_btnGetPuzzleActionPerformed
     
        private void btnDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDownActionPerformed
            int blanks = Integer.parseInt(txtNumberOfBlanks.getText());
            blanks--;
            txtNumberOfBlanks.setText(blanks + "");
    }//GEN-LAST:event_btnDownActionPerformed
     
        private void btnUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpActionPerformed
            int blanks = Integer.parseInt(txtNumberOfBlanks.getText());
            blanks++;
            txtNumberOfBlanks.setText(blanks + "");
    }//GEN-LAST:event_btnUpActionPerformed
     
        private void txtNumberOfBlanksActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNumberOfBlanksActionPerformed
            // TODO add your handling code here:
        }//GEN-LAST:event_txtNumberOfBlanksActionPerformed
     
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
              chooser.showSaveDialog(this);
                File file = chooser.getSelectedFile();
                try {
                    outFile = new FileWriter(file);
                for (int i = 0; i < txtCell.length; i++) {
                for (int j = 0; j < txtCell[0].length; j++) {
                    outFile.write(txtCell[i][j].getText());
                    
               }}
                    outFile.close();
                }
                catch (IOException e) {
                    JOptionPane.showMessageDialog(this, "File Error");
                }
     
     
        }//GEN-LAST:event_jButton2ActionPerformed
     
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
           if(evt.getSource()==jButton3){
               for (int i = 0; i < txtCell.length; i++) {
                for (int j = 0; j < txtCell[0].length; j++) {
     
                        txtCell[i][j].setText("");
                        txtCell[i][j].setEditable(false);
                        txtCell[i][j].setBackground(LIGHT_GRAY);
     
     
                   }
           }}
        }//GEN-LAST:event_jButton3ActionPerformed
        /**
         * One of the cells has been clicked.  If it is a right-click on an
         * editable cell then display the CellNoteView for the user to view
         * and/or amend the notes for this cell.
         * @param evt The MouseEvent.
         */
        private void txtCellMouseClicked(java.awt.event.MouseEvent evt) { 
     
            // Get the JTextBox that has been clicked.
            JTextField txtCellClicked = (JTextField) evt.getSource();
            
            // We will only do anything if an editable cell has been clicked 
            // and it is a right-click (evt.BUTTON3)
            if (!txtCellClicked.isEditable() ||  evt.getButton() != MouseEvent.BUTTON3 )
                return;
            
            int x = 0, y = 0;
            
            // Find the x, y co-ordinates of the cell clicked
            for (int i = 0; i < txtCell.length; i++) {
                for (int j = 0; j < txtCell[0].length; j++) {
                    if (txtCellClicked == txtCell[i][j]) {
                        x = i + 1;
                        y = j + 1;
                    }
                }
            }       
            // Get the CellNoteView window for the cell that has been clicked
            // if it already has one open.
            CellNoteView cellNoteView = openNotes.get(new CellCoordinates(x, y));
            if (cellNoteView == null) { // not already open so open it
                if (cellNotes == null) {
                    cellNotes = new CellNotes();
                }
                // Create the note window and make it visible
                cellNoteView = new CellNoteView(this, new CellCoordinates(x, y), cellNotes);
                cellNoteView.pack();
                cellNoteView.setVisible(true);
                openNotes.put(new CellCoordinates(x, y), cellNoteView);
                txtCellClicked.setBackground(GREEN); // indicate that there is a note
            } else { // already open - so bring it to the front
                cellNoteView.toFront();
            }
        }
        
        /**
         * Called by a CellNoteView to indicate that it is closing.
         * @param coords The co-ordinates of the cell whose note is closing.
         * @param isEmpty If true it means the note is empty.  If false it means
         * that the notes for this cell has content.
         */
        public void cellNoteViewClosing(CellCoordinates coords, boolean isEmpty) {
            // If the note is empty (i.ep. doesn't contain any information) then set
            // it back to WHITE from GREEN
            if (isEmpty) { 
                txtCell[coords.getX() - 1][coords.getY() - 1].setBackground(WHITE);
            }
            // Remove it from the collection of open notes.
            openNotes.remove(coords);
        }
        /**
         * Start the timer running for the puzzle.
         * @param restart If true it means that the timer is being restarted (i.ep.
         * it was previsiously running and then paused).  If false
         * it means the timer is being started for the first time.
         */
        private void startTiming(boolean restart) {
            if (restart) { // restarting after a pause so adjust
                startTime += (System.currentTimeMillis() - currentTime);
            } else { // starting for the first time
                startTime = System.currentTimeMillis();
            }
            lblTimer.setText("0");
            
             /* this timer will go off every 10th of a second
              * and the timerHasPinged() method will be called
              */
            tim = new Timer(100, new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    timerHasPinged();
                }
            });
            tim.start();
        }
        /**
         * Called automatically when the timer expires.  The time display
         * should be updated.
         */
        public void timerHasPinged() {
            currentTime = System.currentTimeMillis();
            
            // How long has been taken so far?
            long secsTaken = (currentTime - startTime) / 1000;
            long minsTaken = secsTaken / 60;
            secsTaken %= 60;
            // Format the time as m:ss
            Formatter fmt = new Formatter();
            fmt.format("%d:%02d", minsTaken, secsTaken);
            lblTimer.setText(fmt.toString());
        }
        
        /**
         * Main entry point for the program.
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new SudokuView().setVisible(true);
                }
            });
        }
        
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton btnCheck;
        private javax.swing.JButton btnDown;
        private javax.swing.JButton btnGetPuzzle;
        private javax.swing.JButton btnPauseRestart;
        private javax.swing.JButton btnUp;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPanel jPanel3;
        private javax.swing.JPanel jPanel4;
        private javax.swing.JLabel lblTimer;
        private javax.swing.JTextField txtNumberOfBlanks;
        // End of variables declaration//GEN-END:variables
        
        // Two-dimensional array of JTextFields representing the Sudoku
        // puzzle grid.
        private JTextField [] [] txtCell;
     
        
        // Cells can have notes attached by the user.  cellNotes is used to hold them.
        private CellNotes cellNotes;
           private FileWriter outFile;
          private JFileChooser chooser = new JFileChooser();
     
        
        // When the user right-clicks a cell a window opens to display the note.  
        // openNotes keeps a record of all currently open notes windows.
        private HashMap<CellCoordinates, CellNoteView> openNotes;
        
        // variables for managing the clock
        private long startTime,
                     currentTime,
                     totalTime;    
        private Timer tim; // timer used to manage the clock;
    /*
     public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            Line2D x1 = new Line2D.Float(0, 266, 798, 266);
            Line2D x2 = new Line2D.Float(0, 404, 798, 404);
            Line2D y1 = new Line2D.Float(271, 128, 271, 541);
            Line2D y2 = new Line2D.Float(535, 128, 535, 541);
            g2.draw(x1);
            g2.draw(x2);
            g2.draw(y1);
            g2.draw(y2);
      }
      */
     
    public synchronized void paint(final Graphics g) {
    super.paint(g);
    g.drawLine(0, 266, 798, 266);
    g.drawLine(0, 404, 798, 404);
    g.drawLine(271, 128, 271, 541);
    g.drawLine(535, 128, 535, 541);
    }
     
     
     
    }
     

  6. #6
    Avatar von zerix
    zerix zerix ist offline Hausmeister
    tutorials.de Moderator
    Registriert seit
    May 2005
    Beiträge
    4.335
    Ich weiß jetzt nicht was du erreichen möchtest, mit diesen Linien. Man sollte aber nicht die paint-Methode des Fensters überschreiben, sondern selbst eine Klasse schreiben, die von JPanel oder von JComponent abgeleitet wird und dann dort die paintComponent-Methode überschreiben.
    Diese eigene Komponente fügt man dann dem Fenster hinzu.

    Gruß

    Sascha
     
    Es ist schwer Allwissend zu sein. Aber ich komme damit klar. ;-)

  7. #7
    Code46 Code46 ist offline Mitglied Gold
    Registriert seit
    Sep 2008
    Beiträge
    184
    hier überschreibe ich die Method:

    Code :
    1
    2
    3
    4
    5
    6
    
    jPanel1 = new JPanel(){
    public void paint(Graphics g){
    super.paint(g);
    paint(g);
    }
    };


    Dies ist der code wo ich die linien zeichne

    Code :
    1
    2
    3
    4
    5
    6
    7
    
    public void paint(final Graphics g) {
    super.paint(g);
    g.drawLine(0, 266, 798, 266);
    g.drawLine(0, 404, 798, 404);
    g.drawLine(271, 128, 271, 541);
    g.drawLine(535, 128, 535, 541);
    }

    Trotzdem Funktioniert es nicht
     

  8. #8
    Avatar von zerix
    zerix zerix ist offline Hausmeister
    tutorials.de Moderator
    Registriert seit
    May 2005
    Beiträge
    4.335
    Ich habe weiter oben mal erwähnt, dass du nicht die paint-Methode sondern die paintComponent überschreiben sollst.
    Es bringt auch nichts, wenn du die paint-Methode des Fensters überschrieben lässt und diese in einem JPanel auch noch aufrufst.

    Das Zeichnen der Linie darf nur in der paint-Component des JPanels vorkommen.
    Also in dem JPanel auf dem die Textfelder hinzugefügt werden.

    Gruß

    Sascha
     
    Es ist schwer Allwissend zu sein. Aber ich komme damit klar. ;-)

  9. #9
    Code46 Code46 ist offline Mitglied Gold
    Registriert seit
    Sep 2008
    Beiträge
    184
    kannst du mir vielleicht ein beispiel geben ****
     

  10. #10
    MiMi MiMi ist offline Mitglied Smaragd
    Registriert seit
    Sep 2007
    Beiträge
    1.177
    Erstelle einfach eine neue Klasse die von JPanel erbt und dort ueberschreibe die paintComponent Methode und nicht die paint-Methode.
    Dazu braucht man kein Beispiel
     
    Waere super wenn ihr euren Code in dieser Form einfuegt:
    [JAVA]System.out.println("Test :)");[/JAVA]
    wird zu:
    Code java:
    1
    
    System.out.println("Test :)");
    Tabs sind uebrigens keine Feinde :)
    Der "Erledigt Button" beisst net :) Und der "Danke Button" ist auch nicht nur zur zierde
    Danke :D

Ähnliche Themen

  1. Antworten: 0
    Letzter Beitrag: 19.04.10, 15:42
  2. Indesign: Master- Seiten in den Vordergrund bringen?
    Von iberlin im Forum Desktop Publishing (DTP)
    Antworten: 4
    Letzter Beitrag: 28.04.09, 17:55
  3. Programm in den Vordergrund bringen klappt nicht (immer)
    Von vfl_freak im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 0
    Letzter Beitrag: 02.03.09, 17:20
  4. Prüfen, ob eine Anwendung im Vordergrund ist?
    Von vfl_freak im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 10
    Letzter Beitrag: 28.01.09, 10:32
  5. JInternalFrame in den Vordergrund bringen
    Von vaporizer im Forum Java
    Antworten: 4
    Letzter Beitrag: 09.05.05, 12:12